简体   繁体   English

使用python paramiko分叉ssh

[英]forking ssh using python paramiko

I am using the python module Paramiko to ssh to a linux box and execute two C programs. 我正在使用python模块Para​​miko ssh到linux box并执行两个C程序。 Program1 generates a signal on a DAQ device on a trigger. 程序1在触发器上在DAQ设备上生成信号。 It waits for the trigger and terminates in 5 seconds. 它等待触发,并在5秒钟后终止。 Program2 generates the trigger. 程序2生成触发器。

Here is my Python class: 这是我的Python类:

class test:
    server = "localhost"
    username = "root"
    password = "12345"
    def ssh (self, cmd = ""):
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(self.server,username=self.username,password=self.password)
            stdin, stdout, stderr = ssh.exec_command(cmd)
            result = stdout.read().splitlines()
            if len(result)!= 0:
                return result
            else:
                return -1

test.ssh("program1")
test.ssh("program2")

The problem is that program2 is being executed after program1 has been already terminated, so it doesn't generate anything. 问题是在终止program1之后正在执行program2,因此它不会生成任何东西。 Is there a way I can run program2 faster than program1 is done? 有没有一种方法可以比program1更快地运行program2? I have tried 我努力了

test.ssh("program1 &")
test.ssh("program2") 

but to no avail. 但无济于事。 If I run these programs manually in two terminal shells, then it works fine. 如果我在两个终端外壳中手动运行这些程序,则可以正常工作。 Any thoughts? 有什么想法吗?

Can use threading or multiprocessing to execute the both the programs in different sessions 可以使用线程或多处理来在不同的会话中执行这两个程序

import multiprocessing

input = ["program1","program2"]   

for i in range(2):
    p = mutiprocessing.Process(target=test.ssh,args=(input[i],))
    p.start()
    processlist.append(p)

for p in processlist:
    p.join()

You could try running them in the same SSH session: test.ssh("program1 & program2") . 您可以尝试在同一SSH会话中运行它们: test.ssh("program1 & program2") This way you wouldn't be paying twice for all the set-up and tear-down that you do test.ssh . 这样,您将不需要为test.ssh所有设置和拆卸支付两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM