473,910 Members | 4,221 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 8669
"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
3842
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 an interactive command line session. I tried using the python's async_chat module but it is very...
4
11220
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
8287
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 request looking for the router's direction, but that seems to be it. I don't think any data actually...
5
5881
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
2380
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 session that originally started the instance as described in 1. to 3.)
5
33929
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
5067
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 negotiation for telnet than just the generic TCP client they present in the sample. I am hoping...
2
4625
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
7737
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
10037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9879
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11349
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10921
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8099
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7250
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5939
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6142
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3360
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.