Steps to Build and Run the  Telnet Script in Python

I’m using a Debian-based Linux operating system here to carry out the demonstration. 

Step1:

Ensure that the telnet server is running in the host machine to which you are going to connect using the script. For simplicity, we are going to connect to localhost which is nothing but our own machine. You can check if a telnet server is running on your machine by typing the following command in the terminal:

telnet localhost

This command connects to the telnet server running at localhost. If you can manually connect to the server in this way, then your script will also be able to connect to it. 

Step 2:

Create a new python file with any name of your choice with a ‘.py’ extension. The location of this file is also arbitrary. The command to do this is given below:

touch <file_name.py>

Step 3:

Now it’s time to insert our script into this file. You can use a text editor of your choice for this purpose. I’m using a mousepad here.

mousepad <file_name>

Telnet Automation / Scripting Using Python

Telnet is the short form of Teletype network, which is a client/server application that works based on the telnet protocol. Telnet service is associated with the well-known port number – 23. As Python supports socket programming, we can implement telnet services as well. In this article, we will learn how to automate telnet login and command execution using a simple Python script.

Similar Reads

Telnet Automation/Scripting using Python

The main reason for using Python in Telnet Automation is that Python has a basic syntax when compared to other scripting languages which allow us to focus more on the logic of the program. It has a large community which makes troubleshooting, a piece of cake. Python provides a wide range of libraries that can be imported easily. Each library serves a different purpose. This feature of Python makes it suitable for multiple purposes which includes telnet automation....

Steps to Build and Run the  Telnet Script in Python

I’m using a Debian-based Linux operating system here to carry out the demonstration....

Code

Python3 #!/bin/python3 import telnetlib import getpass # Set the Telnet server address and port number host = "localhost" port = 23    # Set the username username = "gfg_user"    # Get password from the user print(f"Logging in as '{username}'") password = getpass.getpass()    # Set the command to execute command = "ls -l"    # Create a Telnet object tn = telnetlib.Telnet(host, port)    # Wait for the login prompt tn.read_until(b"login: ")    # Enter the username and wait for the password prompt tn.write(username.encode('ascii') + b"\n") tn.read_until(b"Password: ")    # Enter the password and wait for the shell prompt tn.write(password.encode('ascii') + b"\n") tn.read_until(b"$ ")    # Execute the command and wait for the output tn.write(command.encode('ascii') + b"\n") output = tn.read_until(b"$ ")    # Print the output print(output.decode('ascii'))    # Close the Telnet connection tn.close()...