How do I check if Telnet session is open?

M

Mudcat

Hi all,

I am running a script that uses telnet sessions which have to remain
open for an extended period of time. The device under test I'm working
on times out the sessions after 10 mns for security reasons due to
inactivity, and there are long stretches of time where I am unable to
do any type of maintenance process where I can prod them to stay open.

In order to work around this I have created a class that has exceptions
where if the session is closed it will automatically re-open since it
retains it's ip/port info. I thought this would fix my problem, but it
didn't completely.

The problem comes because Python is unaware that the session has closed
when it tries to write to it. I don't know the internal workings of
telnet sessions or how it's supposed to work, but according the Python
the write is successful. However when the read occurs, then it fails. I
could put exception handling there also, but by then it's too late. The
write has already been lost.

So how can I from within Python tell if a session is still open before
writing to it? I don't see any options in Telnet class that do it. Is
there any way?

Thanks ahead of time,
Marc
 
M

Mudcat

Let me narrow the focus a little since I've found out how this is
usually done. I'm attempting to solve the problem using select. However
I can't get the select function to block until something's available.
Here is my code. I am running on windows.
conn = telnetlib.Telnet("10.10.10.89", "3083")
conn2 = telnetlib.Telnet("10.10.10.89", "3083")
sock = conn.get_socket()
sock2= conn2.get_socket()
sock.setblocking(1)
sock2.setblocking(1)
w, r, e = select.select([], [sock, sock2], [] )

Pretty simple really. I'm blocking on the sockets which should block on
the select call. However it never blocks. The call returns immediately
spitting back out both my sockets which have nothing in them. When I do
a socket recv on both, they block because they contain no info. The
select should have never returned in the first place.
Anyone know what I'm doing wrong?
 
I

Irmen de Jong

Mudcat said:
Let me narrow the focus a little since I've found out how this is
usually done. I'm attempting to solve the problem using select. However
I can't get the select function to block until something's available.
Here is my code. I am running on windows. [...]
w, r, e = select.select([], [sock, sock2], [] )

The select call waits until you can *write* to the sockets.
It should read:

w, r, e = select.select([sock, sock2], [], [] )

--Irmen
 
I

Irmen de Jong

Irmen said:
The select call waits until you can *write* to the sockets.
It should read:

w, r, e = select.select([sock, sock2], [], [] )

Darn, I forgot to switch the w,r around too. It must be:

r, w, e = select.select([sock, sock2], [], [] )


--Irmen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top