Tuesday, December 22, 2020

Python Script to Connect Cisco Device

 

Topology is in gns3 loopback interface is configured on the desktop to connect the router


Python Script Username and password will be prompted for input from the user

import paramiko
import time
import getpass
from pip._vendor.distlib.compat import raw_input

ssh_client = paramiko.SSHClient()

ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
username = raw_input('Enter Username:')
password = getpass.getpass('Enter Password:')
router = {'hostname': '10.10.10.1', 'port': '22', 'username':username, 'password':password}
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('show ip int brie\n')
time.sleep(1)

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

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

c:>python sshconnect.py

Below is the output of the command executed





No comments:

Post a Comment