简体   繁体   中英

python multissh pool on windows

Trying to get a process pool to work on windows but after asking me the password it again asks me the password.

import os
import sys 
import paramiko
import getpass
import socket
from multiprocessing import Pool


def processFunc(hostname):
 handle = paramiko.SSHClient()
 handle.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 handle.connect(hostname, username=user, password=pw)
 print("child")
 stdin, stdout, stderr = handle.exec_command("show clock")
 cmdOutput = ""
 while True:
  try:
 cmdOutput += stdout.next()
  except StopIteration:
  break
  print("Got output from host %s:%s" % (hostname, cmdOutput))
  handle.close()

 user = "sup"
 f= open('csip.txt','r')
  hostnames = []
 for line in f:
    hostname = line.strip()
    hostnames.append(hostname)
 pw = getpass.getpass("Enter ssh password:")

 if __name__ == "__main__":
     pool = Pool(processes=4)
     pool.map(processFunc, hostnames, 1)
 pool.close()
 pool.join()

Am i doing something wrong? The script should read hostnames from the txt file get the password and then invoke the process pool.

The below works -

But i want help to improve it. dont want to hardcode the username and password.

import os
import sys 
import paramiko
from multiprocessing import Pool

#Globals
Hostnames = []
f= open('csip.txt','r')
for line in f:
hname = line.strip()
Hostnames.append(hname)


def processFunc(Hostname):
    handle = paramiko.SSHClient()
    handle.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    handle.connect(Hostname, username="sup", password="123")
    print("child")
    stdin, stdout, stderr = handle.exec_command("show platform | i unknown")
    cmdOutput = ""
    while True:
    try:
        cmdOutput += stdout.next()
    except StopIteration:
        break
print("Got output from host %s:%s" % (Hostname, cmdOutput))
handle.close()


if __name__ == "__main__":
    pool = Pool(processes=9)
    pool.map(processFunc, Hostnames, 1)
    pool.close()
    pool.join()

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