View Single Post
Old 7th June 2008
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,131
Default

By using the "here" document construct of the shell will save you a lot of quoting or escaping
Code:
$ cat inline
#!/bin/sh

FILE=named.conf


cat <<END >${FILE}
zone $1 {
        type master;
        file "master/db.$1";
};
END
A sample run
Code:
$ sh inline xyz.com

$ cat named.conf
zone xyz.com {
        type master;
        file "master/db.xyz.com";
};
You even can redirect the output of a "here document" to a file
Code:
$ cat inline_redir
#!/bin/sh

FILE=$(mktemp)

echo ${FILE}
 
cat <<END > ${FILE} 
zone "$1" {
        type slave;
        file "sec/db.$1";
        masters { 72.26.x.x; };
};
END

$ sh inline_redir gorilla.com
/tmp/tmp.XwKYS14669

$ cat /tmp/tmp.XwKYS14669
zone "gorilla.com" {
        type slave;
        file "sec/db.gorilla.com";
        masters { 72.26.x.x; };
};
Now piping through ssh
Code:
$ cat ssh_append
#!/bin/sh

FILE=test.zone

ssh j65nko@parmenides "cat <<END > ${FILE} 
zone "$1" {
        type slave;
        file "sec/db.$1";
        masters { 72.26.x.x; };
};
END
"

$ sh ssh_append mickey_mouse.com

$ ssh j65nko@parmenides 'cat test.zone'
zone mickey_mouse.com {
        type slave;
        file sec/db.mickey_mouse.com;
        masters { 72.26.x.x; };
};
As you can see, the quotation marks are lost.
A fixed version
Code:
$ cat ssh_append                        
#!/bin/sh

FILE=test.zone

ssh j65nko@parmenides "cat <<END > ${FILE}
zone \"$1\" {
        type slave;
        file \"sec/db.$1\";
        masters { 72.26.x.x; };
};
END
"
$ sh ssh_append chimpansee.org 

$ ssh j65nko@parmenides 'cat test.zone' 
zone "chimpansee.org" {
        type slave;
        file "sec/db.chimpansee.org";
        masters { 72.26.x.x; };
};
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote