3
# Minimal interface to the Internet telnet protocol.
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.
10
# Usage: telnet host [port]
12
# The port may be a service name or a decimal port number;
13
# it defaults to 'telnet'.
16
import sys, posix, time
21
# Telnet protocol characters
23
IAC = chr(255) # Interpret as command
32
hostaddr = gethostbyname(host)
34
sys.stderr.write(sys.argv[1] + ': bad host name\n')
38
servname = sys.argv[2]
42
if '0' <= servname[:1] <= '9':
46
port = getservbyname(servname, 'tcp')
48
sys.stderr.write(servname + ': bad tcp service name\n')
51
s = socket(AF_INET, SOCK_STREAM)
54
s.connect((host, port))
56
sys.stderr.write('connect failed: ' + repr(msg) + '\n')
62
# child -- read stdin, write socket
64
line = sys.stdin.readline()
67
# parent -- read socket, write stdout
68
iac = 0 # Interpret next char as command
69
opt = '' # Interpret next char as option
71
data = s.recv(BUFSIZE)
73
# EOF; kill child and exit
74
sys.stderr.write( '(Closed by remote host)\n')
86
cleandata = cleandata + c
88
if c == DO: print '(DO)',
91
elif c in (WILL, WONT):
92
if c == WILL: print '(WILL)',
96
print '(command)', ord(c)
101
cleandata = cleandata + c
102
sys.stdout.write(cleandata)
108
except KeyboardInterrupt: