Creating multiple SSH connections at a time using Paramiko

前端 未结 3 1153
一向
一向 2020-12-05 07:47

The code below runs grep in one machine through SSH and prints the results:

import sys, os, string
import paramiko

cmd = \"grep -h \'king\' /opt/data/horror         


        
相关标签:
3条回答
  • 2020-12-05 08:21

    In my case i have to execute commands on server with one ip and port and after complete need to do sftp to other ip and different port.Condition is one connection should be live while doing sftp to another ip due to port forwarding.

    Both connection are working separably but while combining both second sftp connection is not working.

    0 讨论(0)
  • 2020-12-05 08:26

    Just run everything in a for loop, and don't forget to close stdin before moving on to next iteration. That is, after line stdin.flush() add line stdin.close()

    0 讨论(0)
  • 2020-12-05 08:32

    You'll need to put the calls into separate threads (or processes, but that would be overkill) which in turn requires the code to be in a function (which is a good idea anyway: don't have substantial code at a module's top level).

    For example:

    import sys, os, string, threading
    import paramiko
    
    cmd = "grep -h 'king' /opt/data/horror_20100810*"
    
    outlock = threading.Lock()
    
    def workon(host):
    
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host, username='xy', password='xy')
        stdin, stdout, stderr = ssh.exec_command(cmd)
        stdin.write('xy\n')
        stdin.flush()
    
        with outlock:
            print stdout.readlines()
    
    def main():
        hosts = ['10.10.3.10', '10.10.4.12', '10.10.2.15', ] # etc
        threads = []
        for h in hosts:
            t = threading.Thread(target=workon, args=(h,))
            t.start()
            threads.append(t)
        for t in threads:
            t.join()
    
    main()
    

    If you had many more than five hosts, I would recommend using instead a "thread pool" architecture and a queue of work units. But, for just five, it's simpler to stick to the "dedicated thread" model (especially since there is no thread pool in the standard library, so you'd need a third party package like threadpool... or a lot of subtle custom code of your own of course;-).

    0 讨论(0)
提交回复
热议问题