Created
December 28, 2017 11:47
-
-
Save TannerRayMartin/f0b91c5e492b1f15698656486403ef02 to your computer and use it in GitHub Desktop.
Connecting to a Cisco switch via console cable through a serial port with a laptop using python (pySerial)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Tanner Ray Martin' | |
#this is for python 3, for python 2: change input to raw_input | |
import serial | |
#serial is the main module used and needs to be installed | |
import time | |
''' | |
I have a cisco 2960-s series switch hooked up to a laptop through a serial port so think of 'ser' as 'switch' | |
''' | |
#creating your serial object | |
ser = serial.Serial( | |
port = 'COM3', #COM is on windows, linux is different | |
baudrate=9600, #many different baudrates are available | |
parity='N', #no idea | |
stopbits=1, | |
bytesize=8, | |
timeout=8 #8 seconds seems to be a good timeout, may need to be increased | |
) | |
#open your serial object | |
ser.isOpen() | |
#in this case it returns str COM3 | |
print(ser.name) | |
#first command (hitting enter) | |
command = '\r\n' | |
#convert str to binary (commands sent to switch must be binary) | |
command = str.encode(command) | |
#send the command to the switch | |
ser.write(command) | |
#wait a sec | |
time.sleep(0.5) | |
ser.inWaiting() | |
#get the response from the switch | |
input_data = ser.read(225) #(how many bytes to limit to read) | |
#convert binary to str | |
input_data = input_data.decode("utf-8", "ignore") | |
#print response | |
print(input_data) | |
#create a loop | |
while 1: | |
#enter your own command | |
command = input(':: ') | |
#type 'exit' to end serial session with the switch | |
if command == 'exit': | |
ser.close() | |
exit() | |
else: | |
#convert command to binary | |
command = str.encode(command + '\r\n') | |
#send command | |
ser.write(command) | |
#set response variable (empty binary str) | |
out = b'' | |
#take a short nap | |
time.sleep(.5) | |
#while response is happening (timeout hasnt expired) | |
while ser.inWaiting() > 0: | |
#trying to read one line at a time (100 bytes at a time) | |
out = ser.readline(100) | |
#converting response to str | |
out = out.decode("utf-8", "ignore") | |
#printing response if not empty | |
if out != '': | |
print(out) | |
#repeat until timeout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That was very helpful, thank you