Showing posts with label ssh. Show all posts
Showing posts with label ssh. Show all posts

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")

Tuesday, 12 August 2014

python script to ssh and telnet and take input from Excel sheet

So this is a super cool script that i am going to share .

functions of the script

1. python script takes input from excel sheet cells for 
  • IP address
  • username 
  • password
  • transport type(ssh/telnet)
  • enable password
2. python script runs the functions defined and captures output of your choice and stores it in a file with the hostname/IP address of the router

assuming you have pre-installed the packages as shown in the previous script

import paramiko
import sys
import os
import xlrd
import unidecode
import telnetlib
import time

def sshCall(data1,data2,data3):
dssh = paramiko.SSHClient()
dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
dssh.connect(data1.encode('ascii'),port=22, username=data2.encode('ascii'), password=data3.encode('ascii'))  
dssh.exec_command('term len 0')
stdin, stdout, stderr = dssh.exec_command('show hostname')
bc=stdout.read()
b=bc.strip('\n')
stdin, stdout, stderr = dssh.exec_command('sh ver')
a= stdout.read()
print bc
f = open(b, 'a')
f.write(a)
f.close()
dssh.close()
def telNetCall(data1,data2,data3,data5):
hostlist= [ (data1),] 
for host in hostlist:
print data1
user  = (data2)
password = (data3)
ena = (data5)
telnet  = telnetlib.Telnet(host) 
telnet.read_until('Username: ', 3)
telnet.write(user.encode('ascii') + '\r')
telnet.read_until('Password: ', 3)
telnet.write(password.encode('ascii') + '\r')
telnet.write('enable' + '\r\n')
telnet.write(ena.encode('ascii') + '\r\n')
telnet.write('term len 0' + '\r\n')
telnet.write("show version"+ "\r\n")
telnet.write('exit' + '\r')
a=telnet.read_all()
f = open(host, 'w')
f.write(str(a))


os.chdir("C:/myscript/")
workbook = xlrd.open_workbook('Book1.xls')
worksheet = workbook.sheet_by_name('Sheet1') 


for x in range(1,4):
data1 = (worksheet.cell(x,0).value).decode('ascii')
data2 = (worksheet.cell(x,1).value).decode('ascii')
data3 = (worksheet.cell(x,2).value).decode('ascii')
data4 = (worksheet.cell(x,3).value).decode('ascii')
data5 = (worksheet.cell(x,4).value).decode('ascii')
if data4 == 'telnet':
telNetCall(data1,data2,data3,data5)
else:
sshCall(data1,data2,data3)





Sunday, 10 August 2014

python script to ssh into cisco device

So this one had my head scratching not because the script was too complex , but the overall effort to run the script. 

Again as i am new to python it took a while to get on with installing the correct packages and loading the right binaries.


so i will go step by step first on what is needed to run the ssh script in python 2.7

step1: installing the paramiko package

go to this link and download the paramiko package and unzip it in the python 2.7 folder
https://pypi.python.org/pypi/paramiko/1.14.0

now install it as show below

step2: intalling the ecdsa0.11 package

go to this link and download the ecdsa0.11 package and unzip it in the python 2.7 folder

now install it as show below

step3: download the relevant pre built pycrypto binaries for windows machine from link below


step4: so finally the script

import paramiko
import sys
import os

dssh = paramiko.SSHClient()
dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
dssh.connect('10.95.34.104',port=22, username='admin', password='Cisc0123')
dssh.exec_command('term len 0')
stdin, stdout, stderr = dssh.exec_command('sh ver')
a= stdout.read()
print a
f = open('testing.txt', 'a')
f.write(a)
f.close()
dssh.close()