1. 程式人生 > 其它 >Juniper srx防火牆定時關機、重啟併發送微信通知

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

效果如下: