telnetlib python example

后端 未结 6 1820
走了就别回头了
走了就别回头了 2020-12-08 05:26

So I\'m trying this really simple example given by the python docs:

import getpass
import sys
import telnetlib

HOST = \"\"
user = raw_input(\         


        
6条回答
  •  一整个雨季
    2020-12-08 06:04

    I don't have a telnet server to test against, but I think the issue is that you are not reading server responses up to the prompt, after each command you write.

    PROMPT = ':~$'
    tn = telnetlib.Telnet(HOST)
    tn.read_until('login: ')
    tn.write(user + '\n')
    if password:
       tn.read_until('Password: ')
       tn.write(password + '\n')
    tn.read_until(PROMPT)
    tn.write('ls\n')
    print tn.read_until(PROMPT)
    tn.write('exit\n')
    

    btw, telnetnetlib can be tricky and things varies depending on your FTP server and environment setup. you might be better off looking into something like pexpect to automate login and user interaction over telnet.

提交回复
热议问题