Dealing with Junk Values in Remote SSH CLI Output via Paramiko
While using Python's Paramiko library for SSH connections and output retrieval from a remote machine's CLI, users may encounter unwanted junk values interspersed with the desired output. These values, often in the form of ANSI escape codes like "\x1b[2J\x1b[1;1H", can clutter the output and hinder its usability.
Understanding the Source of Junk Values
Contrary to the assumption that they are junk, these escape codes are vital for proper output display in a terminal environment.他们向终端提供有关如何格式化文本,移动光标等的说明。但是,如果您不使用交互式终端,这些代码可能会出现问题。
解决方案:默认情况下,paramiko的sshclient.invoke_shell方法启动互动的终端会话,引导severse severse toses to to nose cope neve copes neve copes。为了消除这一点,请切换到exec_command方法,该方法在没有伪终端的情况下运行命令(除非明确启用):[&& &&&&华 output = re.sub(r'(\ x1b \ [。这对于处理输出中的非ASCII字符是必不可少的。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3