Python telnetlib.expect() dot character '.' not working as expected
<p>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  <code>[Ll]ogin</code>, but the prompt causing me problems is the standard Ubuntu prompt:</p>

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

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

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

<pre><code>import telnetlib
import re
pw = "p@ssw0rd"
user = "bdavis"
regex = [
    b"[Ll]ogin.",  # b"[Ll]ogin" works here
    b"[Pp]assword",
    b'&gt;',
    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('&gt;',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()
</code></pre>

<p>I think the following line:</p>

<pre><code>if re.search("[Ll]ogin$",s) is not None:
</code></pre>

<p>should be replaced with:</p>

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

<p>OR using simple string operations:</p>

<pre><code>if 'login' in s.lower():
</code></pre>

<p>because the matched part will not ends with <code>ogin</code> because of <code>:</code>.</p>

python regex telnet telnetlib