telnet求助

本帖最后由 sinxadmin 于 2011-06-03 14:18 编辑

大家好!
  1. import getpass
  2. import time
  3. import sys
  4. import telnetlib

  5. HOST = "localhost"
  6. PORT = 1981
  7. user = raw_input("Enter your remote account: ")
  8. password = getpass.getpass()
  9. tn = telnetlib.Telnet(HOST,PORT)
  10. try:
  11.         tn.read_until("username: ")
  12. except EOFError:
  13.         print 'username:error'
  14. tn.write(user + '\n')
  15. if password:
  16.     try:
  17.         tn.read_until("password: ",30)
  18.     except EOFError:
  19.         print 'password:error'
  20.     print password
  21.     tn.write(password+'\n')

  22. tn.write("help\n")
  23. tn.write("exit\n")

  24. print tn.read_all()
复制代码
# python telnet.py
Enter your remote account: admin
Password:
password:error
123456

tn.read_until("password: ",30)这里报的异常,是tn.write(user + '\n')没有发送成功吗?

>telnet localhost 1981
username: admin
password:

[]#

作者: sinxadmin   发布时间: 2011-06-03

用\r 替换\n可以么?

作者: oychw   发布时间: 2011-06-07