Paramiko and Netmiko examples

Paramiko example:

import paramiko
import time

device = '10.1.1.1'
username = 'admin'
password = 'Cisco123'

print('Connecting to: ', device)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=device,
               username=username,
               password=password)
print('\nExecuting command...')

remote_conn = client.invoke_shell()
remote_conn.send("show interface detailed virtual\n")
time.sleep(.5)

print('\n -------------- Print --------------')
output = remote_conn.recv(999999)
print(output.decode())
print('\n -------------- Print --------------')

print('\nTask completed.')
client.close()
print('\nSSH Connection Closed.')

Netmiko example:

from netmiko import ConnectHandler

device = '10.1.1.1'
username = 'admin'
password = 'Cisco123'

client = {'host': device,
          'port': 22,
          'username': username,
          'password': password,
          'device_type': 'cisco_wlc_ssh'}

print('Connecting to: ', device)

net_connect = ConnectHandler(**client)
print('\nExecuting command...')

print('\n -------------- Print --------------')
output = net_connect.send_command('show interface detailed virtual')
print(output)
print('\n -------------- Print --------------')

print('\nTask completed.')
net_connect.disconnect()
print('\nSSH Connection Closed.')

Common error

If the Python file name is paramiko.py or netmiko.py, the following error will occur. Changing the file name can solve this problem.

AttributeError: module 'paramiko' has no attribute 'SSHClient'

Leave a Reply

Your email address will not be published.