Reputation: 31
Don't know if the issue is with the python, the telnet library or the router but I just can't seem to get the script running like it should.
It's a simple script that will display the output of the routing table from the router, but somehow if you don't set the command for trap/debug it won't display/print anything at all.
I'm new to programming and just started to learn python, please tell me where I'm making a mistake or is it simply the routers limitation?
The script is fairly simple:
import getpass
import telnetlib
import time
user= "root"
password = "admin"
print("Router Script")
HOST = "192.168.100.1" #the gateway of the router
tn = telnetlib.Telnet(HOST)
tn.set_debuglevel(7) #the master of puppets
tn.read_until(b"Login:")
time.sleep(1)
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"Password:")
tn.write(password.encode('ascii') + b"\n")
tn.read_until(b"WAP>")
tn.write(b"display ip route\n" )
print(tn.read_all().decode('ascii'))
This is what the output looks like
EDIT: I'm running it directly on my router using powershell i.e "py.exe C:\script.py" Also the script is written in python 3.0 if I need to mention, and the library is also for that.
Here is the output as text as requested, it's all messy and without any order...
Router Script
Telnet(192.168.100.1,23): recv b'\xff\xfb\x01\xff\xfb\x03\xff\xfb\x18\r\nWelcome Visiting Huawei Home Gateway\r\nC'
Telnet(192.168.100.1,23): IAC WILL 1
Telnet(192.168.100.1,23): IAC WILL 3
Telnet(192.168.100.1,23): IAC WILL 24
Telnet(192.168.100.1,23): recv b'opyright by Huawei Technologies Co., Ltd.\r\n\r\nLogin'
Telnet(192.168.100.1,23): recv b':'
Telnet(192.168.100.1,23): send b'root\n'
Telnet(192.168.100.1,23): recv b'\xff\xfc\x01\xff\xfc\x03'
Telnet(192.168.100.1,23): IAC WONT 1
Telnet(192.168.100.1,23): IAC WONT 3
Telnet(192.168.100.1,23): recv b'root\x07\r\nPassword:'
Telnet(192.168.100.1,23): send b'admin\n'
Telnet(192.168.100.1,23): recv b'\x07'
Telnet(192.168.100.1,23): recv b'\r\nPassword is default value, please modify it!\r\nWA'
Telnet(192.168.100.1,23): recv b'P>'
Telnet(192.168.100.1,23): send b'display ip route\n'
Telnet(192.168.100.1,23): recv b'display ip route\x07'
Telnet(192.168.100.1,23): recv b'\r\n\r\nFlags:[B]lackhole\r\n\r\n-------------------------'
Telnet(192.168.100.1,23): recv b'--------------------------------------------------'
Telnet(192.168.100.1,23): recv b'--------------------------------------------------'
Telnet(192.168.100.1,23): recv b'---\r\nDest/MaskLen Interface G'
Telnet(192.168.100.1,23): recv b'ateway SourceIP Flags '
Telnet(192.168.100.1,23): recv b' Metric Origin \r\n---------------'
Telnet(192.168.100.1,23): recv b'--------------------------------------------------'
Telnet(192.168.100.1,23): recv b'--------------------------------------------------'
Telnet(192.168.100.1,23): recv b'-------------\r\n0.0.0.0/0 Internet '
Telnet(192.168.100.1,23): recv b' -- -- -'
Telnet(192.168.100.1,23): recv b'- 10 StaticRoute \r\nxx.xx.'
Telnet(192.168.100.1,23): recv b'xx.xx/14 VOIP -- '
Telnet(192.168.100.1,23): recv b' xx.xx.xx.xx -- 0 '
Telnet(192.168.100.1,23): recv b' System \r\nxx.xx.xx.xx/32 I'
Telnet(192.168.100.1,23): recv b'nternet -- xx.xx.xx.'
Telnet(192.168.100.1,23): recv b'xx -- 0 System '
Telnet(192.168.100.1,23): recv b' \r\n192.168.100.0/24 LocalNetwork -'
Telnet(192.168.100.1,23): recv b'- 192.168.100.1 -- '
Telnet(192.168.100.1,23): recv b' 0 System \r\n---------------'
Telnet(192.168.100.1,23): recv b'--------------------------------------------------'
Telnet(192.168.100.1,23): recv b'--------------------------------------------------'
Telnet(192.168.100.1,23): recv b'-------------\r\nTotal: 4\r\n\r\nsuccess!\r\nWAP>'
Upvotes: 2
Views: 1930
Reputation: 365975
The problem is that you're calling read_all
:
Read all data until EOF as bytes; block until connection closed.
But the router hasn't closed the connection. It just sends you a bunch of bytes ending in this:
success!
WAP>
And at that point, the router is waiting for another command.
Until you tell it to quit, or close the connection yourself, you're never going to reach EOF.
Which means your code is sitting there waiting for read_all
to finish, which will never happen. Presumably, your code just hangs there until you hit Ctrl-C.
What you probably want to do is read until the prompt again:
print(tn.read_until(b"WAP>").decode('ascii'))
And then maybe close()
the connection, or write(b'exit\r\n')
or something that will make the router close the connection for you.
Upvotes: 1