相关疑难解决方法(0)

Paramiko ssh 死/挂,输出大

我尝试使用 Paramiko 和 ssh 来备份服务器以调用tar命令。当文件数量有限时,一切正常,但当文件夹很大时,脚本会无休止地等待。下面的测试告诉我问题出在标准输出的大小上。

有没有办法纠正它并执行这种命令?

案例大输出:

query = 'cd /;ls -lshAR -h'
chan.exec_command(query)
while not chan.recv_exit_status():
    if chan.recv_ready():
        data = chan.recv(1024)
        while data:
            print data
            data = chan.recv(1024)

    if chan.recv_stderr_ready():
        error_buff = chan.recv_stderr(1024)
        while error_buff:
            print error_buff
            error_buff = chan.recv_stderr(1024)
    exist_status = chan.recv_exit_status()
    if 0 == exist_status:
        break
Run Code Online (Sandbox Code Playgroud)

结果是(不行 - 阻止 - 死??)

query = 'cd /;ls -lshAR -h'
chan.exec_command(query)
while not chan.recv_exit_status():
    if chan.recv_ready():
        data = chan.recv(1024)
        while data:
            print data
            data = chan.recv(1024)

    if chan.recv_stderr_ready(): …
Run Code Online (Sandbox Code Playgroud)

python ssh paramiko

5
推荐指数
1
解决办法
2234
查看次数

Paramiko:从远程执行命令的标准输出读取

所以我正在使用paramiko进行一些基本的SSH测试,我没有得到任何输出到stdout.继承我的代码.

import paramiko
client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
com="ls ~/desktop"
client.connect('MyIPAddress',MyPortNumber, username='username', password='password')
output=""
stdin, stdout, stderr = client.exec_command(com)

print "ssh succuessful. Closing connection"
client.close()
print "Connection closed"
stdout=stdout.readlines()
print stdout
print com
for line in stdout:
    output=output+line
if output!="":
    print output
else:
    print "There was no output for this command"
Run Code Online (Sandbox Code Playgroud)

所以每当我运行它时,执行该命令(如果我执行类似cp的操作,文件被复制),但我总是得到"此命令没有输出".当打印stdout = stdout.readlines()时,[]是输出.另外,如果我在for循环中添加一个print语句,它就永远不会运行.有人可以帮帮我吗?谢谢!

python paramiko

4
推荐指数
2
解决办法
4万
查看次数

在后台运行进程

我发现使用paramiko很难在后台运行一个进程.我用了 :

stdin, stdout, stderr = ssh.exec_command('executefile.py &') 
Run Code Online (Sandbox Code Playgroud)

并发现没有发现executefile.py进程正在运行.

然后我尝试使用其他方式包括反斜杠:

stdin, stdout, stderr = ssh.exec_command('executefile.py \&') 
Run Code Online (Sandbox Code Playgroud)

这种方法奏效了.有一个实例在机器上运行,但毫不奇怪,它没有在后台运行.我可以知道因为它没有在后台运行,因为代码在此代码之后停留在第二行.它是

all_inf = stdout.readlines()
Run Code Online (Sandbox Code Playgroud)

现在代码不会超出上面的行,除非脚本的进程被杀死.

我正在学习paramiko,任何帮助表示赞赏.

paramiko

4
推荐指数
3
解决办法
9737
查看次数

python paramiko等待完成执行命令

我在paramiko中写了这段代码:

def TryExecute(hostname='192.168.1.1', user='root', passwd='root'):
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(hostname, username=user, password=passwd, timeout=3)
    #stdin, stdout, stderr =  ssh.exec_command("uname -a")

    session = ssh.invoke_shell()
    session.send("\n")

    session.send("echo step 1\n")
    time.sleep(1)

    session.send("sleep 30\n")
    time.sleep(1)

    while not session.recv_ready():
        time.wait(2)

    output = session.recv(65535)

    session.send("echo step 2\n")
    time.sleep(1)

    output += session.recv(65535)
Run Code Online (Sandbox Code Playgroud)

我尝试在我的linux服务器上执行更多命令,问题是我的python代码不等待完成执行命令,例如,如果我尝试执行"sleep 30",则python不等待30秒完成执行命令,怎么解决这个问题?我试试白衣,而recv_ready()蝙蝠不等待:(

python paramiko

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

paramiko ×4

python ×3

ssh ×1