首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python read_until返回断线字符串

Python read_until返回断线字符串
EN

Stack Overflow用户
提问于 2016-07-10 05:27:57
回答 2查看 4.1K关注 0票数 2

Python telnetlib read_until recv问题

"read_until“函数使用长命令返回断线字符串。

这些命令运行得很完美,但没有显示完整的文本。

我怎么才能解决这个问题?请帮帮忙。

#我的代码

代码语言:javascript
运行
复制
tn = telnetlib.Telnet(ip, 23, 5)
prompt = ']# '
tn.write('echo "this is a long command for the test purpose... > test.txt"\n')
print tn.read_until(prompt, 1)

#调试输出

Telnet(192.168.220.4,23):发送'echo‘这是一个用于测试目的的长命令.> test.txt"\n’

Telnet(192.168.220.4,23):recv 'echo‘这是一个l’

Telnet(192.168.220.4,23):用于测试目的的recv 'ong命令.> te\r\x00

Telnet(192.168.220.4,23):recv‘用于测试目的的长命令.> tes’

Telnet(192.168.220.4,23):recv‘\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x0 8t.txt’

Telnet(192.168.220.4,23):recv‘\r\n这是一个用于测试的长命令.>‘

Telnet(192.168.220.4,23):recv‘test.txt\r\n nroot@RHEL6 6-5 tmp#’

#实输出

test.txt“

这是一个很长的测试命令.> test.txt

根@RHEL6 6-5 tmp#

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-02 05:28:13

这个问题似乎是由于线的包装而发生的.我找到了一种解决方案,将窗口大小宽度设置为接近最大值允许接收长行,而不需要telnet服务器应用行包装。

(有关设置选项https://www.ietf.org/rfc/rfc1073.txt的详细信息,请参阅窗口大小选项RFC )

代码语言:javascript
运行
复制
import telnetlib
import struct
from telnetlib import DO, DONT, IAC, WILL, WONT, NAWS, SB, SE

MAX_WINDOW_WIDTH = 65000  # Max Value: 65535
MAX_WINDOW_HEIGHT = 5000


def set_max_window_size(tsocket, command, option):
    """
    Set Window size to resolve line width issue
    Set Windows size command: IAC SB NAWS <16-bit value> <16-bit value> IAC SE
    --> inform the Telnet server of the window width and height.
    Refer to https://www.ietf.org/rfc/rfc1073.txt
    :param tsocket: telnet socket object
    :param command: telnet Command
    :param option: telnet option
    :return: None
    """
    if option == NAWS:
        width = struct.pack('H', MAX_WINDOW_WIDTH)
        height = struct.pack('H', MAX_WINDOW_HEIGHT)
        tsocket.send(IAC + WILL + NAWS)
        tsocket.send(IAC + SB + NAWS + width + height + IAC + SE)
    # -- below code taken from telnetlib source
    elif command in (DO, DONT):
        tsocket.send(IAC + WONT + option)
    elif command in (WILL, WONT):
        tsocket.send(IAC + DONT + option)

ip = 'x.x.x.x'
tn = telnetlib.Telnet(ip, 23, timeout=5)
tn.set_option_negotiation_callback(set_max_window_size)

tn.write('echo "this is a long command for the test purpose... > test.txt"\n')

prompt = ']# '
print tn.read_until(prompt, timeout=1)
票数 3
EN

Stack Overflow用户

发布于 2019-05-07 08:59:40

为我工作。

代码语言:javascript
运行
复制
def set_max_window_size(tsocket, command, option):
"""
Set Window size to resolve line width issue
Set Windows size command: IAC SB NAWS <16-bit value> <16-bit value> IAC SE
--> inform the Telnet server of the window width and height.
Refer to https://www.ietf.org/rfc/rfc1073.txt
:param tsocket: telnet socket object
:param command: telnet Command
:param option: telnet option
:return: None
"""
if option == NAWS:
    naws_command = struct.pack('!BBBHHBB',
    255, 250, 31, # IAC SB NAWS
    50, 100, #width, height
    255, 240) # IAC SE
    tsocket.send(naws_command)
# -- below code taken from telnetlib source
elif command in (DO, DONT):
    tsocket.send(IAC + WONT + option)
elif command in (WILL, WONT):
    tsocket.send(IAC + DONT + option)

ip = 'x.x.x.x'
tn = telnetlib.Telnet(ip, 23, timeout=5)
socket = tn.get_socket()
tn.set_option_negotiation_callback(set_max_window_size)

tn.write('echo "this is a long command for the test purpose... > test.txt"\n')

prompt = ']# '
print tn.read_until(prompt, timeout=1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38288887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档