Introduction
Python is one of the most accessible programming languages, it simplified syntax, which gives more emphasis towards Human understanding, bit like English Language. Due to its ease of learning and usage, python codes can be easily written and executed much faster than other programming languages.
Python developers are highly versatile and can bring value to a vast number of projects.
Spotify, Nasa, Reddit, Google, Facebook, YouTube, Instagram. That’s right: they all use Python.
Python is everywhere as it is best in data handling, you may not even realize how widespread it is.
A Python module is a file containing Python definitions and statements.
we can consider a module to be the same as a code library or a file that contains a set of functions that you want to include in your application. Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.
When you’re looking to build something, you want to be sure you have the right tools, here we are showing you how using Telnet Library we can telnet to the cisco device and can have basic configuration on devices through Python Script.
Strings in python are surrounded by either single quotation marks, or double quotation marks. Variable assignation is done with the variable name (welcome) followed by an equal sign and the string:
welcome = “This is Welcoming message, which is a string, stored in variable”
In Python 2, both string and byte strings are same type, whereas in Python 3 Byte objects, defined as “sequence of bytes” and similar to “unicode” objects from Python 2.
Conversion from strings to byte code can be done using “encode() method”.
Telnet Library Code for Device Accessibility and Configuring Loopback
- import getpass
- import telnetlib
- device = “Device_IP”
- user = input (“Enter your username: “)
- password = getpass.getpass()
- tn = telnetlib.Telnet(device)
- tn.read_until(b”Username: “)
- tn.write(user.encode(‘ascii’) + b”\n”)
- tn.read_until(b”Password: “)
- tn.write(password.encode(‘ascii’) + b”\n”)
- print (“Telnet successful to the device!”)
- tn.write(b”conf ter\n”)
- tn.write(b”int loop 0 \n”)
- tn.write(b”ip address 1.1.1.1 255.255.255.255 \n”)
- tn.write(b”no shut\n”)
- tn.write(b”description “Using Python”\n”)
- tn.write(b”end\n”)
- tn.write(b”exit\n”)
- print (“Commands are pushed – below is the detail :”)
- print(tn.read_all().decode(‘ascii’))
- print (“Thanks for using Python Script”)]
Telnet Library Code Explanation
Line 1, 2: When there is need to import non-default libraries (modules) into our code, we can use import command followed with the module/library name.
Line 3: Assigning device IP manually or statically into the device variable (Variable are just like containers).
Line 4: Asking for the username from the user through input method.
Line 5: There is a secrecy to get the password and it shouldn’t be visible to anyone that is a reason instead of input, leveraging getpass here.
Line6: Instead of using complete line create tn variable to assigning a long value and telnetting to device.
Line 7,8: Reading till username is prompted and issuing username. In python “\n” represents to the new line character. Similarly in Line 9,10 are representing the password issuing.
Line 11: It is using a print function just to display the message for users after accessing the device successfully.
Line 12,18: In these lines we are using cisco commands to configure the devices.
Line 19,21: Here we are again printing messages before users so that they are aware what happened by Python and a good bye message.
Telnet Library Code Execution in NetMinion LAB
Actual program snapshot through visual studio code is as follows which we are running through the windows management PC as shown in above diagram and will access the router3.
Below is the command execution results –
Congrats! You now have learnt your first program how to access and configure the device through Python without investing a single penny that will certainly help for your future programs. In addition to this post please check out our more blog post on F5 Big-IP’s Application Security Manager and for other topics.
You may also see the video related to it on our YouTube Channel. Click Here to watch
We also offer a diverse library of pre-recorded videos for any online training or buy self-paced courses.
“Get enrolled now”.
Email:info@netminion.net, netminionsolutions@gmail.com
Helpline: +91-9599857762(IN), + 19024124779 (CA)
LinkedIn: https://www.linkedin.com/in/netminion-solutions/
Website: https://netminion.net
Videos Website: https://videos.netminion.in
Telegram Channel: https://t.me/NetMinionSolitionsOffical
Buy a Rack Rental : https://labs.netminion.net/page/login/index.php
Keep Learning! Keep Growing! Keep investing!
Welcome to NetMinion Solutions, a leading education training institute/company to nurture minds and fostering a passion for learning. No matter if you are a beginner or a professional – our dedicated faculty and state-of-the-art facilities create an enriching environment where you can explore, innovate, and grow exponentially – academically and personally both.
We are committed to practical learning and provide cutting-edge lab solutions, to enhance your learning journey – including CCNA, CCNP & CCIE, data center, Wireless, Cloud, VMware, F5 -LTM, GTM, ASM, APM, Palo Alto, SD-WAN, Checkpoint, ACI and list goes on.