Python Telnet 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4528831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:09:05  来源:igfitidea点击:

Python Telnet connection

pythontelnet

提问by Martin

I was playing around with python 3.1 when I came to a problem. I want to connect to a telnet server. Here is my code:

当我遇到问题时,我正在玩 python 3.1。我想连接到 telnet 服务器。这是我的代码:

import sys
import telnetlib

tn = telnetlib.Telnet("10.0.0.138")


tn.read_until(b"Username :", 2)
tn.write(b"\n")

tn.read_until(b"Password :", 2)
tn.write(b"\n")

tn.read_until(b"=>", 2)
tn.write(b"exit\n")

tn.close

It works to read until "Username :". There is also no error message when writing an emty line. But when i read until "Password :" i get an empty string. I also get an empty string when i read all.

它可以一直读到“用户名:”。写入空行时也没有错误消息。但是当我读到“密码:”时,我得到一个空字符串。当我阅读所有内容时,我也会得到一个空字符串。

Please help me if you can.

如果可以,请你帮助我。

EDIT: Here is the output when i connect to the server via putty.

编辑:这是我通过腻子连接到服务器时的输出。

 Willkommen am THOMSON TG787v
   Plattform:VDNT-D  Firmware:8.2.5.0  Seriennummer:CP0919MT238
 Bitte identifizieren Sie sich mit Ihrem Benutzernamen und Kennwort
--------------------------------------------------------------------------------




Username :
Password :
------------------------------------------------------------------------

                             ______  Thomson TG787v
                         ___/_____/\
                        /         /\  8.2.5.0
                  _____/__       /  \
                _/       /\_____/___ \  Copyright (c) 1999-2009, THOMSON
               //       /  \       /\ \
       _______//_______/    \     / _\/______
      /      / \       \    /    / /        /\
   __/      /   \       \  /    / /        / _\__
  / /      /     \_______\/    / /        / /   /\
 /_/______/___________________/ /________/ /___/  \
 \ \      \    ___________    \ \        \ \   \  /
  \_\      \  /          /\    \ \        \ \___\/
     \      \/          /  \    \ \        \  /
      \_____/          /    \    \ \________\/
           /__________/      \    \  /
           \   _____  \      /_____\/
            \ /    /\  \    /___\/
             /____/  \  \  /
             \    \  /___\/
              \____\/

------------------------------------------------------------------------
CP0919MT238=>

I pressed return after "Username :" and then after "Password :".

我在“用户名:”之后按回车,然后在“密码:”之后按回车。

采纳答案by Uku Loskit

Lol, i had pretty much the same router as you.

大声笑,我和你的路由器几乎一样。

Try this, bit of my old code:

试试这个,我的旧代码:

tn = telnetlib.Telnet(HOST)

tn.read_until('Username : ')

tn.write(user+ "\r")

tn.read_until("Password : ")

tn.write(password+ "\n")

tn.write("\r")

This is for Python 2, but try just adding the extra space after the semicolon. Also, if this does not work, use wireshark and see what the putty connection is doing and correct your code to match.

这是针对 Python 2 的,但请尝试在分号后添加额外的空格。另外,如果这不起作用,请使用wireshark并查看腻子连接正在做什么并更正您的代码以匹配。

回答by thotheolh

The docs in this link: http://docs.python.org/library/telnetlib.html

此链接中的文档:http: //docs.python.org/library/telnetlib.html

It has a sample code at the end under the section "Telnet Example".

它在“Telnet 示例”部分的末尾有一个示例代码。

You can access the example via: http://docs.python.org/library/telnetlib.html#telnet-example

您可以通过以下方式访问该示例:http: //docs.python.org/library/telnetlib.html#telnet-example

回答by Ankit Tiwari

# Script to Telnet in to a host
# For now I have hardcoded the HOST that can be taken as input if required
#run as " python teli.py ""

import time
import telnetlib
HOST ="www.google.com"
tn=telnetlib.Telnet(HOST,"80")
tn.write("GET /index.html HTTP/1.1\nHost:"+HOST+"\n\n")
l=tn.read_all()
print l

回答by M Siddharth

The simple python telnet program below will definitely work on python3.

下面这个简单的python telnet 程序肯定可以在python3 上运行。

import telnetlib
import sys
HOST = input("enter your host IP:")
tn=telnetlib.Telnet(HOST)
tn.write(b'admin\n')
tn.write(b'admin123\n')
tn.write(b"enable\n")
tn.write(b"cisco\n")
tn.write(b"conf t \n")
tn.write(b"hostname SIDDHARTH \n")
tn.write(b'interface loopback0\n')
tn.write(b'ip address 1.1.1.1 255.255.255.255\n')
tn.write(b'interface loopback1\n')
tn.write(b'ip address 2.2.2.2 255.255.255.255\n')
tn.write(b"end \n")
tn.write(b"exit \n")
x = tn.read_all()
print (x)