ASA config backup from Linux

I have used some scripts to automate at work place, never got a chance to write blog.

On community request here one of the blog like to explain here ..more to come either by volunteer or by demand. (when i get free time)

here is the setup

Simple Linux ubuntu runnng my Python3 script and ASA running virtual

below script used to test: (make sure you read before you try any live environment)

I have below folder contain script and deviceip (that is ASA IP)

asabackup.py code as below

#!/usr/bin/env python
# This is my script lab purpose, when you using at real network read carefully before excturing - Balaji Bandi
import sys
import time
import paramiko
import os
import cmd
import datetime

###set date and time
now = datetime.datetime.now()

###start FOR ...in
f = open('deviceip')
for ip in f.readlines():
        ip = ip.strip()
        ###prefix files for backup
        filename_prefix ='/var/scripts/asabackup/' + ip

        ###session start
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect('10.10.9.253', username='bbandi', password='cisco@123')

        ###ssh shell
        chan = client.invoke_shell()
        time.sleep(2)
        ###terminal lenght for no paging
        chan.send('terminal pager lines 0\n')
        time.sleep(1)
        ###show config and write output
        chan.send('sh run\n')
        time.sleep(10)
        output = chan.recv(999999)
        #filename = "%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" % (filename_prefix,now.day,now.month,now.year,now.hour,now.minute,now.second)
        filename = "%s_%.2i%.2i%i_%.2i%.2i%.2i" % (filename_prefix,now.year,now.month,now.day,now.hour,now.minute,now.second)
        ff = open(filename, 'a')
        #ff.write(output)
        ff.write(output.decode("utf-8") )
        ff.close()
        ###close ssh session
        client.close()

        print (ip)
        f.close()

deviceip

asafw

results by running as below : ( you will see device name with date format backup stored in the folder)

Note : This covers only script backup, ASA config basic already configured.

happy Labbbiinnnnnnnnnnnnng!