Here’s a very simple, crude, yet easy to understand Python telnet client. I made this because I often need to access some old-fashioned telnet-only services — some eagle BBS services to be precise — at work. Unfortunately, the telnet port is blocked by the company’s firewall, and therefore I have no choice but using a remote UNIX server as a proxy via SSH. The server, however, does not provide the traditional telnet command for the security reasons, so I created this script to circumvent the restriction (the server has Python anyway.. interesting).
Please note that, because of its primary purpose, this script is somewhat geared toward the eagle BBS compromising some usability as a general purpose telnet client although this issue can be cured easily (see the description below). It works with Python 1.5+. OK, let’s look around the source code first (or click here to download). Here we go:
- #!/home/bin/python
- #
- # Synopsis: terminal-based python telnet client
- # Usage: ptelnet.py [hostname] [portnumber]
- #
- # Programmed by Gang Seong Lee
- # Revised by Ha Hong (a.k.a. RedRiver)
- from telnetlib import Telnet
- import time, sys
- from threading import *
- import sys, tty, termios
- host = 'localhost';
- port = 23
- class ReaderThread(Thread):
- def __init__(self, telnet):
- self.telnet = telnet
- Thread.__init__(self)
- def run(self):
- while 1:
- str = self.telnet.read_some()
- if str == '': break
- sys.stdout.write(str)
- sys.stdout.flush()
- def main(host, port):
- telnet = Telnet()
- telnet.open(host, port)
- reader = ReaderThread(telnet)
- reader.start()
- fd = sys.stdin.fileno()
- old_settings = termios.tcgetattr(fd)
- tty.setraw(fd)
- while 1:
- if not reader.isAlive(): break
- # org: line = raw_input()
- # org: telnet.write(line+'n')
- ch = sys.stdin.read(1)
- telnet.write(ch)
- telnet.close()
- #termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
- termios.tcsetattr(fd, 1, old_settings)
- if __name__ == '__main__':
- try:
- host = sys.argv[1]
- except: pass
- try:
- port = int(sys.argv[2])
- except: pass
- main(host, port)
The usage of the command is quite simple:
$ python ptelnet.py [hostname] [port number]
Or, if you give the executable permission with proper path replacement of the first line, this would be like this:
$ ./ptelnet.py [hostname] [port number]
Of course, here’s some humble descriptions matching the humble code: the script is essentially composed of two parts — the reader thread part and key stroke handling part. The reader thread runs in the background and prints whatever received in the telnet session to the standard output. The key stroke handling part sends whatever entered to the telnet session without local echoing. This non-echoing is perfect for eagle BBS services, but if you consider using this script for general telnet servers, you may want to revert the key handling part (line 40–48) to the original one as the followings which echoes locally: (but there is one stupid drawback: it echoes your password.)
- while 1:
- if not reader.isAlive(): break
- line = raw_input()
- telnet.write(line+'n')
- telnet.close()
Known issues: the script has some minor problems. First, it does not understand the telnet terminal control handshaking at all. Second, the TERMIOS and stdin.read() thing which is essentially the getch() is not perfectly portable. You may have to mend it especially in order to run under Windows. Third, if the script terminates unexpectedly your terminal could be messed up. Issue the reset command in this case.
May 30th, 2010 at 7:57 am
Hello! I would really really want to download your ptelnet.py but I’m forbidden from downloading it! :(
Please, send it to my mail? :)
June 3rd, 2010 at 3:23 pm
Thanks for your comments. I’ve fixed the security settings so that you can download the file now (and, I mailed you anyway). Hope that helps.
June 27th, 2010 at 7:54 pm
Thought I had a good deal with First National Bank Omaha. I had a (supposedly) 5.99% fixed lifetime rate on a balance transfer, and up jumped the boggy man. I have paid on-line for the last two years and when I received my last statement I now have a 28.99% apr. I have never missed a payment or been late and you see what these companies do to you and never blink an eye.





February 6th, 2009 at 5:27 pm
I am using python on Windows and I’m finding it difficult to substitute termios could you help me in this..
thank you