pythonpython-3.xparamikopysftpremote-execution

How to not wait for the output of the remote execution?


I am using pysftp for remote execution, and the thing is that I don't want to wait for the output of the remote execution, I just want to start the execution and be done with it.

import pysftp as sftp
cnopts = sftp.CnOpts()
cnopts.hostkeys = None
handle = sftp.Connection('10.0.2.10',username='kali', password='root', cnopts=cnopts)
handle.execute('/tmp/doThis')
handle.exeute('/tmp/doThat')

Now the thing is that the script is waiting for doThis to get over and then it start with doThat. I tried using '&', but it has no affect. Is there someway of doing this, or is it not possible?


Solution

  • Why don't you try the threading concept?

    import pysftp as sftp
    from threading import Thread
    cnopts = sftp.CnOpts()
    cnopts.hostkeys = None
    handle = sftp.Connection('10.0.2.10',username='kali', password='root', cnopts=cnopts)
    
    def exec(cmd):
        #handle will be taken from the prev declaration
        handle.execute(cmd)
    
    list_cmds = ['/tmp/doThis', '/tmp/doThat']
    
    for cmd in list_cmds:
        Thread(target=exec, args=[cmd]).start()