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
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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
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
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