tags:

views:

363

answers:

3

I have a basic chat server which I can easily connect to with telnet. I simply enter the host and port and, without any further authentication, can begin entering commands that the server can interpret. In an effort to simulate user traffic, I would like to create a script that will open telnet, connect to the server, and immediately begin sending commands to the server. I initially attempted to do this with batch scripts, but after encountering too many road blocks I have decided to use Python's telnetlib module. This is what the script looks like now

import telnetlib
myTel = telnetlib.Telnet('XXX.XXX.XXX.XXX', XXXX)
myTel.write('login')
myTel.write('move room')
myTel.write('say message')
myTel.write('exit')
myTel.write('logout')

Its really that simple and the script runs with no errors. However, if I've already manually logged into the server on another telnet session, I fail to see the script entering the main room or sending a chat message which leads me to believe that something is going wrong. If I manually start two telnet sessions I can easily send and receive messages between the sessions, so I would think that a manual telnet session should be receiving the messages from the session started by the script.

Any ideas?

A: 

In your code, you write to the server, but in order to receive data, you'll eventually have to one of the read methods of the library. To see an example implementation of a client with a 'reading thread', see this blog post: http://hahong.org/blog/posts/261.

fn. Also try:

myTel.write('login\n')

.. with newlines.

The MYYN
The very next sentence reads: "Alternatively, the host name and optional port and timeout can be passed to the constructor, in which case the connection to the server will be established before the constructor returns."
avakar
yes, of course ..
The MYYN
I added some read_until commands to my script along with adding a \n to the string in each write command and still nothing. Any other ideas?
HoboMo
+2  A: 

Perhaps adding a newline would help?

myTel.write('login\n')

I'd also recommend using some network sniffing tool (i.e. Wireshark) to verify that you're really sending what you intended.

avakar
A: 

I would use read_until to check that the server is ready to receive the command here is a sample using telnet to talk to a cisco switch:

from telnetlib import Telnet
PASSWD = "mypass"

t = Telnet(switch)
t.read_until("Password: ", 2)
t.write(PASSWD + "\r\n")

Here I wait for a password prompt before sending the password.

kerchingo