using telnetlib in python/cgi to config a router and display to webpage

David used Ask the Experts™
using telnetlib in python to configure a router. i'm using cgi to display the python script to a browser.
i'm having trouble troubleshooting the code because cgi just display's an error screen 'internal server error'
so, i really don't see where i'm making the mistake. i can run the python script through the python interpreter, but that's just a hassle.

any suggestions on how to troubleshoot the output? i've tried using
print "Content-type:text/plain\n" as opposed to "Content-type:text/html\n" so as to just display the unformatted output without html formatting, but i still am not seeing where i'm making mistakes.

i know i can cat /var/log/httpd/error_log

but i guess what i'm asking is if anyone can give me some more specifics about what is actually going on.
i understand this:
telnetlib is a module that'll open a socket to the router or switch.  the physical and data link connection we're not going to see, the network layer is addressed by the ip which i have set to a variable and is here
host = ""
tn = telnetlib.Telnet(HOST, timeout=TIMEOUT)
any further specifics of communication is held within the actual module telnetlib -- socket or transport layer tcp settings.

the server 'sends' the message, encapsulated at what? layer 7 down through layer 2 as the data communications model/osi specifies.. it goes over the wire and over to the client which decapsulates the packet and sends it back up to the telnet application.

i understand i can't see the output of the client(the switch or router) because stdout is defined on the router and it's not the socket that the command came in on, so it has no transport back to the server.  it is buffered? in the object?

this is where i'm getting lost

i know i'll eventually use output = tn.read_all() to display the output on the website, but that, at least in part, is failing



i can provide more code. i think these are all fairly standard things to non-newbies like me
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fractional CTO
Distinguished Expert 2018
Unsure exactly what you're asking.

Telnet != HTTP, so you can use telnetlib... very easily... to conduct an HTTP conversation.

To accomplish this, try this sequence...

1) Use curl to connect to your router.

2) You will not be able to see any /var/log/httpd/error_log entries of use, unless these logs are available on your router (where the HTTP server is running).

3) Once you get #1 working, then if you must, you can always convert your #1 command line curl to use libcurl in some language like Python.
DavidCyberNetSec Guy


Telnetlib is a python module
DavidCyberNetSec Guy


i'm not sure i understand the answer, but i'll keep it in the repository for future reference. i was trying to do something specifically with telnetlib for a class, but will look into the other things .. i've heard of curl and would like to see what it can do ... isn't it just a downloader of sorts?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial