自動備份FortiGate設定檔
日前協助幫大師級人物寫一隻小程式…
備份幾台FortiGate的設定檔…
這些設備都有切VDOM…
所以還單純一點
這是shell script配合expect…
用ssh登入設備…再把備份檔丟去tftp server…
然後再移至分類好的資料夾中..
然後設定cron讓他定時去跑這個script
要特別注意tftp的根目錄…記得開啟建立新檔案的功能…
因為是每天備份…所以我就用設備的ip作分類…
檔名就用備份時的日期….
#!/bin/bash
user="帳號"
password="密碼"
tftp_ip="tftp的主機"
fg_devices="FG的ip1 FG的ip2"
date="$(date +"%Y%m%d")"
for fg_device in $fg_devices ; do
expect -c "
set timeout 30
spawn ssh $user@$fg_device
expect \"(yes/no)\" { send \"yes\r\"; exp_continue }
expect \"*?assword:*\"
set send_slow {1 0.05}
send -s \"$password\r\"
send_user \"$password\r\"
expect \"FG*\"
send -s \"config global\n\"
expect \"FG*\"
send -s \"execute backup full-config tftp $date.cfg $tftp_ip\n\"
expect \"FG*\"
send -s \"exit\n\"
interact"
mv /home/tftproot/$date.cfg /home/fgconfig/$fg_device/$date.cfg
done
p.s
因為其中一個設備…他的設定檔實在太多
導致每次都沒辦法將他的設定檔抓下來(因為閒置太久)…
所以特別加上timeout 30
然後再加上
expect \"FG*\"
讓他可以跟著設備發出的訊息做下一步動作….
總算解決設定檔太大而無法下載完的問題…
尚無回應.