1

I am trying to automate generic telnet connections. I am relying heavily on REGEX to handle different login prompts. Currently, I am using the regex [Ll]ogin, but the prompt causing me problems is the standard Ubuntu prompt:

b-davis login: 
Password:
Last login: Mon Aug 29 20:28:24 EDT 2016 from localhost on pts/5

Because the word login is mentioned twice. Now the regex [Ll]ogin.{0,3}$ I thought should solve this, but it stopped matching all together. I tried something simpler, [Ll]ogin. which should produce the same results as [Ll]ogin but it doesn't!

I am using bytestrings because python throws a TypeError if I don't. I feel the problem lies somewhere not regex related, so here is the entire piece of code:

import telnetlib
import re
pw = "p@ssw0rd"
user = "bdavis"
regex = [
    b"[Ll]ogin.",  # b"[Ll]ogin" works here
    b"[Pp]assword",
    b'>',
    b'#']

tn = telnetlib.Telnet("1.2.3.4")

while type(tn.get_socket()) is not int:
    result = tn.expect(regex, 5)  # Retrieve send information
    s = result[2].decode()

    if re.search("[Ll]ogin$",s) is not None:
        print("Do login stuff")
        print(result)
        tn.write((user + "\n").encode())  # Send Username
    elif re.search("[Pp]assword",s) is not None:
        print("Do password stuff")
        tn.write((pw + "\n").encode())  # Send Password
    elif re.search('>',s) is not None:
        print("Do Cisco User Stuff")
        tn.write(b"exit\n")  # exit telnet
        tn.close()
    elif re.search('#',s) is not None:
        print("Do Cisco Admin Stuff")
    else:
        print("I Don't understand this, help me:")
        print(s)
        tn.close()
Rudedog9d
  • 61
  • 3
  • 10

1 Answers1

0

I think the following line:

if re.search("[Ll]ogin$",s) is not None:

should be replaced with:

if re.search("[Ll]ogin", s) is not None:  # NOTE: No `$`

OR using simple string operations:

if 'login' in s.lower():

because the matched part will not ends with ogin because of :.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • I apologize, it was last and I made a type. I know that `"[Ll]ogin$"` will not work, what I MEANT (and described in the question) is that `"[Ll]ogin."` does not work. The dot should represent any character (except newline) but it does not act this way – Rudedog9d Aug 31 '16 at 20:01
  • To follow up, ended up implementing that regex later (if the `if-elif` section) because `Telnetlib.expect` would not do what I needed/expected(). – Rudedog9d Aug 31 '16 at 20:30
  • @Rudedog9d, Did you try to change as I suggested in the answer (changing `if ..` part)? BTW, if you just want to run a command, using ssh will be easier. – falsetru Sep 01 '16 at 00:06
  • Yes, both of your suggestions were things I had tried previously. The touble is with the login prompt, once logged in some Linux Banners will display "Last Login: Sun Sep 18..." Also, I kow ssh would be easier, but it is not supported by the device in this case :( – Rudedog9d Sep 18 '16 at 14:58
  • I don't totally remember what I ended up doing at the time, but I the final product would have uses `s.lower()`. Now that I know a bit more python, I believe this is the best option! (for future reference) – Rudedog9d Jan 11 '17 at 14:34