0

I am writing a script on python that will connect to a telnet server and write some basic commands. The login part is working fine, so is the "show version" command, but when I write the "quit" command, it types "uit" into telnet.

One thing that did work is writing "qquit", that would enter "quit" into the telnet, but it's inconsistent. Jus to try things out, I tried three "show version" commands, two of them worked, the middle one typed "how version", missing the first letter.

user = "cisco"
password = "cisco"
tn = telnetlib.Telnet(HOST)

tn.read_until(b"Username: ")
tn.write((user + "\n").encode('ascii'))
if password:
     tn.read_until(b"Password: ")
     tn.write((password + "\n").encode('ascii'))

tn.write(b"show version\n")
tn.write(b"quit\n")

print(tn.read_until(b"quit").decode('ascii'))

Expected output

DPI-testbed-Switch>show version

...

...

...

DPI-testbed-Switch>quit

Actual Output

DPI-testbed-Switch>show version

...

...

...

DPI-testbed-Switch>uit

Translating "uit"...domain server (255.255.255.255)

I would greatly appreciate any help, and am open to any changes that need to be made, so long as I can run commands in a reliable way.

Taha
  • 65
  • 1
  • 10
  • 1
    Don't forget that in the Telnet protocol, a newline is really a carriage return-newline pair: `"\r\n"`. – Some programmer dude Jul 02 '19 at 12:17
  • @Someprogrammerdude Just tried using "\r\n" after the "show version" and "quit", and there was no difference – Taha Jul 02 '19 at 12:31
  • @Someprogrammerdude Your idea gave me a thought, I tried using two spaces before the commands (didn't matter if I used "\n","\r", or both), and that actually helped solve the issue, and even helped with another issue I was having, but would you happen to know why that's the way it works? – Taha Jul 02 '19 at 12:37

0 Answers0