list.txt
192.168.1.1:root:passwd
192.168.1.2:root:passwd
…
192.168.1.50:root:passwd
cat/root/list.txt |
whileread row;do
IP=$(echo $row |cut -d : -f 1 )
username=$(echo $row |cut -d : -f 2 )
passwd=$(echo $row |cut -d : -f 3 )
pr='sysctl –w –q net.ipv4.tcp_tw_reuse = 1'
/usr/bin/expect -f /root/abc.sh"$IP" "$username" "$passwd" "$pr"
done
expect.sh
#!/usr/bin/expect -f
setip [lindex $argv 0]
setusername [lindex $argv 1]
setpassword [lindex $argv 2]
settimeout 10
spawn ssh$username@$ip
expect {
"*yes/no"{ send "yes\r"; exp_continue}
"*password:"{ send "$password\r" }
}
expect"#*"
send"[lindex $argv 3]\r"
send "exit\r"
expecteof
本文介绍了一种通过脚本批量实现SSH连接并下发特定命令的方法。该方法使用expect脚本语言来自动化SSH登录过程,并通过读取配置文件中的主机列表、用户名及密码,实现了对多台服务器的统一管理和维护操作。
2175

被折叠的 条评论
为什么被折叠?



