Home > Connect Python to GNS3 for Automation in Win10

Connect Python to GNS3 for Automation in Win10

July 26th, 2019 in Knowledge Go to comments

4. Python (version 3) code to telnet to R1

Next we create Python code to telnet to our R1 router, login with our provided username and password. It will also enter privileged mode (Router#) with “cisco” as the secret password. Then create a loopback interface 1 with IP address of 1.1.1.1/32 on our router:

import getpass
import telnetlib

host = "10.1.1.1"
user = input("Enter Username:")
password = getpass.getpass()
print('Successfully passed getpass')
tn = telnetlib.Telnet(host)
print('Successfully passed telnet')
tn.read_until(b"Username:")
tn.write(user.encode("ascii") + b"\n")

if password:
    tn.read_until(b"Password:")
    tn.write(password.encode("ascii")+b"\n")

    tn.write(b"en \n")
    tn.write(b"cisco\n")
    tn.write(b"conf t\n")
    tn.write(b"int loopback 1\n")
    tn.write(b"ip add 1.1.1.1 255.255.255.255\n")
    tn.write(b"end\n")
    tn.write(b"exit\n")
    print(tn.read_all().decode("ascii"))

The source code can also be downloaded here.

Note: The letter “b” in front of the string (for example in line: tn.read_until(b”Username:”)) indicates that the literal should become a bytes literal in Python 3. Bytes literals are always prefixed with ‘b’ or ‘B’; they produce an instance of the bytes type instead of the str type.

Save this code to the “telnet.py” file to any location you want (we saved it in “C:/Users/PC/PycharmProjects/Python_Telnet/telnet.py”). Then open command line and run this file as follows:

Win10_cmd_Run_python_script.jpg

Of course you have to change the correct path to your Python file. Notice that we are using Python version 3 to run this file. If we are lucky, we will get a nice result like this:

Win10_cmd_Run_python_script_2.jpg

Note: If you use some Python IDEs (like PyCharm) to run this file then the “Password: ” prompt will not be shown so please make sure to run it under “cmd”.

Now on R1 issue the “show ip interface brief” command to see our new Loopback 1 interface has been created:

R1_check_new_interface_added.jpg

5. Some errors you may see during the configuration

The hardest error you may see in this lab is the telnet connection to R1 does not work with the following error:

C:\Users\PC>python C:/Users/PC/PycharmProjects/Python_Telnet/telnet.py
Enter Username:R1
Password: (type “12345” here)
Successfully passed getpass
Traceback (most recent call last):
  File “C:/Users/PC/PycharmProjects/Python_Telnet/telnet.py”, line 8, in <module>
    tn = telnetlib.Telnet(host)
  File “C:\Program Files (x86)\Python37-32\lib\telnetlib.py”, line 218, in __init__
    self.open(host, port, timeout)
  File “C:\Program Files (x86)\Python37-32\lib\telnetlib.py”, line 234, in open
    self.sock = socket.create_connection((host, port), timeout)
  File “C:\Program Files (x86)\Python37-32\lib\socket.py”, line 727, in create_connection
    raise err
  File “C:\Program Files (x86)\Python37-32\lib\socket.py”, line 716, in create_connection
    sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

To solve this problem we recommend you to:
+ Check step 3 – Verify the connection between our laptop and router in GNS3 carefully to make sure two devices can communicate normally
+ Try to run command line in Administrator mode (Search > Cmd > Right-click and choose “Run as administrator”)
+ Disable any firewall, antivirus software during the lab
+ Check the IP address of “host” in “telnet.py” file carefully (it should be “host = “10.1.1.1” in our lab)

Another problem we may encounter is after typing “Username: R1” and waiting for the “Password” prompt. The program is suspended:

PyCharm_GNS3_telnet_error.jpg

As we said above, if we run this Python file through some Python IDEs (like PyCharm), the “Password” function of “getpass.getpass()” will not interpreted correctly and interrupted.

Comments (2) Comments
  1. GreggSnume
    October 9th, 2019

    Хорошая статья

  2. Karol Garrido
    November 4th, 2022

    Where are you?