Sunday 5 June 2016

updating python script to SSH with and without Enable password for Cisco devices

python script to SSH with and without Enable password for Cisco devices
i have tried this on Cisco IOS, IOS-XE and NX-OS and works for me.

Without enable password requirement to print 'Show hostname' and 'Show version'(you can change that to whatever CLI you need to print.

import paramiko

def sshCall(host,port,user,passw):
  dssh = paramiko.SSHClient()
  dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  dssh.connect(hostname=host,port=port,username=user,password=passw)
  dssh.exec_command('term len 0')
  stdin, stdout, stderr = dssh.exec_command('show hostname')
  bc=stdout.read()
  print bc
  # b=bc.strip('\n')
  stdin, stdout, stderr = dssh.exec_command('show version')
  a= stdout.read()
  print a
  f = open('/output.txt', 'a')

  f.write(bc)

  f.write(a)
  f.close()
  dssh.close()

sshCall('1.1.1.1',22,"username","password)

With enable password

import paramiko
import time

def sshCall(host,port,user,passw):
  dssh = paramiko.SSHClient()
  dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  dssh.connect(hostname=host,port=port, username=user,password=passw)
  shell = dssh.invoke_shell()
  time.sleep(1)
  shell.send('enable\n')
  shell.send('FixStream123!\n')
  time.sleep(1)
  shell.send('term len 0\n')

  shell.send('show version\n')
  time.sleep(2)
  resp = shell.recv(9999)
  print resp
  dssh.close()

sshCall('1.1.1.1',22,"usr","pwd")

No comments:

Post a Comment