Executing a remote shell command using telnet

If you need to connect an old network switch or router via telnet, you can do so from a Python script instead of using a bash script or an interactive shell. This recipe will create a simple telnet session. It will show you how to execute shell commands to the remote host.

Getting ready

You need to install the telnet server on your machine and ensure that it's up and running. You can use a package manager that is specific to your operating system to install the telnet server package. For example, on Debian/Ubuntu, you can use apt-get or aptitude to install the telnetd package, as shown in the following command:

$ sudo apt-get install telnetd
$ telnet localhost

How to do it...

Let us define a function that will take a user's login credentials from the command prompt and connect to a telnet server.

Upon successful connection, it will send the Unix 'ls' command. Then, it will display the output of the command, for example, listing the contents of a directory.

Listing 7.1 shows the code for a telnet session that executes a Unix command remotely as follows:

#!/usr/bin/env python
# Python Network Programming Cookbook -- Chapter - 7
# This program is optimized for Python 2.7.
# It may run on any other version with/without modifications.

import getpass
import sys
import telnetlib
 
 
def run_telnet_session():
  host = raw_input("Enter remote hostname e.g. localhost:")
  user = raw_input("Enter your remote account: ")
  password = getpass.getpass()
  
  session = telnetlib.Telnet(host)
  
  session.read_until("login: ")
  session.write(user + "
")
  if password:
    session.read_until("Password: ")
    session.write(password + "
")
    
  session.write("ls
")
  session.write("exit
")
  
  print session.read_all()
 
if __name__ == '__main__':
  run_telnet_session()

If you run a telnet server on your local machine and run this code, it will ask you for your remote user account and password. The following output shows a telnet session executed on a Debian machine:

$ python 7_1_execute_remote_telnet_cmd.py 
Enter remote hostname e.g. localhost: localhost
Enter your remote account: faruq
Password: 
 
ls
exit
Last login: Mon Aug 12 10:37:10 BST 2013 from localhost on pts/9
Linux debian6 2.6.32-5-686 #1 SMP Mon Feb 25 01:04:36 UTC 2013 i686
 
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
 
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
You have new mail.
faruq@debian6:~$ ls       
down              Pictures               Videos
Downloads         projects               yEd
Dropbox           Public
env               readme.txt
faruq@debian6:~$ exit
logout

How it works...

This recipe relies on Python's built-in telnetlib networking library to create a telnet session. The run_telnet_session() function takes the username and password from the command prompt. The getpass module's getpass() function is used to get the password as this function won't let you see what is typed on the screen.

In order to create a telnet session, you need to instantiate a Telnet() class, which takes a hostname parameter to initialize. In this case, localhost is used as the hostname. You can use the argparse module to pass a hostname to this script.

The telnet session's remote output can be captured with the read_until() method. In the first case, the login prompt is detected using this method. Then, the username with a new line feed is sent to the remote machine by the write() method (in this case, the same machine accessed as if it's remote). Similarly, the password was supplied to the remote host.

Then, the ls command is sent to be executed. Finally, to disconnect from the remote host, the exit command is sent, and all session data received from the remote host is printed on screen using the read_all() method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.66