NETWORK ENGINEER BLOG

Tips and Reviews for Engineers

Python で FortiGate のログを取得

Python2.7 で FortiGate のログを収集する際のスクリプト例です。
Automated FortiGate Backups を参考にさせて頂きました。

import re
import sys
import datetime, time
import string
from Exscript.util.interact import read_login
from Exscript.protocols import SSH2

IP         = '192.168.1.83'
DeviceName = 'FGVM'
Username   = 'admin'
Password   = 'password'

Date = datetime.date.today().strftime("%Y-%m-%d")

account = read_login()     # Prompt the user for his name and password
conn = SSH2()              # We choose to use SSH2

print 'Starting Fortilog.py for '+DeviceName+' '+IP
conn.connect(IP)
conn.login(account)        # Authenticate on the remote host

# set terminal to standard removing the --more--- pause
conn.execute('config system console')
conn.execute('set output standard')
conn.execute('end')

# increase the timeout because FortiGate configs are BIG!
conn.set_timeout(120)
conn.execute('show full-configuration')
config_string = conn.response

# clean up the config removing some crud from the top and bottom
config_string = config_string[26:-12]

# save the date stamped config
config_file = open("fortilog-"+DeviceName+"-"+Date+".cfg","w")
config_file.write(config_string)
config_file.close()

conn.send('exit\r')
conn.close()
print "Finished Fortiback.py wrote fortilog-"+DeviceName+"-"+Date+".cfg"