Telnet and python, and the legacy of DOS   Leave a comment

I’ve been mucking around a lot with SSH and SCP lately, and because of that I’ve found myself logging into the wireless router regularly to check the IP adress of my computers. My router happens to support telnet, so I began using that instead of wasting time with the web browser. Of course, this is just the sort of task that is asking to be automated. So, a quick google, and I found http://docs.python.org/library/telnetlib.html.

So I sat down, and wrote some code:
#!/usr/bin/env python
#List hosts connected to router
import os, sys
import telnetlib
CMD = "dhcpserver status"
USERNAME, PASSWORD = "admin", "******"
HOST, PORT = "192.168.1.254", 23
tn = telnetlib.Telnet(HOST, PORT)
tn.read_until("Login:")
tn.write(USERNAME + "\n")
tn.read_until("Password:")
tn.write(PASSWORD + "\n")
tn.read_until("admin>")
tn.write(CMD + "\n")
print tn.read_until("admin>")

This is pretty much just  a small modification of the example given on the telnetlib page, and for some reason, it didn’t actually work. After a couple minutes, I realised why. My router happens to use “\r\n” as it’s newline character, so a simple “\n” wasn’t actually pushing the username and password through. After adding “\r” where necessary, it worked quite nicely. Here’s the final code, and an example of it’s usage.

#!/usr/bin/env python
#Print output of command run on router
import os,sys
import telnetlib
CMD = " ".join(sys.argv[1:])
USERNAME, PASSWORD = "admin", "******"
HOST, PORT = "192.168.1.254", 23
tn = telnetlib.Telnet(HOST, PORT)
tn.read_until(":")
tn.write(USERNAME + "\r\n")
tn.read_until(":")
tn.write(PASSWORD + "\r\n")
tn.read_until(">")
tn.write(CMD + "\r\n")
print tn.read_until(">")


xavieran@tranquility:~/Code$ ./info.py dhcpserver status
DHCP Server Lease Status
Interface 'QS_PPPoE'
IP address | Client UID/hw addr | Client Host Name | Expiry
----------------+---------------------+------------------+---------------
No network topology given - unconfigured
------------------------------------------------------------------------
Interface 'iplan'
IP address | Client UID/hw addr | Client Host Name | Expiry
----------------+---------------------+------------------+---------------
192.168.1.4 | 00:1b:77:59:e6:2e | Tranquility | 10 hours
192.168.1.2 | 00:1c:df:08:16:2b | Le-Chateau | 6 hours
192.168.1.7 | 00:15:af:80:ca:8d | aiesha-laptop | Expired
192.168.1.6 | 01:00:0e:35:ab:97:61| NB0928 | Expired
192.168.1.5 | 01:00:11:25:44:97:c2| NB0928 | Expired
192.168.1.1 | 01:00:26:c6:72:dd:a0| R8WNVML | Expired
192.168.1.3 | 01:00:27:13:68:08:fc| R8WNVML | Expired

Advertisements

Posted 21/03/2010 by Emmanuel Jacyna in Code, Python

Tagged with , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: