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...