Guido van Rossum | 260cc1a8 | 1992-08-10 10:48:14 +0000 | [diff] [blame] | 1 | # Minimal interface to the Internet telnet protocol. |
| 2 | # |
| 3 | # *** modified to use threads *** |
| 4 | # |
| 5 | # It refuses all telnet options and does not recognize any of the other |
| 6 | # telnet commands, but can still be used to connect in line-by-line mode. |
| 7 | # It's also useful to play with a number of other services, |
| 8 | # like time, finger, smtp and even ftp. |
| 9 | # |
| 10 | # Usage: telnet host [port] |
| 11 | # |
| 12 | # The port may be a service name or a decimal port number; |
| 13 | # it defaults to 'telnet'. |
| 14 | |
| 15 | |
| 16 | import sys, os, time |
| 17 | from socket import * |
| 18 | import thread |
| 19 | |
| 20 | BUFSIZE = 8*1024 |
| 21 | |
| 22 | # Telnet protocol characters |
| 23 | |
| 24 | IAC = chr(255) # Interpret as command |
| 25 | DONT = chr(254) |
| 26 | DO = chr(253) |
| 27 | WONT = chr(252) |
| 28 | WILL = chr(251) |
| 29 | |
| 30 | def main(): |
| 31 | host = sys.argv[1] |
| 32 | try: |
| 33 | hostaddr = gethostbyname(host) |
| 34 | except error: |
| 35 | sys.stderr.write(sys.argv[1] + ': bad host name\n') |
| 36 | sys.exit(2) |
| 37 | # |
| 38 | if len(sys.argv) > 2: |
| 39 | servname = sys.argv[2] |
| 40 | else: |
| 41 | servname = 'telnet' |
| 42 | # |
| 43 | if '0' <= servname[:1] <= '9': |
| 44 | port = eval(servname) |
| 45 | else: |
| 46 | try: |
| 47 | port = getservbyname(servname, 'tcp') |
| 48 | except error: |
| 49 | sys.stderr.write(servname + ': bad tcp service name\n') |
| 50 | sys.exit(2) |
| 51 | # |
| 52 | s = socket(AF_INET, SOCK_STREAM) |
| 53 | # |
| 54 | try: |
| 55 | s.connect(host, port) |
| 56 | except error, msg: |
| 57 | sys.stderr.write('connect failed: ' + `msg` + '\n') |
| 58 | sys.exit(1) |
| 59 | # |
| 60 | thread.start_new(child, (s,)) |
| 61 | parent(s) |
| 62 | |
| 63 | def parent(s): |
| 64 | # read socket, write stdout |
| 65 | iac = 0 # Interpret next char as command |
| 66 | opt = '' # Interpret next char as option |
| 67 | while 1: |
| 68 | data, dummy = s.recvfrom(BUFSIZE) |
| 69 | if not data: |
| 70 | # EOF -- exit |
| 71 | sys.stderr.write( '(Closed by remote host)\n') |
| 72 | thread.exit_prog(1) |
| 73 | cleandata = '' |
| 74 | for c in data: |
| 75 | if opt: |
| 76 | print ord(c) |
| 77 | print '(replying: ' + `opt+c` + ')' |
| 78 | s.send(opt + c) |
| 79 | opt = '' |
| 80 | elif iac: |
| 81 | iac = 0 |
| 82 | if c == IAC: |
| 83 | cleandata = cleandata + c |
| 84 | elif c in (DO, DONT): |
| 85 | if c == DO: print '(DO)', |
| 86 | else: print '(DONT)', |
| 87 | opt = IAC + WONT |
| 88 | elif c in (WILL, WONT): |
| 89 | if c == WILL: print '(WILL)', |
| 90 | else: print '(WONT)', |
| 91 | opt = IAC + DONT |
| 92 | else: |
| 93 | print '(command)', ord(c) |
| 94 | elif c == IAC: |
| 95 | iac = 1 |
| 96 | print '(IAC)', |
| 97 | else: |
| 98 | cleandata = cleandata + c |
| 99 | sys.stdout.write(cleandata) |
| 100 | sys.stdout.flush() |
| 101 | ## print 'Out:', `cleandata` |
| 102 | |
| 103 | def child(s): |
| 104 | # read stdin, write socket |
| 105 | while 1: |
| 106 | line = sys.stdin.readline() |
| 107 | ## print 'Got:', `line` |
| 108 | s.send(line) |
| 109 | |
| 110 | main() |