Wednesday, December 23, 2020

Python Script for Cisco Config Backup

 This is a simple python script to backup the config of your cisco device, with date and time into a txt file

import paramiko
import time
from datetime import datetime

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

router = {'hostname': '10.10.10.1', 'port': '22', 'username':'cisco', 'password':'cisco'}
print(f'Connecting to Device...{router["hostname"]}')

ssh_client.connect(**router, look_for_keys=False, allow_agent=False)

shell = ssh_client.invoke_shell()

shell.send('terminal length 0\n')
shell.send('enable\n')
shell.send('cisco\n')
shell.send('show run\n')

time.sleep(2)

output = shell.recv(10000).decode()
print(output)

now = datetime.now()
year = now.year
month = now.month
day = now.day
hour = now.hour
minute = now.minute

file_name = f'{router["hostname"]}-{year}-{month}-{day}-{hour}-{minute}.txt'

with open(file_name, 'w') as f:
f.write(output)

print('Closing Connection')
ssh_client.close()

No comments:

Post a Comment