Thursday 19 May 2022

Paramiko - config grab with Cisco IOS

import time
import paramiko
import getpass
from datetime import datetime

routers = ["192.168.0.1"]
username = raw_input("Please enter your username: ")
password = getpass.getpass("Please enter your password: ")

now_time = datetime.now()
str_now_time = str(now_time)

sshcon = paramiko.SSHClient()
sshcon.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for target in routers:
f = open("{0}-{1}-txt".format(target,str_now_time) , "w")
print ('Attempting to connect to {0}'.format(target))
sshcon.connect(hostname=target,username=username,password=password,look_for_keys=False)
remote_connection = sshcon.invoke_shell()
remote_connection.send("ter len 0\n")
time.sleep(5)
remote_connection.send("show run\n")
time.sleep(5)
output = remote_connection.recv(65535)
# print(output)
print ('Writing config to file')
f.write(output)
sshcon.close
print("Job completed successfully")

No comments:

Post a Comment