View Single Post
  #1   (View Single Post)  
Old 6th June 2008
splooge splooge is offline
New User
 
Join Date: May 2008
Posts: 5
Default Appending to file on remote host via SSH

Hey guys! I'm playing around with scripting for (basically) my first time. I found out most of what I needed through examples I found on google and have finished with about the first three-fourths of the script. Essentially, I am creating a script that makes my life easier when adding domains to my DNS servers. In one of our DNS farms we have 5 servers (1 master, not public facing, and 4 slaves which *are* public facing). In any case, creating the initial zone file and then modifying 5 instances of named.conf can get quite tedious when you're adding almost a new domain every day, so I'm trying to come up with a solution.

The script, when complete, will do this:

1) Create an initial zone file in /etc/namedb/master/
2) Add the zone to named.conf
3) Reload named, to read in the new named.conf
4) via SSH, append the new zone to named.conf on the slaves
5) via rndc, reload the slaves remotely

This part all works EXCEPT for #4 above:
Code:
if [ -z "$1" ] || [ -z "$2" ]
then
        echo "Proper usage of this script is `basename $0` [domain-name] [IP address]"
        exit
fi

#Let's do the zone file ...
echo "Creating initial zone file ..."

echo "\$TTL 300
$1.                     IN SOA  ns1.pwned.com. hostmaster.pwned.com. (
                                2008060401 ; serial
                                1H         ; refresh
                                10M        ; retry
                                1D         ; expire
                                1D         ; minimum
                                )

                        NS      ns1.pwned.com.
                        NS      ns2.pwned.com.

                        A       $2
www                     CNAME   @
" > /etc/namedb/master/db.$1

echo "Changing ownership on db.$1 ..."
chown bind:bind /etc/namedb/master/db.$1

# Let's update named.conf ...
echo "Updating named.conf ..."

echo zone \"$1\"" {
        type master;
        file \"master/db.$1\";
};
" >> /etc/namedb/named.conf

# Reload named for changes to take effect ...
echo "Reloading named ..."

rndc reload
What I *can't* get to work is appending to file on a remote host via SSH. I get errors at the curly braces and the $1 variable doesn't get carried over to the remote box. Here's basically what I am trying that is failing: (Note: ns1 is FreeBSD, ns2 is Gentoo)

Code:
ssh ns2 echo "zone \"test.com\" {
	type slave;
	file \"sec/db.test.com\";
	masters { 72.26.x.x; };
};
" >> /etc/namedb/named.conf
These are the error messages I get:
Code:
bash: line 1: type: slave: not found
bash: -c: line 3: syntax error near unexpected token `}'
bash: -c: line 3: `	masters { 72.26.x.x; };
Can anyone suggest the proper code to append multiple lines to a file on a remote machine via SSH that will ALSO carry the $1 variable over to the remote machine?

Thanks. I hope I was clear enough.

Last edited by splooge; 7th June 2008 at 02:37 AM.
Reply With Quote