Reputation: 69
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()
Upvotes: 1
Views: 4891
Reputation: 13293
Have you tried writing the ASCII character for CTRL+] to the telnet connection?
tn.write('\x1d')
Upvotes: 2
Reputation: 69
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
Upvotes: 0