简体   繁体   English

Windows上的python multissh池

[英]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. 试图让进程池在Windows上工作,但是在询问我密码之后再次询问我密码。

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. 该脚本应从txt文件读取主机名,以获取密码,然后调用进程池。

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()

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

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