SemFio Networks

  • Home
  • About
  • Services
    • Wi-Fi DevOps with Mist
  • Partners
    • Mist
  • Training
  • Contacts
  • Blog
  • Home
  • About
  • Services
    • Wi-Fi DevOps with Mist
  • Partners
    • Mist
  • Training
  • Contacts
  • Blog

Establish a Console Connection within a Python script with pySerial

7/15/2020

1 Comment

 
Picture
I have been using the python library called pySerial and wanted to share how I made it work on my end.
​
I have used it to establish a console connection to a Cisco AP in order to perform send the initial set of configurations within a python script and automate the process. I will only talk about using it on macOS as this is what I use. See: Configure a Cisco IOS-XE AP for an APoS Site Survey using Python.

Console Connection

In order for pySerial to work properly, you need to first establish your console connection with the network equipment you are configuring. In my case, I connected it to a Cisco AP.

Use a console cable to connect your computer to the console port of the equipment. In my case, I am using an 
AirConsole. 
Picture
On macOS, that means retrieving the tty corresponding to your serial connection. In order to see all of the tty available once you have connected your console cable, you can use the following command:
Picture
In my case, you can see that the tty I will need is called: /dev/tty.AirConsole-68-raw-serial 

pySerial Installation

All you have to do to install pySerial is to run this command: python -m pip install pyserial
Picture
Note: the serial python library also exists. Make sure that you install the pyserial library and not the serial one!

Time to Code!

You can now create a python script. To use the library, you need to import it using the following line:

    
Then, you will need to create a serial.Serial object that will open the serial connection to your equipment (my AP here). You can do it within a with statement so it can open and close it nicely for you:

    
You can notice that I need the TTY in order to create that Serial object.
You can also set a timeout of the connection. This is optional and I have set mine to 1sec.
You also notice that I am printing the name of the Serial connection by using the ser.name variable.

In order to send a command, you will have to use this code (in this case we are just sending a return character):

    
Here are a few things to note:
  • The text to be sent needs to be encoded using utf-8
  • The '\r' character has to be added to the command in order to simulate the user pressing on Enter (Note: '\n' didn't work for me when interacting with the Cisco AP)
  • You will have to use the sleep function to wait for the command to be executed and send you the output back
  • The text received will have to be decoded using utf-8

This set of code will have to be executed for every command that you want to send out. So I would recommend creating a function that will do all of this for you. Here is an example:

    
You can use the wait_time to see how long you need to wait in order to receive all the output from the AP. For instance, you will need to wait longer if you are sending a "show run" vs. sending "conf t".

Example - Show AP Summary

Here is an example of a script that will connect to an AP (running EWC) and send out the "show ap summary" to discover which APs are joined:

    
And here is the output when you run the script:
Picture
Source code can be found here: ​github.com/francoisverges/semfio-cisco/blob/master/pyserial-example/show-ap-summary.py

Resources

  • PySerial documentation: pyserial.readthedocs.io/en/latest/pyserial.html
  • Use of PySerial to configure a Cisco AP for an APoS: www.semfionetworks.com/blog/configure-a-cisco-ios-xe-ap-for-an-apos-site-survey-using-python
1 Comment
james lead link
12/24/2020 01:03:44

Sir, I have a question, I am a 2nd-year student looking to get into cybersecurity. Is python necessary for that?

Reply



Leave a Reply.

    Picture

    François Vergès

    François Vergès is the founder of SemFio Networks. As a Network Engineer, he has a real passion for Wi-Fi.

    Picture Picture Picture

    Categories

    All
    5G
    5GHz
    6GHz
    802.11
    802.11ax
    802.11v
    802.1X
    AirConsole
    Aruba
    ArubaOS
    AutoCAD
    Automation
    Brand
    Capture
    Cisco
    Cisco WLC
    CLI
    Cloud
    Co-Channel Contention
    Controller Upgrade
    Course
    CWNE
    CWNP
    Design
    DFS
    Diagrams
    Ekahau
    FreeRADIUS
    Frequencies
    FSPL
    Hotspot
    Ideas
    Industry Canada
    Interference
    ISED
    Jetson Nano
    KRACK
    London
    MagicQuadrant
    MakeWi FiVisible
    MakeWi-FiVisible
    Market
    Meetup
    Mobility Express
    News
    Packet Analysis
    Programming
    Python
    Reference Guide
    Script
    Security
    SemFio
    Site-survey
    Site-survey
    Spectrum-analysis
    Technology
    Timeline
    Tip
    Training
    Validation
    Video
    Warehouse
    WiFi
    Wi-FI
    Wi-Fi
    Wi Fi 6
    Wi-Fi 6
    Wifitraining
    Wi Fi Troubleshooting
    Wi-Fi Troubleshooting
    Wireshark
    WLAN Pi
    WLPC
    WPA2

    Archives

    July 2020
    March 2020
    February 2020
    January 2020
    December 2019
    October 2019
    August 2019
    July 2019
    June 2019
    April 2019
    January 2019
    December 2018
    November 2018
    August 2018
    May 2018
    April 2018
    March 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    May 2017
    April 2017
    February 2017
    January 2017
    November 2016
    September 2016
    August 2016
    May 2016
    April 2016
    March 2016
    February 2016
    December 2015
    November 2015
    September 2015
    August 2015
    July 2015
    April 2015
    February 2015
    January 2015
    December 2014
    November 2014
    October 2014
    September 2014
    August 2014
    June 2014
    May 2014

    RSS Feed

SemFioNetworks-EmailContact SemFioNetworks-LinkedInProfile SemFioNetworks-YoutubeChannel

Let's Talk

Please get in touch with us if you have any questions. We offer a wide variety of professional Wi-Fi services that can help your wireless environment becoming faster, more secure and more efficient!
Get in touch
SemFioNetworks-Logo
Copyright © 2020 by SemFio Networks Inc.