gpt4 book ai didi

python - 如何在 Linux 下 ssh.invoke_shell() 工作

转载 作者:太空宇宙 更新时间:2023-11-04 05:17:02 29 4
gpt4 key购买 nike

import paramiko
import os
import sys

#Data conection
ssh_servidor = 'xxx.xx.xx.xxx'
ssh_usuario = 'yyy'
ssh_clave = 'pws'
ssh_puerto = yy

# Conect to the server
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Open a server session
ssh.connect(ssh_servidor, ssh_puerto, ssh_usuario, ssh_clave)
channel = ssh.invoke_shell()
channel.send('clear')
channel.send('\n')
channel_data = str()

while True:

if channel.recv_ready():
enter code herechannel_data += channel.recv(9999)
print '############ Device Ouput ##############'
print channel_data
print '########################################'

else:
continue

if channel_data.endswith('xxxx@yyyyy~$'):
channel.send('ls | grep mmmm')
channel.send('\n')

问题是理论上 varchannel_data 必须有并打印这:

############ Device Ouput ##############
Linux tito 2.6.32-5-amd64 #1 SMP Fri May 10 08:43:19 UTC 2013 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Dec 14 10:01:15 2017 from .xx.xx.xxx

xxxx@yyyyy~$
########################################

为此,它打印出以下内容:

############ Device Ouput ##############
Linux tito 2.6.32-5-amd64 #1 SMP Fri May 10 08:43:19 UTC 2013 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Dec 14 10:01:15 2017 from .xx.xx.xxx

########################################

主机“xxxx@yyyyy~$”从未出现过,所以我要求时的代码部分:

if channel_data.endswith('xxxx@yyyyy~$'):

这从未发生过。

与我看到的 paramiko 库示例的唯一区别是代码可以在 Windows 上运行。

那么问题是,此时 win 和 Linux 之间有什么区别?

为什么 S.O.不显示主机 xxxx@yyyyy~$

有什么建议吗?

最佳答案

我找到了答案:

channel_bytes=channel.recv(9999)

channel_data = channel_bytes.decode("utf-8")

否则,人们会遇到使用字节(来自缓冲区)作为字符串的情况。

谢谢大家!!

关于python - 如何在 Linux 下 ssh.invoke_shell() 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47814725/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com