简体   繁体   中英

forking ssh using python paramiko

I am using the python module Paramiko to ssh to a linux box and execute two C programs. Program1 generates a signal on a DAQ device on a trigger. It waits for the trigger and terminates in 5 seconds. Program2 generates the trigger.

Here is my Python class:

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. Is there a way I can run program2 faster than program1 is done? 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") . This way you wouldn't be paying twice for all the set-up and tear-down that you do test.ssh .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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