2017-08-24 62 views
0

請幫我改正我的腳本。我的腳本檢查主機的ssh登錄並根據結果顯示成功/失敗。當我給出錯誤的主機名時它失敗。python3.5 paramiko因主機名失敗

代碼是以下:

[[email protected] script]# cat param.py 
import multiprocessing 
import paramiko 
import random 
import threading 
import time 

host_name = "test2" 
print ("Checking hostname :"+str(host_name)) 
file = open('output_file','a') 
ssh = paramiko.SSHClient() 
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
try: 
     ssh.connect(host_name, username='root', password='test') 
     print ("success") 
     file.write("success:"+str(host_name+"\n")) 
except paramiko.SSHException: 
     print ("Connection Failed") 
     file.write("failed:"+str(host_name+"\n")) 
     quit() 
stdin,stdout,stderr = ssh.exec_command("hostname&&uptime") 
for line in stdout.readlines(): 
     print (line.strip()) 
ssh.close() 

它正常工作時,正確的用戶名/密碼給出:

[[email protected] script]# python3 param.py 
Checking hostname :test2 
success 
test2 
12:31:49 up 83 days, 2:56, 2 users, load average: 0.00, 0.01, 0.05 

時,給出了錯誤的密碼,它工作正常。我已經在腳本中將密碼更改爲錯誤密碼,並表示連接失敗。

[email protected] script]# python3 param.py 
Checking hostname :test2 
Connection Failed 

現在我的問題是,當我將主機名更改爲doenot存在時,paramiko失敗並彈出大量錯誤。

[[email protected] script]# python3 param.py 
Checking hostname :invalidtest2 
Traceback (most recent call last): 
    File "param.py", line 16, in <module> 
    ssh.connect(host_name, username='root', password='test') 
    File "/root/usr/local/lib/python3.5/site-packages/paramiko/client.py", line 301, in connect 
    to_try = list(self._families_and_addresses(hostname, port)) 
    File "/root/usr/local/lib/python3.5/site-packages/paramiko/client.py", line 199, in _families_and_addresses 
    hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM) 
    File "/root/usr/local/lib/python3.5/socket.py", line 728, in getaddrinfo 
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags): 
socket.gaierror: [Errno -2] Name or service not known 

如何獲得連接失敗的消息?我正在使用python3.0

回答

0

保留密碼存儲在腳本本身並不是個好主意。修改腳本,以便從文件獲取主機名並將輸出寫入不同文件

#!/bin/python3 
import threading, time, paramiko, socket, getpass 
from queue import Queue 
start_time1 = time.time() 
locke1 = threading.Lock() 
q = Queue() 

#Check the login 
def check_hostname(host_name, pw_r): 
    with locke1: 
     print ("Checking hostname :"+str(host_name)+" with " + threading.current_thread().name) 
     file_output = open('output_file','a') 
     file_success = open('success_file','a') 
     file_failed = open('failed_file','a') 
     file_error = open('error_file','a') 
     ssh = paramiko.SSHClient() 
     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
     try: 
      ssh.connect(host_name, username='root', password=pw_r) 
      #print ("Success") 
      file_success.write(str(host_name+"\n")) 
      file_success.close() 
      file_output.write("success: "+str(host_name+"\n")) 
      file_output.close() 

      # printing output if required from remote machine 
      #stdin,stdout,stderr = ssh.exec_command("hostname&&uptime") 
      #for line in stdout.readlines(): 
      # print (line.strip()) 

     except paramiko.SSHException: 
       # print ("error") 
       file_failed.write(str(host_name+"\n")) 
       file_failed.close() 
       file_output.write("failed: "+str(host_name+"\n")) 
       file_output.close() 
       #quit() 
     except paramiko.ssh_exception.NoValidConnectionsError: 
       #print ("might be windows------------") 
       file_output.write("failed: " + str(host_name + "\n")) 
       file_output.close() 
       file_failed.write(str(host_name+"\n")) 
       file_failed.close() 
       #quit() 
     except socket.gaierror: 
      #print ("wrong hostname/dns************") 
      file_output.write("error: "+str(host_name+"\n")) 
      file_output.close() 
      file_error.write(str(host_name + "\n")) 
      file_error.close() 
     ssh.close() 

def performer1(): 
    while True: 
     hostname_value = q.get() 
     check_hostname(hostname_value,pw_sent) 
     q.task_done() 

if __name__ == '__main__': 

    print ("This script checks all the hostnames in the input_file with your standard password and write the outputs in below files: \n1.file_output\n2.file_success \n3.file_failed \n4.file_error \n") 

    f = open('output_file', 'w') 
    f.write("-------Output of all hosts-------\n") 
    f.close() 
    f = open('success_file', 'w') 
    f.write("-------Success hosts-------\n") 
    f.close() 
    f = open('failed_file', 'w') 
    f.write("-------Failed hosts-------\n") 
    f.close() 
    f = open('error_file', 'w') 
    f.write("-------Hosts with error-------\n") 
    f.close() 

    with open("input_file") as f: 
     hostname1 = f.read().splitlines() 

#Read the standard password from the user 
    pw_sent=getpass.getpass("Enter the Password:") 


    for i in hostname1: 
     q.put(i) 
    #print ("all the hostname : "+str(list(q.queue))) 
    for no_of_threads in range(3): 
     t = threading.Thread(target=performer1) 
     t.daemon=True 
     t.start() 

    q.join() 
    print ("Check output files for results") 
    print ("completed task in" + str(time.time()-start_time1) + "seconds") 
相關問題