鱼C论坛

 找回密码
 立即注册
查看: 4453|回复: 2

[已解决]Telnet无法输出任何内容

[复制链接]
发表于 2016-5-30 16:40:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 syother 于 2016-5-30 16:42 编辑

Telnet无法输出任何内容




  1. >>> def te():
  2. ...     from telnetlib import Telnet
  3. ...     tn = Telnet('11.28.27.2')
  4. ...     tn.read_until('Username:')
  5. ...     tn.write('admin\n')
  6. ...     tn.read_until('Password:')
  7. ...     tn.write('111111\n')
  8. ...     tn.write('ping 11.28.27.76 \n')
  9. ...     tn.write('exit \n')
  10. ...     print tn.read_all()
  11. ...
  12. >>> te()
复制代码


在2的设备上已看到登录的地址了,但执行的Ping操作结果无法输出
最佳答案
2016-6-2 21:29:39
  1. import getpass
  2. import telnetlib

  3. HOST = "localhost"
  4. user = input("Enter your remote account: ")
  5. password = getpass.getpass()

  6. tn = telnetlib.Telnet(HOST)

  7. tn.read_until(b"login: ")
  8. tn.write(user.encode('ascii') + b"\n")
  9. if password:
  10.     tn.read_until(b"Password: ")
  11.     tn.write(password.encode('ascii') + b"\n")

  12. tn.write(b"ls\n")
  13. tn.write(b"exit\n")

  14. print(tn.read_all().decode('ascii'))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-6-2 21:29:39 | 显示全部楼层    本楼为最佳答案   
  1. import getpass
  2. import telnetlib

  3. HOST = "localhost"
  4. user = input("Enter your remote account: ")
  5. password = getpass.getpass()

  6. tn = telnetlib.Telnet(HOST)

  7. tn.read_until(b"login: ")
  8. tn.write(user.encode('ascii') + b"\n")
  9. if password:
  10.     tn.read_until(b"Password: ")
  11.     tn.write(password.encode('ascii') + b"\n")

  12. tn.write(b"ls\n")
  13. tn.write(b"exit\n")

  14. print(tn.read_all().decode('ascii'))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-21 17:42:46 | 显示全部楼层
python3.5.1 版本,如下代码运行:
import getpass
import telnetlib

HOST = input("输入远程服务器的IP地址:")
user = input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until("password: ")
    tn.write(password.encode('ascii') + "\n")

tn.write("show ver\n")
tn.write("exit\n")

print(tn.read_all().decode('ascii'))

运行出错如下:


Warning (from warnings module):
  File "C:\Python35-32\lib\getpass.py", line 101
    return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Password: hillstone
Traceback (most recent call last):
  File "F:/book/PYTHON/视频笔记/小实验/telnet3.py", line 10, in <module>
    tn.read_until("login: ")
  File "C:\Python35-32\lib\telnetlib.py", line 302, in read_until
    i = self.cookedq.find(match)
TypeError: a bytes-like object is required, not 'str'
>>>

请高手帮忙看看代码,多谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 22:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表