blob: ee7c43b87423084fe8d919a2af105976887de552 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum22825e81991-07-01 18:32:32 +00002
3# Minimal interface to the Internet telnet protocol.
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, posix, time
17from socket import *
18
19BUFSIZE = 1024
20
21# Telnet protocol characters
22
23IAC = chr(255) # Interpret as command
24DONT = chr(254)
25DO = chr(253)
26WONT = chr(252)
27WILL = chr(251)
28
29def main():
30 host = sys.argv[1]
31 try:
32 hostaddr = gethostbyname(host)
33 except error:
34 sys.stderr.write(sys.argv[1] + ': bad host name\n')
35 sys.exit(2)
36 #
37 if len(sys.argv) > 2:
38 servname = sys.argv[2]
39 else:
40 servname = 'telnet'
41 #
42 if '0' <= servname[:1] <= '9':
43 port = eval(servname)
44 else:
45 try:
46 port = getservbyname(servname, 'tcp')
47 except error:
48 sys.stderr.write(servname + ': bad tcp service name\n')
49 sys.exit(2)
50 #
51 s = socket(AF_INET, SOCK_STREAM)
52 #
53 try:
Jeremy Hyltona8d30d52000-08-25 15:38:41 +000054 s.connect((host, port))
Guido van Rossum22825e81991-07-01 18:32:32 +000055 except error, msg:
56 sys.stderr.write('connect failed: ' + `msg` + '\n')
57 sys.exit(1)
58 #
59 pid = posix.fork()
60 #
Guido van Rossumf628a981992-05-19 13:51:20 +000061 if pid == 0:
Guido van Rossum22825e81991-07-01 18:32:32 +000062 # child -- read stdin, write socket
63 while 1:
64 line = sys.stdin.readline()
65 s.send(line)
66 else:
67 # parent -- read socket, write stdout
68 iac = 0 # Interpret next char as command
69 opt = '' # Interpret next char as option
70 while 1:
71 data = s.recv(BUFSIZE)
72 if not data:
73 # EOF; kill child and exit
74 sys.stderr.write( '(Closed by remote host)\n')
75 posix.kill(pid, 9)
76 sys.exit(1)
77 cleandata = ''
78 for c in data:
79 if opt:
80 print ord(c)
81 s.send(opt + c)
82 opt = ''
83 elif iac:
84 iac = 0
Guido van Rossumf628a981992-05-19 13:51:20 +000085 if c == IAC:
Guido van Rossum22825e81991-07-01 18:32:32 +000086 cleandata = cleandata + c
87 elif c in (DO, DONT):
Guido van Rossumf628a981992-05-19 13:51:20 +000088 if c == DO: print '(DO)',
Guido van Rossum22825e81991-07-01 18:32:32 +000089 else: print '(DONT)',
90 opt = IAC + WONT
91 elif c in (WILL, WONT):
Guido van Rossumf628a981992-05-19 13:51:20 +000092 if c == WILL: print '(WILL)',
Guido van Rossum22825e81991-07-01 18:32:32 +000093 else: print '(WONT)',
94 opt = IAC + DONT
95 else:
96 print '(command)', ord(c)
Guido van Rossumf628a981992-05-19 13:51:20 +000097 elif c == IAC:
Guido van Rossum22825e81991-07-01 18:32:32 +000098 iac = 1
99 print '(IAC)',
100 else:
101 cleandata = cleandata + c
102 sys.stdout.write(cleandata)
103 sys.stdout.flush()
104
105
106try:
107 main()
108except KeyboardInterrupt:
109 pass