I have a shell script that connects to a remote machine to perform actions. One of those actions, is to set one or more DNS servers. While largely static data is easy to capture and pipe to the remote machine via SSH:
config_ntp()
{
ssh -T admin@server_ip <<-NTPSERVER
sysconf ntp addserver $NTPSERVER
NTPSERVER
}
Creating a dynamically sized list of commands, is trickier than I thought. What I have:
DNSSERVERS=(8.8.8.8 8.8.8.7)
config_dns()
{
cmd=""
for server in ${DNSSERVERS[@]}; do
cmd+="network dns add nameserver $server$'\n' "
done
cmd+="service dns restart$'\n'"
echo -e "cmd: $cmd"
ssh -T admin@server_ip $cmd
}
The result of calling this:
$ sh setup.sh
cmd: network dns add nameserver 8.8.8.8$'
' network dns add nameserver 8.8.8.7$'
' service dns restart$'
'
Syntax Error: Invalid character detected: '\'.
Command Result : 22 (Invalid argument)
Exiting...
That was my latest incarnation. I was playing with $'\n'
as suggested elsewhere... previously, I just had \n
, which resulted in the same error.
How do I create a variable containing a list (variable length, dynamically generated) of commands to pipe, via ssh, to a remote machine?