先安装expect
yum install expect
编写下面的脚本:
#!/bin/expect
set timeout 30spawn ssh -l root 192.168.201.225
expect "password:"
send "远程主机的密码"
expect "#"
#send "echo '111'>/root/111.txt\r"
send "cd /root\r"
expect "#"
send "wget http://192.168.192.24:8080/monitor.tar\r"
expect "#"
send "tar -xvf monitor.tar\r"
expect "#"
send "sed -i '/monitor.sh/d' /etc/crontab\r"
expect "#"
send "echo '* * * * * sh /root/monitor/monitor.sh' >> /etc/crontab\r"
expect "#"
send "exit\r"
interact
注意:
send 后面的命令结尾必须加上\r
每个命令后面必须加上expect "#",再执行下一个命令
本文介绍如何利用expect脚本实现SSH登录远程主机并执行一系列自动化任务的过程,包括文件下载、解压、配置定时任务等。
983

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



