connect to telnet in python with a quizz as login?
Question: I need to connect to a remote server via telnet. To authenticate to the server
I have to answer like a 100 questions. So I tried to automate this task in
python using telnetlib but the prompt halts without returning any message.

here is what I did

    
    
    import telnetlib 
    
    port = 2002
    host = "23.23.190.204"
    
    tn = telnetlib.Telnet(host, port)
    tn.read_until("""
    Welcome to EULER!
                     =================
                                      Answer 100 simple questions to authenticate yourself
    """)
    print tn.read_all()
    tn.close()
    

in the command line prompt I get this message

    
    
    Welcome to EULER!
    =================
    Answer 100 simple questions to authenticate yourself
    

then I am getting asked a question if the answer is correct I get the next
question till I finished the 100. But in the python program I not getting
neither the message nor the questions! what to do?

**EDIT:**

after setting the debug level for telnet, I get the answer of the server.
Could you please explain why is that?

    
    
    tn.set_debuglevel(9)
    


Answer: This is a fake telnet server using netcat (`ncat` from
[Nmap](http://nmap.org/)):

    
    
    $ ncat -l 9000 < msg.txt > log.txt
    

listing on port 9000 and passes a file named `msg.txt` (the questions) and log
the input into `log.txt` (the answers), it should simulate your server.

The file `msg.txt` content:

    
    
    Welcome to EULER!
    =================
    Answer 100 simple questions to authenticate yourself
    What is your name?
    How old are you?
    Do you use Python?
    

the file content in hex (using `hexdump msg.txt`):

    
    
    00000000: 0A 57 65 6C 63 6F 6D 65 20 74 6F 20 45 55 4C 45 | Welcome to EULE|
    00000010: 52 21 0A 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D 3D |R! =============|
    00000020: 3D 3D 3D 3D 0A 41 6E 73 77 65 72 20 31 30 30 20 |==== Answer 100 |
    00000030: 73 69 6D 70 6C 65 20 71 75 65 73 74 69 6F 6E 73 |simple questions|
    00000040: 20 74 6F 20 61 75 74 68 65 6E 74 69 63 61 74 65 | to authenticate|
    00000050: 20 79 6F 75 72 73 65 6C 66 0A 57 68 61 74 20 69 | yourself What i|
    00000060: 73 20 79 6F 75 72 20 6E 61 6D 65 3F 0A 48 6F 77 |s your name? How|
    00000070: 20 6F 6C 64 20 61 72 65 20 79 6F 75 3F 0A 44 6F | old are you? Do|
    00000080: 20 79 6F 75 20 75 73 65 20 50 79 74 68 6F 6E 3F | you use Python?|
    00000090: 0A                                              |                |
    00000091;                                                 |                |
    

notice the new line character, it's `\x0A` or `\n` (it can also be `\x0D\x0A`
or `\n\r`).

The client:

    
    
    import telnetlib 
    
    port = 9000
    host = "127.0.0.1"
    
    tn = telnetlib.Telnet(host, port)
    r = tn.read_until("""\nWelcome to EULER!
    =================
    Answer 100 simple questions to authenticate yourself\n""")
    
    tn.read_until("What is your name?\n")
    tn.write("foo\n") # The client sends `\n`, notice the server may expects `\n\r`.
    print("Question 1 answered.")
    
    tn.read_until("How old are you?\n")
    tn.write("100\n")
    print("Question 2 answered.")
    
    tn.read_until("Do you use Python?\n")
    tn.write("yep\n")
    print("Question 3 answered.")
    
    tn.close()
    

now lets test it, on the client side:

    
    
    $ python client.py
    Question 1 answered.
    Question 2 answered.
    Question 3 answered.
    $
    

on the server side, dump the log file content:

    
    
    $ ncat -l 9000 < msg.txt > log.txt
    $
    $ cat log.txt # or `type log.txt` on windows
    foo
    100
    yep
    
    $
    $ hexdump log.txt
    00000000: 66 6F 6F 0A 31 30 30 0A 79 65 70 0A             |foo 100 yep |
    0000000c;
    $
    

put it together you should get the idea.

