Netmiko - backup-fortigate.py.md

from scp import SCPClient
import os, paramiko, json

def fortigate_backup(ip, username, password):

    ssh = paramiko.client.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())

    print ("---- Connecting to device {0}".format( ip ))

    ssh.connect(ip, username = username, password = password, timeout = 180)

    with SCPClient(ssh.get_transport(), socket_timeout = 180) as scp:

        config_filename = basepath + "configs/" + ip

        print ("---- Getting and writing configuration from device: " + config_filename)

        scp.get("sys_config", config_filename)

        print ("---- Configuration stored from device: " + config_filename)

    return

#---- Define base path to run from anywhere
basepath = os.path.abspath(os.path.dirname(__file__)) + "/"

#---- Retrieve creds from file
with open(basepath + "creds.json", "r") as f:
    creds = json.load(f)

username = creds["FORTIGATE"]["USERNAME"]
password = creds["FORTIGATE"]["PASSWORD"]

#---- Retrieve list of IPs from file
ips = [line.rstrip("\n") for line in open(basepath + "devices-fortigate")]

for ip in ips:
    fortigate_backup(ip, username, password)