473,545 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_unt il() 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("localho st")
printRead(tn.re ad_until("login : ", TIMEOUT))
tn.write(TELNET _USER + NEWLINE)
printRead(tn.re ad_until("passw ord: ", TIMEOUT))
tn.write(TELNET _PWD + NEWLINE)
printRead(tn.re ad_until(TELNET _PROMPT, TIMEOUT))

# Send commands
tn.write("echo foo" + NEWLINE)
printRead(tn.re ad_until(TELNET _PROMPT, TIMEOUT))
tn.write("echo bar1" + NEWLINE)
printRead(tn.re ad_until(TELNET _PROMPT, TIMEOUT))
tn.write("echo bar2" + NEWLINE)
printRead(tn.re ad_until(TELNET _PROMPT, TIMEOUT))
tn.write("echo bar3" + NEWLINE)
printRead(tn.re ad_until(TELNET _PROMPT, TIMEOUT))
tn.write("echo bar4" + NEWLINE)
printRead(tn.re ad_until(TELNET _PROMPT, TIMEOUT))
tn.write("echo bar5" + NEWLINE)
printRead(tn.re ad_until(TELNET _PROMPT, TIMEOUT))
tn.write("echo bar6" + NEWLINE)
printRead(tn.re ad_until(TELNET _PROMPT, TIMEOUT))
tn.write("echo bar7" + NEWLINE)
printRead(tn.re ad_until(TELNET _PROMPT, TIMEOUT))
tn.write("echo bar8" + NEWLINE)
printRead(tn.re ad_until(TELNET _PROMPT, TIMEOUT))
tn.write("echo bar9" + NEWLINE)
printRead(tn.re ad_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 8652
"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_unt il() 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
3821
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 to write a telnet server ( residing on the box) listening on some specific port where it can listen to telnet requests from clients and provide them...
4
11141
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, this code returns the traceback that follows: import telnetlib host = 'nnn.nnn.nnn.nnn'
2
8263
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 manually without a problem. I'm running a sniffer at the same time just to see what's happening behind the scenes. The computer does an ARP...
5
5847
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 allow them. Thanks for any ideas, R
1
2344
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 cmd.exe that starts the app. 4. The app starts looking for another instance of itself and if it was found it will kill the instance (but not the telnet...
5
33830
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 shouldn't be too complicated, I thought... I have yet to find any examples of being able to do this.
3
5046
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 retrieve the results. The TcpClient class documentation provides a nice example, but it does not work for telnet... I suspect there is more to the...
2
4561
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) telnet.read_until("login: ") telnet.write(USER + "\n") telnet.read_until("Password: ")
2
7688
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 script, but the issue is I am not able to open multiple consoles. The Scripts which i wrote is as follows: import pexpect
0
7680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.