shell 免密批量执行脚本

作用: 

1. 可以在执行脚本过程中免输入yes 

2.可以在执行脚本过程中免输入密码

脚本内容如下:  

***  ipfile.txt  为主机列表

[root@YZSJHL-N1-1 opt]# cat remote.sh

#!/usr/bin/expect -f

#chmod 777 distribute_file.sh

password=xxxxxx
path=$1
topath=$2

while read -r ipline
do

expect<<-END
#spawn scp -qpr $path root@${ipline}:${topath}
spawn ssh root@${ipline} "sh /opt/opmysql.sh"
expect {
"(yes/no)?" {send "yes "; exp_continue}
"${ipline}'s password:" {send "${password} "}
"Permission denied" { send_user "[exec echo " Error: Password is wrong "]"; exit}
}
expect eof
exit
END
done < ipfile.txt

原文地址:https://www.cnblogs.com/lixinliang/p/11249874.html