Search code examples
pythontelnet

Python Telnet if read = xxxx


I'm having a small issue in Python with Telnet. im trying to have the script start up by determining whether the telnet connection is already logged in or not. My code looks something like the following:

if TLNT.read_until("login: ",2): #if it reads the login prompt:
    login(USER,PASS) #runs the function used to login to the system
main() #otherwise will determine system is already logged in and start the main script

The Issue here I am unsure of the formatting here, as the script will skip over the login function even though i have purposely logged out the telnet system so i know that it sees the login prompt. if the telnet prompt is left at "login: " what will read_until return?


Solution

  • The answer is in the documentation!

    Telnet.read_until(expected[, timeout])

    Read until a given string, expected, is encountered or until timeout seconds have passed.

    When no match is found, return whatever is available instead, possibly the empty string. Raise EOFError if the connection is closed and no cooked data is available.

    It does not return True or False, so you probably don't want to use the return value as your conditional expression.