Using Python telnet lib w/o logout command

Go To StackoverFlow.com

1

I am trying to use python's telnetlib module to get information from a remote device. Unfortunately, it looks like the remote device does not have a "logout" type of command. So you have to manually close the connection with CTRL-] (when manually telnetting). I tried using Telnet.close() but doesn't seem to return any data.

Suggestions?

HOST = "172.16.7.37"
user = "Netcrypt"
password = "Netcrypt"

tn = telnetlib.Telnet(HOST)

tn.read_until("User: ")
tn.write(user + "\n")
if password:
   tn.read_until("Password: ")
   tn.write(password + "\n")

tn.write("session \n")

print tn.read_until("NC_HOST> ")

tn.close()
2012-04-05 21:24
by slappyjam
Please add the code by editing the original question and using the code formatting tools available there - Jason LeBrun 2012-04-05 21:26
code has been poste - slappyjam 2012-04-05 21:33
What do you mean with "doesn't seem to return any data"? Where do you expect it to return something? It must have executed fine until the last line, otherwise read_until wouldn't have returned - j13r 2012-04-05 21:44


2

Have you tried writing the ASCII character for CTRL+] to the telnet connection?

tn.write('\x1d')

2012-04-05 21:28
by Jason LeBrun
would I follow it with tn.write('quit\n') - slappyjam 2012-04-05 21:35
Is 'quit' a valid command for the telnet server that you're connected to? If you need to manually close the connection using Ctrl-], then writing '\x1d' (which is the ASCII value of Ctrl-]) should do the trick - Jason LeBrun 2012-04-05 21:37
If i use tn.write('\x1d') it does terminate the connection. But if I add print tn.read_all() after it, it just hangs. I dont see how i can get the information back from the 'session' command i issue - slappyjam 2012-04-05 21:43
What about read_eager or read_lazy - Jason LeBrun 2012-04-05 21:45
well read_eager and read_lazy don't hang but they don't print anything eithe - slappyjam 2012-04-05 21:52
Well, that would indicate that there isn't anything to read then. You shouldn't sent the \x1d until you've received all of the data that you one, as I imagine it will immediately close the connection - Jason LeBrun 2012-04-05 21:53
I ended up not needing any of that. The deal was I had to read to the prompt, issue my command, read until the next prompt. Never needed to read_all

`print tn.read_until('NC_HOST>')`
< - slappyjam 2012-04-05 22:11
If you've solved your original problem, you should post an answer of your own and accept it - Jason LeBrun 2012-04-05 22:13
OK but I have to wait 7 hours lo - slappyjam 2012-04-05 22:19


0

I ended up not needing any of that. The deal was I had to read to the prompt, issue my command, read until the next prompt. Never needed to read_all().

Here's the working code:

import telnetlib

HOST = "172.16.7.37"
user = "Netcrypt"
password = "Netcrypt"

tn = telnetlib.Telnet(HOST)

tn.read_until("User: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

    tn.read_until('NC_HOST>')

    tn.write("session\n")

    data = tn.read_until('NC_HOST>')

print data
2012-04-06 15:30
by slappyjam
Ads