2016-05-17 48 views
0

試圖讓一個進程池在windows上工作,但在詢問我密碼後,它再次詢問我密碼。windows上的python multissh池

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

我做錯了什麼?腳本應該從txt文件讀取主機名,獲取密碼,然後調用進程池。

回答

0

下面的作品 -

但我想幫助完善它。不想硬編碼的用戶名和密碼。

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