telnet works,but python telnetlib fails - python

I am trying to telnet into a altusen power KVM and reboot a computer.
when doing this using the windows telnet it works great.
but when I use python's telnetlib, all read commands fail
(not immediately but on timeout)
again when using the windows telnet everything works fine, and I can see the login message
the following is the code I use: [on python 2.7.3]
import telnetlib
tn = telnetlib.Telnet("11.11.11.11")
print tn.read_all()//timeout fail,windows telnet does get a response
okay to clearify:
python No user input gets nothing read_all() times out
normal windows telnet client No user input gets the following:
** ALTUSEN -- PN9108 Configuring Through Telnet **
Login:

Related

How do I send telnetlib control + ] command in python

I am trying to send control + ] command in python using telnetlib library.
Currently I am doing:
tn.write('^]')
and also
tn.write('\x1D')
which i got from http://donsnotes.com/tech/charsets/ascii.html
To type control-A I use tn.write('\x01') and it works so I am confused why tn.write('\x1D') is not working for control-].
Thanks for any help
ctrl-] cannot be sent on the wire. You have to use a, let's say, synonym for it like close()
For further reading, see the pinpoint answer here: https://mail.python.org/pipermail/python-list/2012-December/636929.html
The ^] command is not actually sent to the server. It is a command to the telnet client. When you run the telnet program (not related to python at all) you see:
~$ telnet localhost 2050
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
That means ^] is a escape character. It is a way to make the local telnet program exit the input mode that sends everything to the server, and enter the input mode that accepts local commands. When you type ^] in this case, the telnet program won't send it to the server, it will just change the input mode.
Since you're connecting using the telnet protocol directly to the server, and not using the telnet program, it doesn't make sense to send ^].

accessing router via telnet lib python

I'm working on a code that uses telnetlib of python to connect to a router and execute commands and stores the output in a file.
I'm using read_until('#') function and expecting a Router prompt, then execute the next command but my code freezes when I receive a '--More--' data from the remote telnet side. I tried using a pattern match to find '--More--' but then sometime the --More-- keyword doesn't come at once.
Any suggestion ?
Do I have to send some IAC command to the remote telnet side ?
sometime the --More-- keyword doesn't come at once
Try passing in a timeout.
Example: set timeout to 5 seconds for read_until():
read_until('--More--', 5)
Alternatively, you could use the expect() function to look for either '#' or '--More--' with a timeout:
expect(['#', '--More--'], 5)

Check if socket is in use in python

I am running a script that telnets to a terminal server. Occasionally the script is launched while one instance is already running, which causes the already running script to fail with
EOFError: telnet connection closed
Is there a quick and easy and pythonic way to check if the required socket is already in use on the client computer before I try to open a connection with telnetlib?
SOLUTION:
I wanted to avoid making a subprocess call but since I do not control software on the client computers, and other programs may be using the same socket, the file lock suggestion below (a good idea) wouldn't work for me. I ended up using SSutrave's suggestion. Here is my working code that uses netstat in Windows 7:
# make sure the socket is not already in use
try:
netstat = subprocess.Popen(['netstat','-nao'],stdout=subprocess.PIPE)
except:
raise ValueError("couldn't launch netstat to check sockets. exiting")
ports = netstat.communicate()[0]
if (ip + ':' + port) in ports:
print 'socket ' + ip + ':' + port + ' in use on this computer already. exiting'
return
You can check for open ports by running the following linux command netstat | grep 'port number' | wc -l by importing subprocess library in python.
There is not a standard way to know if a server has other opened connections before you attempt to connect to it. You must ask it either connecting to another service in the server that checks it, or by asking the other clients, if you know all of them.
That said, telnet servers should be able to handle more than one connection at a time, so it should not matter if there are more clients connected.

Telnet from telnet session in python

Is it possible to telnet to a server and from there telnet to another server in python?
Since there is a controller which I telnet into using a username and password, and from the controller command line I need to login as root to run linux command. How would I do that using python?
I use the telentlib to login into router controller but from the router controller I need to log in again to get into shell. Is this possible using python?
Thanks!
Just checked it with the hardware I have in hand & telnetlib. Saw no problem.
When you are connected to the first device just send all the necessary commands using telnet.write('cmd'). It may be sudo su\n, telnet 192.168.0.2\n or whatever else. Telnetlib keeps in mind only its own telnet connection, all secondary connections are handled by the corresponding controllers.
Have you looked into using expect (there should be a python binding); basically, what I think you want to do is:
From your python script, use telnetlib to connect to server A (pass in username/password).
Within this "socket", send the remaining commands, e.g. "telnet serverB" and use expect (or some other mechanism) to check that you get back the expected "User:" prompt; if so, send user and then password and then whatever commands, and otherwise handle errors.
This should be very much doable and is fairly common with older stuff that doesn't support a cleaner API.
You can use write() to issue the sudo command.
tn.write("sudo\n")
You could also use read_until() to help with the credentials.
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")

Writing a telnet client

HI,
I have a device that exposes a telnet interface which you can log into using a username and password and then manipulate the working of the device.
I have to write a C program that hides the telnet aspect from the client and instead provides an interface for the user to control the device.
What would be a good way to proceed. I tried writing a simple socket program but it stops at the login prompt. My guess is that i am not following the TCP protocol.
Has anyone attempted this, is there an opensource library out there to do this?
Thanks
Addition:
Eventually i wish to expose it through a web api/webservice. The platform is linux.
If Python is an option you could use telnetlib.
Code example:
#!/usr/bin/env python
import getpass
import sys
import telnetlib
HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()
telnet's protocol is pretty straightforward... you just create a TCP connection, and send and receive ASCII data. That's pretty much it.
So all you really need to do is create a program that connects via TCP, then reads characters from the TCP socket and parses it to update the GUI, and/or writes characters to the socket in response to the user manipulating controls in the GUI.
How you would implement that depends a lot on what software you are using to construct your interface. On the TCP side, a simple event loop around select() would be sufficient.
While telnet is almost just a socket tied to a terminal it's not quite. I believe that there can be some control characters that get passed shortly after the connection is made. If your device is sending some unexpected control data then it may be confusing your program.
If you haven't already, go download wireshark (or tshark or tcpdump) and monitor your connection. Wireshark (formerly ethereal) is cross platform and pretty easy to use for simple stuff. Filter with tcp.port == 23
This might be useful reading for you.
I really find Beej's Guide to Network Programming a good introduction to network programming in C.
Download Putty source code. Examine TELNET.C, the Telnet backend for Putty.
Unless the application is trivial, a better starting point would be to figure out how you're going to create the GUI. This is a bigger question and will have more impact on your project than how exactly you telnet into the device. You mention C at first, but then start talking about Python, which makes me believe you are relatively flexible in the matter.
Once you are set on a language/platform, then look for a telnet library -- you should find something reasonable already implemented.
Check out the source code here. It has helped me out a lot in understanding Telnet protocol.
See options in your telnet client: on an arbitrary listening socket and hit escape] to enter the client.
telnet> help
telnet> set ?

Categories