Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyone had luck with telnetlib.expect()?

I'm writing a library to support telnet'ing to a remote server and running apps.

Things are going swimmingly in establishing a connection, getting data back, parsing, etc. (at least as swimmingly as it can be for communicating with programs via a text interface).

One app will change the cursor if it enters properly, or leave the original cursor if it fails (I don't write the apps, I just have to use them.)

When said app starts up correctly, this works with no problem:

promptB = "hello(x)# "   # Yes, the space at the end is intentional
response = tn_conn.cmd("app_name\n", prompt=promptB)

I would like to use the prompt change (or lack of prompt change) to detect whether the program failed to start. I figured this would be a golden opportunity to try telnetlib's expect(), since expect() allows one to pass a list of strings to match in the response.

I cannot, however, get this to work:

promptA = "hello(x)# "   # Yes, the space at the end is intentional
promptB = "hello> "      # Yes, the space at the end is intentional

tn_conn.write("app_name\n")
which_prompt, mo, response = self.tn_conn.expect([promptA, promptB], timeout=3)

The expect command always times out, whether to apps starts sucessfully or not.

which = "-1"

mo = None

response = "mumble mumble\r\r\n other stuff\r\n\r\nhello# "

The docs say that either a string or a regex object can be passed to expect (I'm passing a string), so am I missing something? A look at the telnetlib code shows that its calling re.search(), not re.match(), so that wouldn't seem to be the issue.

Can anyone please offer suggestions on what I'm doing wrong?

Edit Added parens to the prompt example to better illustrate why expect() was not working as expected.

like image 231
JS. Avatar asked Oct 18 '11 23:10

JS.


People also ask

What is Telnetlib?

The telnetlib module provides a Telnet class that implements the Telnet protocol. See RFC 854 for details about the protocol. In addition, it provides symbolic constants for the protocol characters (see below), and for the telnet options. The symbolic names of the telnet options follow the definitions in arpa/telnet.

How do I telnet a device in Python?

Practical Data Science using Python Telnet is a type of network protocol which allows a user in one computer to logon to another computer which also belongs to the same network. The telnet command is used along with the host name and then the user credentials are entered.

How do I close a telnet connection in python?

After import socket and assuming your session is called session , try session. get_socket(). shutdown(socket. SHUT_WR) .

How do I install Telnetlib?

The telnetlib module provides a Telnet class that implements the Telnet protocol. If you have Python installed, the telnetlib library is already installed, but if it isn't, we can use the pip command to install it. Alternatively, you can use the code below to see if it's installed on your system: Python.


2 Answers

Don't forget if you are using the regex in python you can always use the raw (r'my string') method rather than adding in all the escapes; makes it more readable.

like image 165
Sisyphus Avatar answered Oct 15 '22 17:10

Sisyphus


I got something to work. Lookig for # or % or $ prompts. As for your prompts, make sure that special characters are escaped. ( ). Maybe escape everthing just to be sure.

idx, obj, response = tn_conn.expect("\#","\%","\$",3)
like image 42
Robert Jacobs Avatar answered Oct 15 '22 17:10

Robert Jacobs