is there a way to make a script to act like a host file,

so i would like to say


and it ask me what's the name of the server i type "server1" and the script grap the IP address from a file and ssh to it, i manged to make it work with sshpass but not without the ip !

while read -p 'Enter the ip address for the server: ' ip_addr && [[ -z "$ip_addr" ]] ; do
 echo " please, no blank IP address!"

 sshpass -p "$password" ssh -o StrictHostKeyChecking=no $ip_addr "x=$y"  /bin/bash << "EOF"
                        sudo su - $x
  • 1
    that script fragment is broken. missing the ending "EOF" to start with. – cas Sep 11 at 4:56
  • thank you, this is a part of the script not all of it – Jack albars Sep 11 at 5:10
  • 2
    i guessed that. the problem is that it's not a minimum working example – cas Sep 11 at 5:20
  • Not an answer, but you should really look at Ansible. – xenoid Sep 11 at 6:22

Try the following:

  • create a Textfile with all the hosts to ssh to
  • read the file line by line, and execute something there


host1 #comment
host2 # also comment
host3 # # # it does not matter whats after the hostname


cat Textfile | while read srv comment do;
sshpass -p "$password" ssh -o StrictHostKeyChecking=no USER@${srv} "COMMANDS"

For the Password Thing: You should consider replacing the Password with public key authentication. For that to work, you Need to

  • create a local ssk keypair with 'ssh-keygen ...'
  • add the public key to the remote systems authorized keys with 'ssh-copy-id ..'
  • I will try it thank you so much – Jack albars Sep 11 at 9:02

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.