Python telnetlib read

编程入门 行业动态 更新时间:2024-10-23 06:17:35
本文介绍了Python telnetlib read_until返回截断字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

"read_until";函数使用长命令返回截断的字符串. 这些命令可以完美运行,但不会显示全文. 我怎样才能解决这个问题?请帮忙.

"read_until" function returns cut off string with long commands. The commands ran perfectly, but it does not show full texts. How can I fix this? Please help.

#我的代码

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'this is 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):接收'\ x08 \ 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 [root @ RHEL6-5 tmp]#'

Telnet(192.168.220.4,23): send 'echo "this is a long command for the test purpose... > test.txt"\n' Telnet(192.168.220.4,23): recv 'echo "this is a l' Telnet(192.168.220.4,23): recv 'ong command for the test purpose... > te\r\x00<his is ' Telnet(192.168.220.4,23): recv 'a long command for the test purpose... > 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\nthis is a long command for the test purpose... >' Telnet(192.168.220.4,23): recv ' test.txt\r\n[root@RHEL6-5 tmp]# '

#个实际输出

<出于测试目的,他是一个很长的命令...> test.txt" 对于测试目的,这是一个很长的命令...> test.txt [root @ RHEL6-5 tmp]#

<his is a long command for the test purpose... > test.txt" this is a long command for the test purpose... > test.txt [root@RHEL6-5 tmp]#

推荐答案

该问题似乎是由于换行引起的. 我找到了一种解决方案,将窗口大小的宽度设置为接近最大值,可以接收长行而无需telnet服务器应用换行.

The issue appears to be occurring due to line-wrapping. I found a solution where setting the window size width to near the max value allows long lines to be received without line-wrapping applied by the telnet server.

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

(See the Window Size Option RFC for details on setting the option www.ietf/rfc/rfc1073.txt )

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 www.ietf/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)

更多推荐

Python telnetlib read

本文发布于:2023-08-07 13:52:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319403.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   telnetlib   read

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!