473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Telnet session

Hello all,

I'm currently trying to pass commands to a telnet session and get the texte
generated (stdin + stdout) by the session. The problem I get is that the
Telnet.read_until() function seems to freeze after a couple of command. I
did a simplify script that reproduce the problem each time (I'm using 2.3.4
on W2K):

-------------------- Begin of script --------------------
import sys
import re
from string import *
from telnetlib import Telnet
TELNET_USER = "myUser"
TELNET_PWD = "myPassword"
TELNET_HOST = "localhost"

TELNET_PROMPT = "C:\\>"

NEWLINE = "\r\n"
TIMEOUT = 1
REMOVEJUNK = 1 #Remove Telnet output junk (formating chars?) before
printing
def printRead(texte):
if not REMOVEJUNK:
print texte
return

p = re.compile( '(.\[[0-9]{1,2};[0-9]{1,2}H[0-9]?|.\[K)+')
print p.sub('\n', texte)

# Loging in
tn = Telnet("localhost")
printRead(tn.read_until("login: ", TIMEOUT))
tn.write(TELNET_USER + NEWLINE)
printRead(tn.read_until("password: ", TIMEOUT))
tn.write(TELNET_PWD + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))

# Send commands
tn.write("echo foo" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar1" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar2" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar3" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar4" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar5" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar6" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar7" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar8" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))
tn.write("echo bar9" + NEWLINE)
printRead(tn.read_until(TELNET_PROMPT, TIMEOUT))

# Closing the connextion
tn.write("exit" + NEWLINE)
tn.close()
-------------------- End of script --------------------

The output looks like (command sent in read. Not included in the script
output.):
echo foo
echo foo
foo
C:\>

echo bar1
echo bar1
bar1
C:\>

echo bar2
echo bar2
bar2
C:\>

echo bar3
echo bar3
bar3
C:\>

echo bar4
echo bar4
bar4
C:\>

echo bar5
echo bar5
bar5
C:\>

echo bar6
echo bar6
bar6
C:\>

echo bar7 <----- From here read_until() is freezing forever without a
TIMEOUT
C:\>

echo bar8 echo foo
foo
bar1
bar1
bar1
bar1
echo bar9
--------------- End of output -----------------

It not related to the number of command sent. If your replace one of the
firsts "echo bar" by "dir", you'll get the error immediatly. It seems to be
related to the number of line generated by Telnet.

Anybody know why? How to correct this?

Thanks.

Yannick
Jul 18 '05 #1
3 8627
"Yannick Turgeon" <no****@nowhere.com> writes:
Hello all,


I don't see anything obviously wrong in the structure, a simpleminded
translation to linux worked as expected even with a command to generate a
recursive directory listing which, on my machine, is huge.

What happens if set REMOVEJUNK to False?, I had to do that to see any output
but I don't know what Windows generates. I guess what you're trying to filter
out is ANSI control sequences but I suspect you may not have that right and
you're removing ALL output.

A minor point, you only need to do the re.compile once, moving it outside the
function would be more efficient.

Eddie
Jul 18 '05 #2
Hello Eddie,
What happens if set REMOVEJUNK to False? Same thing except. It does not remove, in that script, "good" output
(textual). And the first symptom of the problem is not bad output but it's
the fact that Telnet.read_until() is freezing after at a given moment, in
our case on "echo bar7". Without a timeout, the function never return.

It's certainly related to Windows. I'm cooked again! I have to interact with
a windows program (not just send a command and get the ouput but instead (1)
start a program, (2) send a commande, (3) get the result, (4) send another
command based on the precedent result). I tryed first with popen3: no
interaction possible if one of your command depends from a precedent program
answer. Pexpect would do the job but it's not working on Windows. Telnet
would do the job too, but it doesn't seem to work correctly on Windows. I
just looked at Perl telnet lib... and I've got nausea, headache and
urticaria! And I don't even know if it would work better, so...

Thanks for your help. At least I know that my script would work fine on
linux and it's probably Windows related.

Yannick

"Eddie Corns" <ed***@holyrood.ed.ac.uk> wrote in message
news:ce**********@scotsman.ed.ac.uk... "Yannick Turgeon" <no****@nowhere.com> writes:
Hello all,
I don't see anything obviously wrong in the structure, a simpleminded
translation to linux worked as expected even with a command to generate a
recursive directory listing which, on my machine, is huge.

What happens if set REMOVEJUNK to False?, I had to do that to see any

output but I don't know what Windows generates. I guess what you're trying to filter out is ANSI control sequences but I suspect you may not have that right and you're removing ALL output.

A minor point, you only need to do the re.compile once, moving it outside the function would be more efficient.

Eddie

Jul 18 '05 #3
I found the answer... but not the solution. The problem comes from the fact
that Microsoft does not use a *real* telnet server. It's documented in Perl
Net::Telnet doc. They are not fully respecting telnet protocol. Windows
Telnet Server resend the text and ANSI char that would appear in a cmd.exe
window. Curious? Sure! So each time you send a command from a client that
lead to "push" the previous command out of the vitual "display" space,
Windows Telnet Server resend all the resulting "display". You can try it
with a Windows telnet session where the client is started from cmd.exe.
You'll see that after login (here though!), you can write 7 "echo" commands
and get the reply without "pushing" previous "echo" out of the window. On
the 8th "echo" ("echo bar7" in the script given in this thread), the window
is redraw. And when programmaticaly telneting this kink of server, you get
ALL what the screen would contain if it were a cmd.exe window. Yes! ALL!
Even what you already receive. Making the read_until() function unusable.

Here is what I've fond on Perl Net::Telnet doc:
----------------
By default MS-Windows doesn't come with a TELNET server. However third party
TELNET servers are available. Unfortunately many of these servers falsely
claim to be a TELNET server. This is especially true of the so-called
"Microsoft Telnet Server" that comes installed with some newer versions
MS-Windows.

When a TELNET server first accepts a connection, it must use the ASCII
control characters carriage-return and line-feed to start a new line (see
RFC854). A server like the "Microsoft Telnet Server" that doesn't do this,
isn't a TELNET server. These servers send ANSI terminal escape sequences to
position to a column on a subsequent line and to even position while writing
characters that are adjacent to each other. Worse, when sending output these
servers resend previously sent command output in a misguided attempt to
display an entire terminal screen.

Connecting Net::Telnet to one of these false TELNET servers makes your job
of parsing command output very difficult. It's better to replace a false
TELNET server with a real TELNET server. The better TELNET servers for
MS-Windows allow you to avoid the ANSI escapes by turning off something some
of them call console mode.

---------------------

Hope it will prevent anybody to spent as much time as I did on this problem!

Yannick
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kumar | last post by:
Hi all, I want to write a telnet server for an embedded linux box using python.The problem is that it doesn't give a shell ( just a limited CLI commands for configuring it . )My requirement is...
4
by: Donnal Walter | last post by:
On Windows XP I am able to connect to a remote telnet server from the command prompt using: telnet nnn.nnn.nnn.nnn 23 where nnn.nnn.nnn.nnn is the IP address of the host. But using telnetlib,...
2
by: john brown | last post by:
I'm telnetting into a router. Apart from the fact I can't seem to view the output when iniciating the session, I can't match one of the expressions using Net::Telnet. I can telnet into the router...
5
by: richardtinkler | last post by:
I need to communicate with a POP3 server using ASP. Does anybody have any ideas how I can set up a telnet connection using an ASP script? There are some components out there, but my host won't...
1
by: Michael | last post by:
Dear all, Given the following challenge: 1. The application is an exe on a W2K server 2. User logs via telnet to the server 3. After checking the user account and password, a bat file runs...
5
by: Greg Martz | last post by:
I'd like to do the following in C# and prefer using tcpclient rather than raw sockets... Connect to a unix box Login run date +%H%M%S retrieve the response. That's it, nothing more. This...
3
by: michael sorens | last post by:
I have searched extensively and have found only hints of what is needed so far: What I want to do is open a telnet session to a remote host on a given port, login, execute a simple command, and...
2
by: eight02645999 | last post by:
hi i am using a telnet session to simulate an authentication mechanism USER = "user" PASSWORD = "password" try: telnet = telnetlib.Telnet(HOST) telnet.set_debuglevel(5)...
2
by: vmalhotra | last post by:
Hi I am new in python scripting. I want to open a Multiple telnet session through once script. In other way i can tell i want to open two linux consoles through one script. I wrote one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.