当我使用python脚本远程连接输出时,输出未正确显示格式

发布于 2025-02-13 14:13:15 字数 827 浏览 1 评论 0原文

我使用我的Python代码连接到远程服务器。 我连接并获得结果。但是结果是\ r \ n 我的代码在下面:

#To connect remote server and run networkmon.py and get results
from pexpect import pxssh
import getpass
try:
    s = pxssh.pxssh()
    hostname = "10.89.71.39"
    username = "manoadmin"
    password = "********" #generic pass added
    s.login(hostname, username, password)
    s.sendline('cat manobackup_scripts/networkmon.py')
    s.prompt()
    print(s.before)
    s.logout()
except pxssh.ExceptionPxssh as e:
    print("pxssh login hata.")

我得到的输出就像;

导入os \ r \ nimport套接字\ r \ n \ r \ r \ nimport time \ r \ n \ n \ r \ r \ nstart_time = time.time.time.time()\ r \ r \ n \ r \ r \ n

但我希望像下面一样打印出来

导入OS 导入套接字

导入时间

start_time = time.time()

I connect to remote server with ssh with my python codes.
I connect and get results. But results are coming with \r\n
My codes are below :

#To connect remote server and run networkmon.py and get results
from pexpect import pxssh
import getpass
try:
    s = pxssh.pxssh()
    hostname = "10.89.71.39"
    username = "manoadmin"
    password = "********" #generic pass added
    s.login(hostname, username, password)
    s.sendline('cat manobackup_scripts/networkmon.py')
    s.prompt()
    print(s.before)
    s.logout()
except pxssh.ExceptionPxssh as e:
    print("pxssh login hata.")

The output i get is like;

import os\r\nimport socket\r\n\r\nimport time\r\n\r\nstart_time = time.time()\r\n\r\n

But i want the out put to be printed out as below

import os
import socket

import time

start_time = time.time()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2025-02-20 14:13:15

默认情况下,s.beforebytes,而不是str。您可以在打印时解码:

print(s.before.decode() )

您可以要求它通过指定编码

s = pxssh.pxssh(encoding='utf8')
...
print(s.before)

By default data in s.before is of bytes, not str. You can decode when printing:

print(s.before.decode() )

You can ask it to automatically convert the data to str by specifying an encoding:

s = pxssh.pxssh(encoding='utf8')
...
print(s.before)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文