-1

I'm trying to setup a DNS server through ssh and am able to send text to the config files, but it requires a password for each line.

 ssh -t $newDNS "sudo sed -i '4iforwarders { $IpDNS; };' /etc/bind/named.conf.options"

 ssh -t $newDNS "sudo sed -i '/^forwarders/i listen-on port 53 { $IpDNS; };' /etc/bind/named.conf.options"

 ssh -t $newDNS "sudo sed -i '/^listen-on/i allow-query { localhost; $subDNS; };' /etc/bind/named.conf.options"

 ssh -t $newDNS "sudo sed -i '/^forwarders/a recursion yes; }/etc/bind/named.conf.options"
plum 0
  • 652
  • 9
  • 21
  • you can convert you multiple calls to `sed` into one `sed` script and then you will only need one `ssh` connection (and password) to make the changes to the file. Look at the [Sed Tutorial](http://grymoire.com/Unix/Sed.html) for examples of multi-statement `sed` scripts. Good luck. – shellter Jul 29 '19 at 19:20

1 Answers1

1

I think you either need to install a ssh key onto each server or use expect and type your password once.

The ssh key solution:
Read this link, but do not set up a password for your RSA key.

The expect solution:

#!/bin/bash

# This function is sent to the server. I've used it for testing
function dosomething {
        ls
}

# The script expects a file with servers separated with newlines.
FILE=$1

# If the file does not exist, it exits. 
[ -f "${FILE}" ] || exit 1

# I didn't know your password and username. If you would like me to add it,
# please sent me a DM or paste it as comment ;-)
read -sp "username: " USERNAME
echo
read -sp "password: " PASSWORD
echo

# While the content of ${FILE} isn't empty...
while IFS= read -r SERVER; do

# .. use expect to spawn ssh, login with ${PASSWORD}, send the dosomething to
# the server, execute dosomething and exit. 
expect <<-EOF
        spawn ssh ${USERNAME}@${SERVER}
        expect "*: " { send "${PASSWORD}\r" }
        expect "*$ " { send "$(typeset -f dosomething)\r" }
        expect "*$ " { send "dosomething\r" }
        expect "*$ " { send "exit\r" }
EOF
done < ${FILE}

# You're up ;-)
exit $?
Bayou
  • 3,293
  • 1
  • 9
  • 22
  • If I've answered your question, would you please accept it? Otherwise update your answer to your needs. – Bayou Jul 30 '19 at 09:04