telnetlib read_until() function confusion - python

I am trying to automate some telnet actions with python3. So I started to use the build in telnetlib (not pexpect).
Actually everything works so far but I do not understand completly how read_until works - actually the documentation says that you can set a timeout and if the search string is not found the timeout sends back a empty byte value or or if there is another value thats the one which can be stored as a return value!
Does that make sense ?!
If I wanna read_until a certain value - how do I find out that this value was really the one the function read. Also I couldn't find out how to check if the timeout was hit.
My workaround for now is:
output = telnet.read_until(str.encode(hostname), 3)
if re.search(hostname, bytes.decode(output), re.IGNORECASE):
#do something when the output matches the searchstring
else:
#stop the function
but that doesn't make any sense for me, so perhaps you now a better solution

Yes, if your hostname is example.com, read_until will return something like weijwrgnerg hgqwv blather example.com. Except if it hasn't found the hostname after three seconds, it will just spit out whatever it's got by then: weijwrgnerg hg.
So I think you're doing it almost right. You probably want to re.escape your hostname, otherwise it will interpret it as a regex. Alternatively, you could just use Python's .endswith(). And you can do output.decode() to get a string (rather than bytes.decode(output)).

Related

How to combine python strings and ints?

I have been working on a small toolkit for awhile now as a way to learn basic python. part of this toolkit is the code that follows this explanation. It is a basic network enumeration script, to simply ping all ip addresses in your local subnet. However I am having an issue implementing a few pieces.
first the code
ip1=raw_input()
if ip1 == None:
ip="192.168.0.x"
else:
ip=ip1
ipend="1"
while ipend != 255:
ip.replace("x", ipend)
attempt=subprocess.Popen(["ping.exe", ip], stdout=subprocess.PIPE).communicate()[0]
if ("unreachable" in attempt):
pass
else:
print "The host at ", ip, "is UP!"
ipend += 1
if ipend == "255":
subprocess.stop
raw_input("Thank you for using PTK. Please press enter to exit.")
enum()
so my first issue, I believe is in my str.replace function, I am attempting to and an int to a str and due to this it seems to be unable to create, ping, and print the correct ip. I'm unsure if converting the entire string to a floating int would work, however its a thought I'm toying with. as previously stated I am very basic with python and am working on learning so please forgive me if this a stupid question with a simple fix.
This line doesn't actually do anything. You need to assign the result to something.
ip.replace("x", ipend)
ipend is a string, incrementing a string won't work. Make it a int and convert it to a str in the replace function.
ipend += 1
And it's usually better to use a for loop if you can, so you're safe from messing up and creating a infinite loop.
for ipend in range(1,256): #Range won't actually give you the last number here.
#Do stuff

Can pexpect be told to ignore a pattern or signal?

I'm trying to write a python script to access several Cisco network devices. Sadly the devices are not set up the same, some have banners up that appear at weird places. These banners might have the patterns that have some of the same stuff a prompt might have.
For example, once I log in I expect a prompt of 'hostname#', so I basically use a pexpect('#') I might have to handle certain other prompts such as 'hostname>' as well so pexpect('>') works. I'll really have a list of possible prompts, plus a timeout and act accordingly.
This is fine unless a banner shows up in the middle somewhere. For example, after logging in a banner might show up that says ## Welcome ## and be there right before the prompt. I believe if I could tell pexpect to ignore the regular expression #.*\r I'd be fine, but I'm not sure this is possible. Is it possible to have pexpect ignore a string, or is there a better way to go about this?
A solution I used to use back in the BBS days was to look for a prompt, and then immediately after I got one, look for a line break or a space with a timeout of a second or two. If the second wait timed out, that meant I was really waiting at a prompt. Since pexpect uses regular expressions, you can actually check to see if you receive any character within a second after a prompt.
def wait_for_prompt(session, prompt, timeout=1):
gotprompt = 0
while not gotprompt:
session.expect(prompt, timeout=None)
gotprompt = session.expect([".", pexpect.TIMEOUT], timeout=timeout)
child = pexpect.spawn("ssh ...")
wait_for_prompt(child, "[#>] ?")
I've never actually used this library before so this may need some tweaking, but it's an approach I used successfully in the '80s. :-)
The Python pexpect module waits for matching input. You can't actually ignore input, except by creating regular expressions that will not match ambiguous or undesirable input. Instead of creating one really complex regex, though, pexpect can also handle lists of regular expressions. You then anchor those regular expressions to reduce ambiguity.
For example, you can easily differentiate between a command prompt and a banner by anchoring the match to the start or end of the line, and making the match as exact as possible:
users = ['fred', 'ginger']
user_prompt = '^(?:%s)#.*?\$ $' % '|'.join(users)
child.expect (['hostname# $', 'hostname> $', user_prompt])
They key is to understand what input you want to match, build an unambiguous regular expression with anchors, and then update your expressions whenever you find a mismatch--which hopefully won't be too often because you didn't create overly-greedy patterns in the first place.
Once expect has found a match it sets the 'before', 'after', and 'match' attributes on the process instance. You could use these to write some logic.
Alternatively you could just craft a better regex that only matches the prompt that you want. Even r'>$|#$' would probably do what you want since it is only going to match those characters at the end of a line.

^H ^? in python

Some terminals will send ^? as backspace, some other terminals will send ^H.
Most of the terminals can be configured to change their behavior.
I do not want to deal with all the possible combinations but I would like to accept both ^? and ^H as a backspace from python.
doing this
os.system("stty erase '^?'")
I will accept the first option and with
os.system("stty erase '^H'")
I will accept the second one but the first will be no longer available.
I would like to use
raw_input("userinput>>")
to grab the input.
The only way I was able to figure out is implementing my own shell which works not on "raw based input" but on "char based input".
Any better (and quicker) idea?
The built-in function raw_input() (or input() in Python 3) will automatically use the readline library after importing it. This gives you a nice and full-feautured line editor, and it is probably your best bet on platforms where it is available, as long as you don't mind Readline having a contagious licence (GPL).
I don't know your question exactly. IMO, you need a method to read some line-based text(including some special character) from console to program.
No matter what method you use, if read this character have special mean in different console, you should confront a console(not only system-specific, but also console-specific) question, all text in console will be store in buffer first, and then show in screen, finally processed and send in to your program. Another way to surround this problem is to use a raw line-obtaining console environment.
You can add a special method(a decorator) to decorate the raw_input() or somewhat input method to process special word.
After solved that question
using this snippet can deal with input,:
def pre():
textline=raw_input()
# ^? should replace to the specific value.
textline.replace("^?","^H")
return textline
To be faster, maybe invoke some system function depend on OS is an idea. But in fact, IO in python is faster enough for common jobs.
To fix ^? on erase do stty erase ^H

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.
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.
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)
In previous attempts, I had pursued the regex option by placing .* at both ends of my search string, as well as doing a re.compile() to the search string before passing it/them to .expect(); all with no luck.
Thanks to jathanism's suggestion, I re-examined using regex, this time with the thought that expect() was, er... expecting 'regex' where I was thinking 'string'.
Sure enough, there were characters in my prompt string that expect was treating as regex symbols -- ()'s to be exact. Escaping the parens let expect() do its job.

In Python what's the best way to emulate Perl's __END__?

Am I correct in thinking that that Python doesn't have a direct equivalent for Perl's __END__?
print "Perl...\n";
__END__
End of code. I can put anything I want here.
One thought that occurred to me was to use a triple-quoted string. Is there a better way to achieve this in Python?
print "Python..."
"""
End of code. I can put anything I want here.
"""
The __END__ block in perl dates from a time when programmers had to work with data from the outside world and liked to keep examples of it in the program itself.
Hard to imagine I know.
It was useful for example if you had a moving target like a hardware log file with mutating messages due to firmware updates where you wanted to compare old and new versions of the line or keep notes not strictly related to the programs operations ("Code seems slow on day x of month every month") or as mentioned above a reference set of data to run the program against. Telcos are an example of an industry where this was a frequent requirement.
Lastly Python's cult like restrictiveness seems to have a real and tiresome effect on the mindset of its advocates, if your only response to a question is "Why would you want to that when you could do X?" when X is not as useful please keep quiet++.
The triple-quote form you suggested will still create a python string, whereas Perl's parser simply ignores anything after __END__. You can't write:
"""
I can put anything in here...
Anything!
"""
import os
os.system("rm -rf /")
Comments are more suitable in my opinion.
#__END__
#Whatever I write here will be ignored
#Woohoo !
What you're asking for does not exist.
Proof: http://www.mail-archive.com/python-list#python.org/msg156396.html
A simple solution is to escape any " as \" and do a normal multi line string -- see official docs: http://docs.python.org/tutorial/introduction.html#strings
( Also, atexit doesn't work: http://www.mail-archive.com/python-list#python.org/msg156364.html )
Hm, what about sys.exit(0) ? (assuming you do import sys above it, of course)
As to why it would useful, sometimes I sit down to do a substantial rewrite of something and want to mark my "good up to this point" place.
By using sys.exit(0) in a temporary manner, I know nothing below that point will get executed, therefore if there's a problem (e.g., server error) I know it had to be above that point.
I like it slightly better than commenting out the rest of the file, just because there are more chances to make a mistake and uncomment something (stray key press at beginning of line), and also because it seems better to insert 1 line (which will later be removed), than to modify X-many lines which will then have to be un-modified later.
But yeah, this is splitting hairs; commenting works great too... assuming your editor supports easily commenting out a region, of course; if not, sys.exit(0) all the way!
I use __END__ all the time for multiples of the reasons given. I've been doing it for so long now that I put it (usually preceded by an exit('0');), along with BEGIN {} / END{} routines, in by force-of-habit. It is a shame that Python doesn't have an equivalent, but I just comment-out the lines at the bottom: extraneous, but that's about what you get with one way to rule them all languages.
Python does not have a direct equivalent to this.
Why do you want it? It doesn't sound like a really great thing to have when there are more consistent ways like putting the text at the end as comments (that's how we include arbitrary text in Python source files. Triple quoted strings are for making multi-line strings, not for non-code-related text.)
Your editor should be able to make using many lines of comments easy for you.

Categories

Resources