using cmd.exe as a telnet client

Donnal Walter donnal at donnal.net
Tue Dec 7 13:09:04 EST 2004


Jp Calderone wrote:
> The iac_FOO method will be called whenever the telnet command FOO is received with the command's "argument" (the byte following it) as its only argument.  When a subnegotiation is received, iacSBchunk is called.
> 
> That's the 1.3 API, anyway.  It will still exist in 2.0, but it is lookingly increasingly likely that the new version will be ready in time for 2.0.  With the new API, the enableLocal() method is invoked when an option negotiation initiated by this side of the connection is allowed by the remote side, and enableRemote() is invoked when the remote side has requested an option be enabled (it can return true or false to allow or deny the enabling).  Similar disableLocal() and disableRemote() methods exist.  Callbacks can be registered for subnegotiation, based on the command for which the subnegotiation is negotiating options.  There are also do() and dont() methods that return Deferreds.  There's a whole bunch of other new cool features too, but I've probably gone on long enough, especially for a module that hasn't even been released :)

Thanks. When 2.0 comes out, I will take definitely a look. In the 
meantime I was determined to figure out how to make telnetlib work. 
(After all, Grant said is would be easy. :-) ) After much trial and 
error, I found that the following function works for me.

import telnetlib as tnl
HOST = 'nnn.nnn.nnn.nnn'
PORT = 23

def negotiate(sock, cmd, opt):
     if cmd == tnl.DO and opt == tnl.SNDLOC:
         sock.sendall(tnl.IAC + tnl.WONT + opt)
     elif cmd == tnl.DO and opt == tnl.BINARY:
         sock.sendall(tnl.IAC + tnl.WILL + opt)
     elif cmd == tnl.DO and opt == tnl.ECHO:
         sock.sendall(tnl.IAC + tnl.WILL + opt)
     elif cmd == tnl.DO and opt == tnl.SGA:
         sock.sendall(tnl.IAC + tnl.WILL + opt)
     elif cmd == tnl.WILL and opt == tnl.BINARY:
         sock.sendall(tnl.IAC + tnl.DO + opt)
     elif cmd == tnl.WILL and opt == tnl.ECHO:
         sock.sendall(tnl.IAC + tnl.DO + opt)
     elif cmd == tnl.WILL and opt == tnl.SGA:
         sock.sendall(tnl.IAC + tnl.DO + opt)

     elif cmd in (tnl.DO, tnl.DONT):
         sock.sendall(tnl.IAC + tnl.WONT + opt)
     elif cmd in (tnl.WILL, tnl.WONT):
         sock.sendall(tnl.IAC + tnl.DONT + opt)
     return


mytn = tnl.Telnet(HOST, PORT)
mytn.set_debuglevel(2)
mytn.set_option_negotiation_callback(negotiate)
mytn.read_until("Enter device name?")


This function is currently a bit redundant, and I have not yet commented 
out each option to see what I can get away with and what not, but at 
least I can now connect without "Connection reset by peer". Thanks to 
all who replied for your advice and encouragement.

Donnal Walter
Arkansas Children's Hospital





More information about the Python-list mailing list