[Twisted-Python] Sending commands when using Telnet client

Yes, I'm sort of a Twisted newbie, so maybe this has an obvious answer, but I'm just not seeing it.
I have a Twisted program that I need to add in the ability to make a client telnet connection to a remote server and send some commands to it and deal with the data received.
In my main program I have this line of code: reactor.connectTCP("mytelnethost", 24, Connections.TelnetConnection("myname of object",events,"command to run"))
My Connections.TelnetConnection class looks like this:
from twisted.internet.protocol import Protocol, ReconnectingClientFactory from twisted.conch.telnet import Telnet
class TelnetClient(Telnet): def connectionMade(self): print "connection made" self.write("\r\n")
def write(self, data): print data Telnet._write(self, data+"\r\n")
def dataReceived(self, data): print "received:", data if "User Name:" in data: self.write("user")
if "Password:" in data: self.write("password")
if ">" in data: time.sleep(2) self.write("connect") if "Connector Name:" in data: time.sleep(2) self.write("another command")
class TelnetConnection(Connection, ReconnectingClientFactory): """ Telnets to host:port and executes cmd. cmd """ protocol = TelnetClient def __init__(self, name, eventQueue, cmd=None): Connection.__init__(self, name)
def clientConnectionFailed(self, connector, reason): print 'connection failed:', reason.getErrorMessage()
def clientConnectionLost(self, connector, reason): print 'connection lost:', reason.getErrorMessage()
This all works great, except that the command sequence is hard coded in the TelnetClient class. I want to be able to reference the "cmd" parameter, which could be a list of commands to iterate over,etc. But I don't see how I can access the "cmd" parameter that gets passed into the TelnetConnection class from within the TelnetClient class.
I'm really desparate for some help, I've been racking my brain on this since yesterday morning.
Thanks,
Don

On Tue, 28 Feb 2006 10:59:50 -0800, Don Smith donwsmith@gmail.com wrote:
Yes, I'm sort of a Twisted newbie, so maybe this has an obvious answer, but I'm just not seeing it.
I have a Twisted program that I need to add in the ability to make a client telnet connection to a remote server and send some commands to it and deal with the data received.
[snip]
This all works great, except that the command sequence is hard coded in the TelnetClient class. I want to be able to reference the "cmd" parameter, which could be a list of commands to iterate over,etc. But I don't see how I can access the "cmd" parameter that gets passed into the TelnetConnection class from within the TelnetClient class.
I'm really desparate for some help, I've been racking my brain on this since yesterday morning.
The usual approach is to save the data (`cmd' in this case) on the factory and use `self.factory.stuff' from the protocol. The `factory' attribute is set on the protocol class by the default `buildProtocol' implementation. It is not available in the protocol's `__init__', but is available by the time `connectionMade' is called.
Jean-Paul

Thank you, thank you, thank you!!!
-Don
On 2/28/06, Jean-Paul Calderone exarkun@divmod.com wrote:
On Tue, 28 Feb 2006 10:59:50 -0800, Don Smith donwsmith@gmail.com wrote:
Yes, I'm sort of a Twisted newbie, so maybe this has an obvious answer,
but
I'm just not seeing it.
I have a Twisted program that I need to add in the ability to make a
client
telnet connection to a remote server and send some commands to it and
deal
with the data received.
[snip]
This all works great, except that the command sequence is hard coded in
the
TelnetClient class. I want to be able to reference the "cmd" parameter, which could be a list of commands to iterate over,etc. But I don't see
how I
can access the "cmd" parameter that gets passed into the TelnetConnection class from within the TelnetClient class.
I'm really desparate for some help, I've been racking my brain on this
since
yesterday morning.
The usual approach is to save the data (`cmd' in this case) on the factory and use `self.factory.stuff' from the protocol. The `factory' attribute is set on the protocol class by the default `buildProtocol' implementation. It is not available in the protocol's `__init__', but is available by the time `connectionMade' is called.
Jean-Paul
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

As you can see in twisted.internet.protocol.Factory
def buildProtocol(self, addr): """Create an instance of a subclass of Protocol.
The returned instance will handle input on an incoming server connection, and an attribute "factory" pointing to the creating factory.
Override this method to alter how Protocol instances get created.
@param addr: an object implementing L{twisted.internet.interfaces.IAddress} """ p = self.protocol() p.factory = self return p
ReconnectingClientFactory inherits that method, so each TelnetClient instance it builds will have a self.factory you can use to access your cmd list.
Hope that helps, -- alecu
On 2/28/06, Don Smith donwsmith@gmail.com wrote:
Yes, I'm sort of a Twisted newbie, so maybe this has an obvious answer, but I'm just not seeing it.
I have a Twisted program that I need to add in the ability to make a client telnet connection to a remote server and send some commands to it and deal with the data received.
In my main program I have this line of code: reactor.connectTCP("mytelnethost", 24, Connections.TelnetConnection("myname of object",events,"command to run"))
My Connections.TelnetConnection class looks like this:
from twisted.internet.protocol import Protocol, ReconnectingClientFactory from twisted.conch.telnet import Telnet
class TelnetClient(Telnet): def connectionMade(self): print "connection made" self.write("\r\n")
def write(self, data): print data Telnet._write(self, data+"\r\n") def dataReceived(self, data): print "received:", data if "User Name:" in data: self.write("user") if "Password:" in data: self.write("password") if ">" in data: time.sleep(2) self.write("connect") if "Connector Name:" in data: time.sleep(2) self.write("another command")
class TelnetConnection(Connection, ReconnectingClientFactory): """ Telnets to host:port and executes cmd. cmd """ protocol = TelnetClient def __init__(self, name, eventQueue, cmd=None): Connection.__init__(self, name)
def clientConnectionFailed(self, connector, reason): print 'connection failed:', reason.getErrorMessage() def clientConnectionLost(self, connector, reason): print 'connection lost:', reason.getErrorMessage()
This all works great, except that the command sequence is hard coded in the TelnetClient class. I want to be able to reference the "cmd" parameter, which could be a list of commands to iterate over,etc. But I don't see how I can access the "cmd" parameter that gets passed into the TelnetConnection class from within the TelnetClient class.
I'm really desparate for some help, I've been racking my brain on this since yesterday morning.
Thanks,
Don
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

So, I have this working now. I can send commands to the telnet server, dynamically. However, is there a way to supress the output of the data I get back so it doesn't flood my shell (stdout)?
Thanks,
Don
On 2/28/06, Alejandro J. Cura alecu@vortech.com.ar wrote:
As you can see in twisted.internet.protocol.Factory
def buildProtocol(self, addr): """Create an instance of a subclass of Protocol. The returned instance will handle input on an incoming server connection, and an attribute \"factory\" pointing to the creating factory. Override this method to alter how Protocol instances get created. @param addr: an object implementing
L{twisted.internet.interfaces.IAddress} """ p = self.protocol() p.factory = self return p
ReconnectingClientFactory inherits that method, so each TelnetClient instance it builds will have a self.factory you can use to access your cmd list.
Hope that helps,
alecu
On 2/28/06, Don Smith donwsmith@gmail.com wrote:
Yes, I'm sort of a Twisted newbie, so maybe this has an obvious answer,
but
I'm just not seeing it.
I have a Twisted program that I need to add in the ability to make a
client
telnet connection to a remote server and send some commands to it and
deal
with the data received.
In my main program I have this line of code: reactor.connectTCP("mytelnethost", 24, Connections.TelnetConnection("myname of object",events,"command to
run"))
My Connections.TelnetConnection class looks like this:
from twisted.internet.protocol import Protocol,
ReconnectingClientFactory
from twisted.conch.telnet import Telnet
class TelnetClient(Telnet): def connectionMade(self): print "connection made" self.write("\r\n")
def write(self, data): print data Telnet._write(self, data+"\r\n") def dataReceived(self, data): print "received:", data if "User Name:" in data: self.write("user") if "Password:" in data: self.write("password") if ">" in data: time.sleep(2) self.write("connect") if "Connector Name:" in data: time.sleep(2) self.write("another command")
class TelnetConnection(Connection, ReconnectingClientFactory): """ Telnets to host:port and executes cmd. cmd """ protocol = TelnetClient def __init__(self, name, eventQueue, cmd=None): Connection.__init__(self, name)
def clientConnectionFailed(self, connector, reason): print 'connection failed:', reason.getErrorMessage() def clientConnectionLost(self, connector, reason): print 'connection lost:', reason.getErrorMessage()
This all works great, except that the command sequence is hard coded in
the
TelnetClient class. I want to be able to reference the "cmd" parameter, which could be a list of commands to iterate over,etc. But I don't see
how I
can access the "cmd" parameter that gets passed into the
TelnetConnection
class from within the TelnetClient class.
I'm really desparate for some help, I've been racking my brain on this
since
yesterday morning.
Thanks,
Don
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

On Tue, 28 Feb 2006 17:31:46 -0800, Don Smith donwsmith@gmail.com wrote:
On 2/28/06, Alejandro J. Cura alecu@vortech.com.ar wrote:
On 2/28/06, Don Smith donwsmith@gmail.com wrote:
Yes, I'm sort of a Twisted newbie, so maybe this has an obvious answer,
but
I'm just not seeing it.
I have a Twisted program that I need to add in the ability to make a
client
telnet connection to a remote server and send some commands to it and
deal
with the data received.
In my main program I have this line of code: reactor.connectTCP("mytelnethost", 24, Connections.TelnetConnection("myname of object",events,"command to
run"))
My Connections.TelnetConnection class looks like this:
from twisted.internet.protocol import Protocol,
ReconnectingClientFactory
from twisted.conch.telnet import Telnet
class TelnetClient(Telnet): def connectionMade(self): print "connection made" self.write("\r\n")
def write(self, data): print data Telnet._write(self, data+"\r\n") def dataReceived(self, data): print "received:", data if "User Name:" in data: self.write("user") if "Password:" in data: self.write("password") if ">" in data: time.sleep(2) self.write("connect") if "Connector Name:" in data: time.sleep(2) self.write("another command")
class TelnetConnection(Connection, ReconnectingClientFactory): """ Telnets to host:port and executes cmd. cmd """ protocol = TelnetClient def __init__(self, name, eventQueue, cmd=None): Connection.__init__(self, name)
def clientConnectionFailed(self, connector, reason): print 'connection failed:', reason.getErrorMessage() def clientConnectionLost(self, connector, reason): print 'connection lost:', reason.getErrorMessage()
This all works great, except that the command sequence is hard coded in
the
TelnetClient class. I want to be able to reference the "cmd" parameter, which could be a list of commands to iterate over,etc. But I don't see
how I
can access the "cmd" parameter that gets passed into the
TelnetConnection
class from within the TelnetClient class.
I'm really desparate for some help, I've been racking my brain on this
since
yesterday morning.
Thanks,
Don
As you can see in twisted.internet.protocol.Factory
def buildProtocol(self, addr): """Create an instance of a subclass of Protocol. The returned instance will handle input on an incoming server connection, and an attribute \"factory\" pointing to the creating factory. Override this method to alter how Protocol instances get created. @param addr: an object implementing
L{twisted.internet.interfaces.IAddress} """ p = self.protocol() p.factory = self return p
ReconnectingClientFactory inherits that method, so each TelnetClient instance it builds will have a self.factory you can use to access your cmd list.
Hope that helps,
alecu
So, I have this working now. I can send commands to the telnet server, dynamically. However, is there a way to supress the output of the data I get back so it doesn't flood my shell (stdout)?
How about removing the print from dataReceived? :)
Jean-Paul
participants (3)
-
Alejandro J. Cura
-
Don Smith
-
Jean-Paul Calderone