centos7 定时任务用expect自动化脚本定时重启网络设备路由器交换机
1.安装expect
yum install expect -y
[root@localhost opt]# expect
expect1.1>
2.编写exp脚本
vim reboot.exp
#/usr/local/bin/expect
spawn telnet “ip地址”
expect “H3C login:”
send “用户名\n”
expect “Password:”
send “密码\n”
expect “”
send “reboot\r”
expect “This command will reboot the system”
send “Y\r”
expect eof
3.定时任务
每天凌晨3点重启设备,并将日志记录到reboot.log
crontab -e
0 3 * * * date >> /opt/reboot.log && expect /opt/reboot.exp >> /opt/reboot.log