Expect+Crontab 定时重启山石防火墙
expect
安装expect
yum install expect
shell脚本
存储位置/usr/etc/script/test.sh
#!/usr/bin/expect -f
spawn ssh [YOUR_USER_NAME]@[YOUR_DEVICE_IP]
expect {
"(yes/no)" {send "yes\r";exp_continue}
"*password:" {send -- "[YOUR_PASSWORD]\r"}
}
expect "*#"
send "show ip route\r"
expect "*More*"
send "a"
expect "*#"
send "reboot\r"
expect {
"*System configuration has been modified*"
{send "y\r";exp_continue}
"*System reboot, are you sure?*"
{send "y\r";exp_continue}
"*Please choose one:*"
{send "a"}
}
set timeout 60
expect eof
exit
Crontab
检查Crontab状态
[root@MiWiFi-R1CM-srv script]# systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2021-11-09 12:03:58 CST; 2min 31s ago
Main PID: 20411 (crond)
Tasks: 1
Memory: 1.3M
CGroup: /system.slice/crond.service
└─20411 /usr/sbin/crond -n
11月 09 12:03:58 MiWiFi-R1CM-srv systemd[1]: Started Command Scheduler.
11月 09 12:03:58 MiWiFi-R1CM-srv systemd[1]: Starting Command Scheduler...
11月 09 12:03:58 MiWiFi-R1CM-srv crond[20411]: (CRON) INFO (RANDOM_DELAY will be scaled with factor...d.)
11月 09 12:03:58 MiWiFi-R1CM-srv crond[20411]: (CRON) INFO (running with inotify support)
11月 09 12:03:58 MiWiFi-R1CM-srv crond[20411]: (CRON) INFO (@reboot jobs will be run at computer's ...p.)
11月 09 12:05:01 MiWiFi-R1CM-srv crond[20411]: (root) RELOAD (/var/spool/cron/root)
Hint: Some lines were ellipsized, use -l to show in full.
增加Crontab任务
crontab -e
6 12 9 * * /usr/bin/expect -f /usr/etc/script/test.sh
备注
expect特殊字符转义
- expect需要转义的字符串
1、 \ 需转义为 \\
2、 } 需转义为 \}
3、 [ 需转义为 \[
4、 $ 需转义为 \$
5、 ' 需转义为 \'
6、 " 需转义为 \\"
- send转义
在特殊字符前加\完成转义
浙公网安备 33010602011771号