2014-01-25 6 views
0

텔넷을 통해 원격 서버에 연결해야합니다. 서버에 인증하려면 100 개의 질문과 같은 대답을해야합니다. 그래서 나는이 작업을 파이썬에서 telnetlib을 사용하여 자동화하려고 시도했지만 어떤 메시지도 리턴하지 않고 프롬프트가 멈춘다. 대답은 내가 다음 질문까지 얻을 맞다면 여기 python으로 telnet에 quizz를 로그인으로 연결 하시겠습니까?

내가

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

후 나는 질문을 얻고이 메시지를 얻을 명령 줄 프롬프트에서

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() 

을 한 것입니다 나는 100을 마쳤다. 그러나 파이썬 프로그램에서 나는 메시지도 질문도받지 못했다! 어떻게해야할까요?

편집 :

텔넷에 대한 디버그 수준을 설정 한 후, I는 서버의 답변을 얻을. 왜 그런지 설명해 주시겠습니까?

tn.set_debuglevel(9) 

답변

2

이 (Nmap에서 ncat) netcat을을 사용하여 가짜 텔넷 서버입니다 : 포트 9000에

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

목록을하고 msg.txt (질문)라는 이름의 파일을 전달하고 (log.txt에 입력 로그 답변), 서버를 시뮬레이트해야합니다.

파일 msg.txt 내용 :

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

16 진수로 파일 내용이 (hexdump msg.txt 사용) : 새로운 라인 문자, 그것은 \x0A 또는 \n

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;             |    | 

통지 (그것은 또한 \x0D\x0A 수 있습니다 또는 \n\r).

클라이언트 :

$ 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; 
$ 

넣어 :

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

서버 측에서 로그 파일 내용을 덤프 :

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() 

지금 테스트는 클라이언트 측에서, 할 수 있습니다 함께하면 아이디어를 얻어야합니다.