1

I want to do SSH with username and password to multiple servers need to know option to put username password in below script

for HOST in $(cat ping.txt ) ; do ssh $HOST "ping -c 10  google.com" ; done
6
  • Can easy reach with expect
    – Costas
    Feb 21, 2017 at 6:22
  • 2
    does your server's have same login password? If yes, then make use of sshpass.
    – Rakesh.N
    Feb 21, 2017 at 6:38
  • Can't use expect here..Looking for other solution Feb 21, 2017 at 6:38
  • Client dependency can't download any new file need to work with existing infra Feb 21, 2017 at 6:42
  • 1
    Time to learn how to setup ssh private & public keys. Read any ssh tutorial Sep 27, 2017 at 5:24

2 Answers 2

2

You can use sshpass, but better to use ansible. Example of sshpass usage:

# vim server.list
192.168.0.100
192.168.0.101

# apt-get install sshpass
$ vim script.sh
#!/bin/bash
while read -r line
do
    echo "running $line"
    SSHPASS=PASSWORD sshpass -e ssh-copy-id USERNAME@"$line" -o "StrictHostKeyChecking no"
done < "server.list"

Run script:

$ sh script.sh
running 192.168.0.100
...
Number of key(s) added: 1
running 192.168.0.101
...
Number of key(s) added: 1
1

Install expect,

#script.sh    
#!/usr/bin/expect -f
    username=user
    password=pass
    for HOST in $(cat $1)
            do
                    echo "ssh $username@$HOST"
                    expect "Password:"
                    send "$pass\r"
            done

file.txt
1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4

bash script.sh file.txt

1
  • I required not to use expect. Suggest wayout with shell script with additional addon download. Feb 21, 2017 at 9:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .