【问题标题】:Receiveing Output of Command from Paramiko - Python从 Paramiko 接收命令输出 - Python
【发布时间】:2016-03-19 00:06:01
【问题描述】:

我正在创建一个 python 脚本,它通过 SSH 连接到一个运行一些命令的 linux 机器中。我将 paramiko 用于 ssh 功能。

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ipadd, username='username', password='password')
chan = ssh.invoke_shell()

chan.send('find / -name "test"')
time.sleep(1)
resp = chan.recv(9999)
print resp

chan.send('find / -name "test1"')
time.sleep(1)
resp = chan.recv(9999)
print resp

命令运行成功,但是当我想接收输出时(使用 chan.recv,它返回以下内容:

find / -name "test"
find / -name "test2"

我相信我应该(而且我需要)将 find cmd 的输出作为字符串接收。

/data/test
/data/test2

【问题讨论】:

标签: python ssh paramiko


【解决方案1】:

看来我做错了两件事。首先,

resp = chan.recv(9999)

应该是

resp += chan.recv(9999)

其次,我需要在请求回复之前发送一个新行

channel.send("\n")

【讨论】:

    猜你喜欢
    • 2021-05-05
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 2012-06-29
    • 2023-03-12
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多