0

I am trying to automate a set of telnet commands used when configuring a linux server. I am currently trying to use Python 2.7 for this and thought that telnetlib would be the way to go. But unfortunatly it doesn't work as I would expect.

tn = telnetlib.Telnet(ip, port)
print tn.read_until("SysCLI>")
tn.write("help" +"\n")
print tn.read_until("Registered components are:", 10)

This will terminate after 10 seconds printing nothing but the first print line: "SysCLI>". I have tried different combinations of "carriage return" and "line feed" including "\r", "\r\n", "\n\r", none of them seem to help. Do anyone have a suggestion on what I am missing?

Update: I tried my code on a different server and it seems to work fine:

ip = "www.google.com"
port = 80

tn = telnetlib.Telnet(ip, port)
tn.write("Get / HTTP/1.1" +"\n" +"\n")

print tn.read_some()

So I am thinking that my problem is the server that needs some special character for sending messages.

jimmy
  • 1,981
  • 3
  • 19
  • 28
  • Use `tn.read_some()` instead of `tn.read_until` to make sure `tn.write` works or not. `read_until` will wait and `read_some` just read something and return. This method will be helpful when you are not sure about the result. – longhua Aug 18 '14 at 10:00
  • Good suggestion. But in this case I know exactly what result to expect since I can still do it manually. I did however try it but it returned nothing, same as read_until() – jimmy Aug 18 '14 at 10:22
  • What OS doesn't your server use? – longhua Aug 18 '14 at 11:22
  • The server is using a home made Linux distribution. – jimmy Aug 18 '14 at 11:30
  • I don't have any suggestion. Maybe you can try to figure out which shell you are using. Is it caused by encoding issue? You can also try to use tcp dump to check transfer between client and server. – longhua Aug 18 '14 at 12:41

1 Answers1

0

So apparently I had misunderstood the system a bit. After the initial connection this is no longer a telnet session per se but actually an application running on the server. That is the reason I see the SysCLI prompt. So this is probably why I can't use the telnetlib.

Instead I will try to use subprocecss.Popen() to run the session through a terminal.

jimmy
  • 1,981
  • 3
  • 19
  • 28