#!/usr/bin/expect
for {set ip 11} {$ip <= 20} {incr ip} { # 假设有10台机器
# 复制脚本到各台机器上
spawn scp -P58422 /root/bin/myscript.sh "root@192.168.1.$ip:/root/bin/myscript.sh"
expect "*password:" 检测到屏幕上出现这个字符串时
send "123456\r" 输入以下字符
send "exit\r"
# 在各台机器上执行命令
spawn ssh -p58422 -t -o StrictHostKeyChecking=no "root@192.168.1.$ip"
expect "*password:"
send "123456\r" \r表示回车,\n表示换行
expect "*]#" 检测到屏幕上的提示符为]#时
send "/root/bin/myscript.sh\r" # 执行脚本
expect "*]#"
send "echo \"01 */1 * * * root /root/bin/myscript.sh\" >> /etc/crontab \r" # 放入crontab内
expect "*]#"
send "service crond reload \r" # 重启crond服务
send "exit\r"
}
转载于:https://blog.51cto.com/zhoupupu/1410289