最近在學(xué)習(xí)python,用python寫了個(gè)檢查ssh存活的腳本。寫完后想增加多線程功能,加快效率,可是一直報(bào)錯(cuò),麻煩大神幫我看下到底錯(cuò)在哪了
以下是代碼:
#!/usr/bin/python
import os
import string
import paramiko
import traceback
import threading
class SshServerCMD():
def __init__(self):
print 'start'
self.username = 'root'
self.passwd = '123'
self.cmd = 'date +%s'
self.ssh = paramiko.SSHClient()
self.pass_file = '/opt/scripts/etc/ssh_proxy.conf'
self.paramiko_log= '/tmp/check_ssh_proxy.log'
self.threads = []
HostList = open(self.pass_file)
for line in HostList.readlines():
ARAY_line = string.split(line)
self.host = ARAY_line[0]
self.port = int(ARAY_line[1])
self.t = threading.Thread(target = self.Connect,args = (self.host,self.port,self.username,self.passwd))
self.threads.append(self.t)
for i in range(10):
self.threads[i].start()
for i in range(10):
self.threads[i].join()
def Connect(self,host,port,username,passwd):
# This function is to connect the agent
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#ssh.load_system_host_keys()
paramiko.util.log_to_file(self.paramiko_log)
try:
self.ssh.connect(host,port,username,passwd)
except Exception as e:
print '\033[31m' + self.host,str(e) + '\033[0m'
else:
self.Cmd()
self.ssh.close()
def Cmd(self):
stdin,stdout,stderr = self.ssh.exec_command(self.cmd)
self.sout = stdout.readlines()
print '\033[32m' + self.host,str(self.sout) + '\033[0m'
if __name__ == '__main__':
SshServerCMD()
以下是/opt/scripts/etc/ssh_proxy.conf文件內(nèi)容
192.168.1.2 4001
192.168.1.3 4002
192.168.1.4 4003
192.168.1.5 4004
192.168.1.6 4005
192.168.1.7 4006
192.168.1.8 4007
192.168.1.9 4008
192.168.1.10 4009
192.168.1.11 4010
報(bào)錯(cuò):執(zhí)行腳本后,發(fā)現(xiàn)程序不停在嘗試ssh 192.168.1.11,也就是文本中所寫的最后一行的ip地址,并且全部都是連接失敗。在邏輯上我就想不通,就算是連接失敗,也應(yīng)該從第一行ip開(kāi)始嘗試,為什么會(huì)從最后一行ip開(kāi)始ssh呢?并且一直在嘗試ssh最后一行ip,求指教。
添加回答
舉報(bào)
0/150
提交
取消