blob: 3c70cb0038e194b40cd851ae7ccd05ca2edc5760 [file] [log] [blame]
Guido van Rossum260cc1a81992-08-10 10:48:14 +00001# 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
16import sys, os, time
17from socket import *
18import thread
19
20BUFSIZE = 8*1024
21
22# Telnet protocol characters
23
Tim Peters172c40b2001-01-21 07:07:30 +000024IAC = chr(255) # Interpret as command
Guido van Rossum260cc1a81992-08-10 10:48:14 +000025DONT = chr(254)
26DO = chr(253)
27WONT = chr(252)
28WILL = chr(251)
29
30def main():
Tim Peters172c40b2001-01-21 07:07:30 +000031 if len(sys.argv) < 2:
32 sys.stderr.write('usage: telnet hostname [port]\n')
33 sys.exit(2)
34 host = sys.argv[1]
35 try:
36 hostaddr = gethostbyname(host)
37 except error:
38 sys.stderr.write(sys.argv[1] + ': bad host name\n')
39 sys.exit(2)
40 #
41 if len(sys.argv) > 2:
42 servname = sys.argv[2]
43 else:
44 servname = 'telnet'
45 #
46 if '0' <= servname[:1] <= '9':
47 port = eval(servname)
48 else:
49 try:
50 port = getservbyname(servname, 'tcp')
51 except error:
52 sys.stderr.write(servname + ': bad tcp service name\n')
53 sys.exit(2)
54 #
55 s = socket(AF_INET, SOCK_STREAM)
56 #
57 try:
58 s.connect((host, port))
59 except error, msg:
60 sys.stderr.write('connect failed: ' + `msg` + '\n')
61 sys.exit(1)
62 #
63 thread.start_new(child, (s,))
64 parent(s)
Guido van Rossum260cc1a81992-08-10 10:48:14 +000065
66def parent(s):
Tim Peters172c40b2001-01-21 07:07:30 +000067 # read socket, write stdout
68 iac = 0 # Interpret next char as command
69 opt = '' # Interpret next char as option
70 while 1:
71 data, dummy = s.recvfrom(BUFSIZE)
72 if not data:
73 # EOF -- exit
74 sys.stderr.write( '(Closed by remote host)\n')
75 sys.exit(1)
76 cleandata = ''
77 for c in data:
78 if opt:
79 print ord(c)
80## print '(replying: ' + `opt+c` + ')'
81 s.send(opt + c)
82 opt = ''
83 elif iac:
84 iac = 0
85 if c == IAC:
86 cleandata = cleandata + c
87 elif c in (DO, DONT):
88 if c == DO: print '(DO)',
89 else: print '(DONT)',
90 opt = IAC + WONT
91 elif c in (WILL, WONT):
92 if c == WILL: print '(WILL)',
93 else: print '(WONT)',
94 opt = IAC + DONT
95 else:
96 print '(command)', ord(c)
97 elif c == IAC:
98 iac = 1
99 print '(IAC)',
100 else:
101 cleandata = cleandata + c
102 sys.stdout.write(cleandata)
103 sys.stdout.flush()
104## print 'Out:', `cleandata`
Guido van Rossum260cc1a81992-08-10 10:48:14 +0000105
106def child(s):
Tim Peters172c40b2001-01-21 07:07:30 +0000107 # read stdin, write socket
108 while 1:
109 line = sys.stdin.readline()
110## print 'Got:', `line`
111 if not line: break
112 s.send(line)
Guido van Rossum260cc1a81992-08-10 10:48:14 +0000113
114main()