Cisco python config backup 4

sudo apt-get update
sudo apt-get install python python-pexpect python-paramiko
#!/usr/bin/python
import paramiko, pexpect, hashlib, StringIO, re, getpass, os, time, ConfigParser, sys, datetime, cmd, argparse

### DEFINE VARIABLES

currentdate=”10-16-2014″
hostnamesfile=’vpnhosts’
asahost=”192.168.222.1″
tacacsuser=’testuser’
userpass=’Password1′
enpass=’Password2′
currentipaddress=”192.168.222.1″
currenthostname=”TESTASA”

#dummy=sys.argv[0]
#currentdate=sys.argv[1]
#currentipaddress=sys.argv[2]
#tacacsuser=sys.argv[3]
#userpass=sys.argv[4]
#enpass=sys.argv[5]
#currenthostname=sys.argv[6]

parser = argparse.ArgumentParser(description=’Get “show version” from a Cisco ASA.’)
parser.add_argument(‘-u’, ‘–user’,     default=’cisco’, help=’user name to login with (default=cisco)’)
parser.add_argument(‘-p’, ‘–password’, default=’cisco’, help=’password to login with (default=cisco)’)
parser.add_argument(‘-e’, ‘–enable’,   default=’cisco’, help=’password for enable (default=cisco)’)
parser.add_argument(‘-d’, ‘–device’,   default=asahost, help=’device to login to (default=192.168.120.160)’)
args = parser.parse_args()

#python vpnbackup.py $currentdate $currentipaddress $tacacsuser $userpass $enpass $currenthostname

def asaLogin():

#start ssh”)
child = pexpect.spawn (‘ssh ‘+tacacsuser+’@’+asahost)

#testing to see if I can increase the buffer
child.maxread=9999999

#expect password prompt”)
child.expect (‘.*assword:.*’)
#send password”)
child.sendline (userpass)
#expect user mode prompt”)
child.expect (‘.*>.*’)
#send enable command”)
child.sendline (‘enable’)
#expect password prompt”)
child.expect (‘.*assword:.*’)
#send enable password”)
child.sendline (enpass)
#expect enable mode prompt = timeout 5″)
child.expect (‘#.*’, timeout=10)
#set term pager to 0″)
child.sendline (‘terminal pager 0’)
#expect enable mode prompt = timeout 5″)
child.expect (‘#.*’, timeout=10)
#run create dir function”)
createDir()
#run create show version”)
showVersion(child)
#run create show run”)
showRun(child)
# run showCryptoIsakmp(child)
showCryptoIsakmp(child)
# run dirDisk0(child)
dirDisk0(child)
# run showInterfaces(child)
showInterfaces(child)
#run  showRoute”)
showRoute(child)
#run showVpnSessionDetail”)
showVpnSessionDetail(child)
# run showVpnActiveSessions(child)
showWebVpnSessions(child)
# run showVpnActiveSessions(child)
showAnyConnectSessions(child)
#send exit”)
child.sendline(‘exit’)
#close the ssh session”)
child.close()

def createDir():
if not os.path.exists(currentdate):
os.makedirs(currentdate)
if not os.path.exists(currentdate+”/”+currenthostname):
os.makedirs(currentdate+”/”+currenthostname)

def showVersion(child):
#setting a new file for output”)
fout = file(currentdate+”/”+currenthostname+”/”+currenthostname+datetime.datetime.now().strftime(“%m-%d-%Y”)+”sh-ver.txt”,’w’)
#capturing the command output to the file”)
child.logfile_read = fout
#sending show version”)
child.sendline(‘show version’)
#expect enable mode prompt = timeout 400″)
child.expect(“.*# “, timeout=50)
#closing the log file”)
fout.close()

def showRun(child):
#setting a new file for output”)
fout = file(currentdate+”/”+currenthostname+”/”+currenthostname+datetime.datetime.now().strftime(“%m-%d-%Y”)+”sh-run.txt”,’w’)
#capturing the command output to the file”)
child.logfile_read = fout
#sending more system running-config”)
child.sendline(‘more system:running-config’)
#expect enable mode prompt = timeout 400
child.expect(“.*# “, timeout=999)
#closing the log file
fout.close()

def showCryptoIsakmp(child):
#setting a new file for output”)
fout = file(currentdate+”/”+currenthostname+”/”+currenthostname+datetime.datetime.now().strftime(“%m-%d-%Y”)+”cryptoisakmp.txt”,’w’)
#capturing the command output to the file”)
child.logfile_read = fout
#sending show crypto isakmp sa”)
child.sendline(‘show crypto isakmp sa’)
#expect enable mode prompt = timeout 400
child.expect(“.*# “, timeout=50)
#closing the log file
fout.close()

def dirDisk0(child):
#setting a new file for output”)
fout = file(currentdate+”/”+currenthostname+”/”+currenthostname+datetime.datetime.now().strftime(“%m-%d-%Y”)+”dirdisk0.txt”,’w’)
#capturing the command output to the file”)
child.logfile_read = fout
#sending dir disk0:”)
child.sendline(‘dir disk0:’)
#expect enable mode prompt = timeout 400
child.expect(“.*# “, timeout=75)
#closing the log file
fout.close()

def showInterfaces(child):
#setting a new file for output”)
fout = file(currentdate+”/”+currenthostname+”/”+currenthostname+datetime.datetime.now().strftime(“%m-%d-%Y”)+”interfaces.txt”,’w’)
#capturing the command output to the file”)
child.logfile_read = fout
#sending show interface”)
child.sendline(‘show interface’)
#expect enable mode prompt = timeout 400
child.expect(“.*# “, timeout=100)
#closing the log file
fout.close()

def showRoute(child):
#setting a new file for output”)
fout = file(currentdate+”/”+currenthostname+”/”+currenthostname+datetime.datetime.now().strftime(“%m-%d-%Y”)+”show-route.txt”,’w’)
#capturing the command output to the file”)
child.logfile_read = fout
#sending show route”)
child.sendline(‘show route’)
#expect enable mode prompt = timeout 400
child.expect(“.*# “, timeout=300)
#closing the log file
fout.close()

def showVpnSessionDetail(child):
#setting a new file for output”)
fout = file(currentdate+”/”+currenthostname+”/”+currenthostname+datetime.datetime.now().strftime(“%m-%d-%Y”)+”vpnsession.txt”,’w’)
#capturing the command output to the file”)
child.logfile_read = fout
#sending show vpn-sessiondb detail”)
child.sendline(‘show vpn-sessiondb detail’)
#expect enable mode prompt = timeout 400
child.expect(“.*# “, timeout=50)
#closing the log file
fout.close()

def showWebVpnSessions(child):
#setting a new file for output”)
fout = file(currentdate+”/”+currenthostname+”/”+currenthostname+datetime.datetime.now().strftime(“%m-%d-%Y”)+”webvpns.txt”,’w’)
#capturing the command output to the file”)
child.logfile_read = fout
#sending show vpn-sessiondb webvpn”)
child.sendline(‘show vpn-sessiondb webvpn’)
#expect enable mode prompt = timeout 400
child.expect(“.*# “, timeout=200)
#closing the log file
fout.close()

def showAnyConnectSessions(child):
#setting a new file for output”)
fout = file(currentdate+”/”+currenthostname+”/”+currenthostname+datetime.datetime.now().strftime(“%m-%d-%Y”)+”anyconnectvpns.txt”,’w’)
#capturing the command output to the file”)
child.logfile_read = fout
#sending show vpn-sessiondb anyconnect”)
child.sendline(‘show vpn-sessiondb anyconnect’)
#expect enable mode prompt = timeout 400
child.expect(“.*# “, timeout=999)
#closing the log file
fout.close()

def main():
#Nothing has been executed yet
#executing asaLogin function
asaLogin()
#Finished running parTest\n\n Now exiting

main()

assyrian technical blog