我想使用用户名和密码通过 SSH 连接到多个服务器,需要知道将用户名密码放入下面的脚本中的选项
for HOST in $(cat ping.txt ) ; do ssh $HOST "ping -c 10 google.com" ; done
答案1
您可以使用 sshpass,但最好使用安西布尔。 sshpass 使用示例:
# 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"
运行脚本:
$ 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
答案2
安装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脚本.sh文件.txt