0

I am currently looking for a solution for executing remote commands on multiple local servers from an input file containing the 'user : password' in the following format:

jboss5:manager:192.168.1.101
database1:db01:192.168.20.6
server8:localnet:192.168.31.83
x:z:192.168.1.151
test:mynet:192.168.35.44
.... and others

Some commands I wish to execute remotely:

cd $HOME; ./start_script.sh; wget 192.168.1.110/monitor.sh; chmod +x monitor.sh; ./monitor.sh

I know there is a utility called "sshpass" but not sure how I could apply this utility for my needs.

I am open to any ideas in order to fulfill my need, any help would be very appreciated!

Thanks

1
  • Hi, since you are looking into executing remote commands on multiple servers, you might be interested in Rex (over at rexify.org).
    – Edward
    Sep 3 '18 at 7:02
0

Instead of using password authentication I recommend you to use public key authentication, it is much stronger than plain password and you don't need then sshpass. Just create private keys without passwords and use ssh's option -i, but if you still wish to use passwords, below is solution:

#!/bin/sh

hosts='
jboss5:manager:192.168.1.101
database1:db01:192.168.20.6
server8:localnet:192.168.31.83
x:z:192.168.1.151
test:mynet:192.168.35.44
'

for acct in ${hosts}; do
  user=$(echo "${acct}" | awk -F: '{print $1}')
  pass=$(echo "${acct}" | awk -F: '{print $2}')
  host=$(echo "${acct}" | awk -F: '{print $3}')

  sshpass -p "${pass}" ssh "${user}@${host}" \
    'cd $HOME; ./start_script.sh; wget 192.168.1.110/monitor.sh; chmod +x monitor.sh; ./monitor.sh'
done
2
  • This is awesome! Exactly what I was looking for. Thank you for taking the time to write it down, very appreciated :)
    – John Doe
    Sep 1 '18 at 17:19
  • @JohnDoe Glad to help. Consider in a future switch to public key authentication or at least use some anti-brute force solution like fail2ban on the servers and non default SSH port(22), it really dangerous to use plain passwords if servers exposed to the wild internet.
    – Alex
    Sep 1 '18 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.