If you take a look into the python 'telnetlib.py' code, you'll see a method called 'expect()'. You might give it a try to see if it will help you get the job done. Keep in mind that it's expecting a list of regex, so design your search strings accordingly.
Since you're using telnetlib, if you haven't already stumbled on this tip yet (I sure used telnetlib a long time without noticing it) try setting the telnet.debuglevel to non-zero. It may help you in snooping the low-level data traffic.
Edit:
Well, I found myself with some time on my hands, so I was able to create and test some demo code. First off, just know that as a beginner, you've chosen to dive into a "deeper" end of the coding pool by playing with telnetlib and expect. They're not hard, but they are tedious due to the arcana of both telnet and the "art" of screen-scraping. Not for the weak of heart.
So here's my cut. I tested this on one of our local servers and was able to change to a invalid password to verify that the code did detect a "Login failed" condition. You may very well need to tweak this code for your application.
#!/usr/bin/python3
import telnetlib
TARGET_NAME = "<insert yours here>"
USER_NAME = "<insert yours here>"
USER_PW = "<insert yours here>"
session = telnetlib.Telnet(host=TARGET_NAME)
session.debuglevel = 1
session.write("\n\n".encode('ascii'))
index, match_obj, text = session.expect(["login: ".encode('ascii')])
if match_obj:
print("DBG: Sending user name")
session.write((USER_NAME + '\r').encode('ascii'))
index, match_obj, text = session.expect(["Password:".encode('ascii')])
if match_obj:
print("DBG: Sending password")
session.write((USER_PW + '\r').encode('ascii'))
print("Checking for failed login")
index, match_obj, text = session.expect(["Login incorrect".encode('ascii')], timeout=3)
if match_obj:
print("Login failed")
else:
print("Well, at least we didn't see, 'Login failed'")
To better understand what's going on here, take a look at the telnetlib source code. You'll notice that the expect() method returns three values: the index of where the text was found, the match object and the text read up to that point. I took advantage of previous regex experience that match objects will evaluate to a boolean based on whether a match was found or not. You might want to use some other combinations of the returned values to determine what exactly the expect call found. Again, tedious, but not difficult.