Juniper srx防火墙定时关机、重启并发送微信通知

方法一:使用event-options进行定时关机

配置前先确定设备的时间是否准确,可通过命令检查

run show system uptime

若时间不准确,需要设置NTP服务器确保设备时间正常

NTP服务器设置参考:set system ntp server ntp1.aliyun.com

 

以下配置为设置防火墙在北京时间每天00:15关机

 

set event-options generate-event shutdowndaily-id404 time-of-day "00:15:00 +0800"
set event-options policy shutdown-srx100 events shutdowndaily-id404
set event-options policy shutdown-srx100 then execute-commands commands "request system power-off"      

若需要设置为重启,可将脚本的power-off 改为reboot

 

方法二:通过shell脚本

由于event-options 执行的条件只有time-interval(每隔XX秒执行) 和 time-of-day (每天指定时间) 两种方式 ,执行起来不够灵活。比如我想设定每周工作日指定时间关机,通过event-options就无法实现。

这种需求可通过shell编写关机脚本,通过crontab定时执行关机脚本实现

在配置模式下通过命令run start shell 进入shell 模式

root@id404# run start shell
root@id404%

首先确定脚本存放的位置 ,我将脚本放置在/cf/var/log/shutdown.sh  也可以放置在其它文件夹,但需要确保设备重启后文件夹不会清空。有部分文件夹会在设备重启后清空的

编写关机脚本

root@id404% vi /cf/var/log/shutdown.sh

 

脚本内容

#!bin/sh
#Author:id404
/sbin/shutdown -h now

修改定时任务

root@% crontab -e

定时任务内容

10 0 * * 1,2,3,4,5 /bin/sh /cf/var/log/shutdown.sh

定时任务设置的内容为周一至同五 00:10 执行关机脚本

 

 

-----------------------------------------------------

进阶:关机前微信通知

微信通知主要是通过shell脚本提交http get请求至通知服务器,由通知服务器推送请求至微信

支持http get的通知服务器有很多 ,可自行选择plushplus、server酱、telegram机器人、BARK等。

我采用的是pushplus通知。

首先通过微信登陆官网 http://pushplus.plus

登陆后找到调用方式一栏

 

 

http://www.pushplus.plus/send?token=bc3046106fec489ad&title=XXX&content=XXX&template=html 

将内容中的title=XXX中的XXX修改为标题,比如“断网通知”;将content=XXX中的XXX修改为内容,比如“ 设备将天5分钟后关机”

注意每个账号的token是不一样的,不要复制我文章上的token内容!!!

 

修改后复制到浏览器并提交,正常情况下微信会收到通知

 

确定微信收到通知后下一步修改关机脚本:

 

#!bin/sh
#Author:id404
curl -d "token=bc3046106fec489ad&title=断网通知&content=宿舍出口将于5分钟后关闭&template=html" http://www.pushplus.plus/send
sleep 300
/sbin/shutdown -h now

 

 

效果如下:

 

    

 

posted @ 2021-07-10 18:55  id404  阅读(589)  评论(0编辑  收藏  举报