Thursday, February 2, 2017

Cisco ASA can't backup problem.
Backing-up with Python script.

Recently I faced a problem - I can't backup ASA neither via ASDM (ASDM is not working) nor via SCP or FTP (variously errors are appearing). So the only way to backup is to make (because we need to backup VPN keys etc.):
more system:running-config
But it's very annoying to hit Enter many times (my config is all about  10000 lines). So I'll use small Python script (prerequisite is Python pexpect).  
#!/usr/bin/python
# -*- coding: utf-8 -*-

import pexpect
import time

asa_ip = '10.10.1.1'
asa_pass = '123456'
asa_enable_pass = '654321'
asa_name = 'test-asa'
connection_string = 'ssh -1 cisco@' + asa_ip
asa = pexpect.spawn(connection_string)
asa.logfile = open("asa_backup.log", "w")

def sender(exp, resp):
 asa.expect(exp)
 asa.send(resp)
 asa.sendcontrol('m') #send 'Enter' key

sender('password:', asa_pass)
sender('>', 'enable')
sender('Password:', asa_enable_pass)
sender('#', 'conf t')
sender('#', 'terminal pager 15000')
sender('#', 'more system:running-config')
'''ensure that entire config is showed
'we can do without that but
you'll face problem if you have '#' character in your
running-config
'''
asa.expect(': end')
backup = asa.before.split('\n')
sender('#', 'end')
sender('#', 'q')
dt = time.strftime("%Y-%m-%d")
backup_file = asa_name + "-asa_backup-" + dt
with open(backup_file, "w") as f:
 for line_id, line in enumerate(backup):
  if line_id not in (0, len(backup)-1, len(backup)):
   f.write(line)
 f.write(': end')
print "OK"

[admin@gfs1 Desktop]$   chmod +x asa_back.py
[admin@gfs1 Desktop]$   ./asa_back.py
[admin@gfs1 Desktop]$ ./asa_back.py
OK
[admin@gfs1 Desktop]$ ll asa_back*
-rwxrwxr-x 1 admin admin    955 Feb  2 18:20 asa_back.py
-rw-rw-r-- 1 admin admin 449936 Feb  2 18:21 asa_backup-2017-02-02
-rw-rw-r-- 1 admin admin 450298 Feb  2 18:21 asa_backup.log

No comments:

Post a Comment