使用expect实现自动化ssh以及执行命令

该脚本实现自动化无需人机交互登陆远程多个ip(密码都一样)执行命令重启vspc服务。

1.创建ip_list_path

2. sh restartVspc.sh <密码>

#!/bin/bash

set -eux

set timeout 30

password=$1

vspc_ip_list_path='./../../home/houbo/vspc_ip.list_test'

for i in $(cat $vspc_ip_list_path)
do

/usr/bin/expect << EOF
# open a new process
spawn /usr/bin/ssh root@$i

# accept strings from process
expect {
#"(yes/no)?"
#{
# send "yes\n"
# expect "*assword:" { send "$password\n"}
#}
"*assword:"
{

# send strings to the process
send "$password\n"
}
}
expect "*]#"
send "/usr/bin/supervisorctl restart vSPCServer\r"
expect "*]#"
send "exit\r"
expect eof
EOF
done

posted @ 2020-12-07 16:40  frantzz  阅读(480)  评论(0编辑  收藏  举报