[Twisted-Python] telnet client code

Jp Calderone exarkun at intarweb.us
Thu May 8 13:32:29 MDT 2003


On Thu, May 08, 2003 at 12:23:18PM -0400, Itamar Shtull-Trauring wrote:
> On 08 May 2003 10:10:58 +0200
> Thomas Heller <theller at python.net> wrote:
> 
> > I have this snippet running in it's own thread, and would like
> > to port it to twisted, my program is using it anyway.
> > Any hints how to get started?
> 
> I'm not sure if telnet is symmetric - if it isn't, looks like we don't
> have telnet client code. Even if it is, the current code needs work in
> order to be usable for what you want (it's one of the more ancient parts
> of Twisted.)

  Telnet is mostly symmetric.  The existing code isn't very client-friendly,
though.   Here's a kinda-sorta example (untested):


    from twisted.protocols import telnet
    from twisted.internet import protocol, reactor

    class RebootTheServer(telnet.Telnet):
        mode = 'WaitForUser'

        def connectionMade(self):
            # Only defined to keep parent from sending the welcome banner
            pass

        def telnet_WaitForUser(self, line):
            if line.startswith('Username: '):
                self.write(self.factory.username + '\r\n')
                return 'WaitForPassword'

        def telnet_WaitForPassword(self, line):
            if line.startswith('Password: '):
                self.write(self.factory.password + '\r\n')

                # Instead of this, you could do another state,
                # like "WaitForPrompt" or something.
                reactor.callLater(1, self.reboot)
                return 'Idling'

        def telnet_Idling(self, line):
            # La la la.
            pass 

        def reboot(self):
            self.write('reboot\r\n')

            # This will make us lose our connection after the next
            # line is received.  To lose it faster, use
            # self.transport.loseConnection() instead
            self.mode = 'Done'

        def connectionLost(self, reason):
            if self.mode != 'Done':
                reason.printTraceback()
            reactor.stop()
           

    f = protocol.ClientFactory()
    f.protocol = RebootTheServer
    f.password = 'password'
    f.username = 'username'

    # fill in host and port
    reactor.connectTCP(host, port, f)

    # Let's go
    reactor.run()

  This is the first expect-y thing I've written with Telnet, so maybe it's
not the best solution, but hopefully it gets you on the right track.

  Jp

-- 
#!/bin/bash
( LIST=(~/.sigs/*.sig)
  cat ${LIST[$(($RANDOM % ${#LIST[*]}))]}
  echo -- $'\n' `uptime | sed -e 's/.*m//'` ) > ~/.signature
-- 
 up 6 days, 16:38, 10 users, load average: 0.07, 0.06, 0.00
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20030508/ace1e005/attachment.sig>


More information about the Twisted-Python mailing list