Showing posts with label telnet. Show all posts
Showing posts with label telnet. 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)





Saturday, 9 August 2014

simple python script to telnet into cisco IOS device

I am no expert in python but just trying to share , something i used recently. its helpful if you want to telnet to IOS devices and get some output. using python scripts

i am using python version 2.7
you can download it from here
https://www.python.org/download/releases/2.7/

Script:

import os
import telnetlib

def telNetCall():
host  = "xx.xx.xx.xx"
user  = "username"
password = "password"
telnet  = telnetlib.Telnet(host) 
telnet.read_until('Username: ', 3)
telnet.write(user + '\r')
telnet.read_until('Password: ', 3)
telnet.write(password + '\r')
telnet.write('enable' + '\r\n')
telnet.write('enable_password' + '\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))
telNetCall()


the script will telnet to the device and write the show version (or anything you define) into a file with the name as the host IP address.
f = open(host, 'w')

so lets run the script now for a small demo, but before that make sure you saved your script with proper indentation and file extension as .py

 step1: in your command prompt change your directory to the python27 directory

step2: execute the script by just typing the script file name with the extension as shown above

step3: done !!! your output file should be created in the python directory as show below with the file name as           the host IP address