Exemplo n.º 1
0
    def telnet_get_connect(self, password_check=False, retry=[]):
        """
        输入:retry:重试密码集合,默认为空; password_check:是否提供密码校正功能,即将密码集合中的正确密码更正在json文件中
        输出:0:连接成功,1:连接失败
        功能:与服务器进行连接
        """
        try:
            self.telnet_connect(self.password)  #利用用户提供的密码进行第一次连接尝试
            check = self.password
            self.con_status = True
        except Exception as e:
            err = str(e)
            if e:
                print('%s身份验证失败,重试密码中......' % self.ip)

        if self.con_status == False:
            for i in retry:
                login_tip = self.tn.read_very_eager()
                if b"incorrect" in login_tip or login_tip is None:
                    check = i
                    self.tn.write(
                        self.username.encode('ascii') + WINDOWS_LINE_SPLITER)
                    self.tn.read_until(b"assword:")
                    self.tn.write(i.encode('ascii') + WINDOWS_LINE_SPLITER)
                    time.sleep(5)  #休眠,因为服务器的返回内容有可能会延迟到达缓冲区,否则下一步读取会出错
                else:
                    if password_check is True:
                        wt = writer()
                        wt.updete_json(ip=self.ip,
                                       password=check,
                                       passAlter=True)
                    self.con_status = True
                    return 0

            login_tip = self.tn.read_very_eager()
            if b"login" in login_tip or login_tip is None:
                loggerForTelnet.error('%s试完所有密码未能连接成功' % self.ip)
                return 1
            else:
                if password_check is True:
                    wt = writer()
                    wt.updete_json(ip=self.ip, password=check, passAlter=True)
                self.con_status = True
                return 0
Exemplo n.º 2
0
def run():
    THREAD_SUM = int(os.environ["THREAD_SUM"])
    wd=reader()
    wt=writer()

    connect_info=wd.readIp()
    #线程数为0的情况,默认改成1,但是config文件中不予改变
    if THREAD_SUM <= 0:
        loggerForSsh.warn(u"配置文件(config.py)中线程数(THREAD_SUM)应当大于0,否则执行时默认为1")
        THREAD_SUM = 1
    distribution = distribute(len(connect_info), THREAD_SUM)

    cmds=wd.readCmd()
    retry = wd.readRetry()
    ssh_showResult = []
    threads = []

    # 添加线程到线程列表
    for thread in range(THREAD_SUM):
        if distribution[thread] is None:
            #当出现None时,说明线程数量比服务器数量多,多余的线程可以开启
            break
        servers = [connect_info[i] for i in distribution[thread]]
        threads.append(myThread(servers,retry,cmds,thread))

    # 开启新线程
    thread_sum = range(len(threads))
    for thread in thread_sum:
        threads[thread].setDaemon(True)
        threads[thread].start()
    
    # 等待所有线程完成
    for thread in thread_sum:
        threads[thread].join()
    loggerForSsh.info(u"所有的线程执行完毕")
    
    #得到所有的结果
    for thread in thread_sum:
        ssh_showResult += threads[thread].get_result()

    try:
        wt.csv_write_head()
        for p in ssh_showResult:
            wt.csv_write_body(p)
    except IOError as e:
        loggerForSsh.error(u"操作csv文件失败")
Exemplo n.º 3
0
    def get_connect(self, retryList):
        #允许连接不在know_hosts文件中的主机。
        self.sh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        #用原始密码连接做第一次尝试
        try:
            self.sh.connect(self.ip,
                            self.port,
                            self.userName,
                            self.passWord,
                            timeout=3)
            self.conStatus = True
        except Exception as e:
            err = str(e)
            if 'Authentication failed' in err:
                print('身份验证失败,重试密码中......')
            elif '[WinError 10060]' in err:
                #print('连接超时无答复,'+self.ip+'主机未开或网络出现问题')#需要特别反应
                logger.error("%s连接超时,目标主机未开或网络出现问题" % self.ip)

        #若第一次尝试失败,进行密码重试
        if self.conStatus is False:
            for rePass in retryList:
                try:
                    self.sh.connect(self.ip, self.port, self.userName, rePass)
                    self.conStatus = True
                    #更新配置文件中的password
                    wt = writer()
                    wt.updete_json(ip=self.ip, password=rePass, passAlter=True)
                    break
                except Exception as e:
                    err2 = str(e)
                    if err2 != '':
                        continue

        #若尝试完所有密码还是连接失败
        if self.conStatus is False:
            logger.error('%s试完所有密码未能连接成功' % self.ip)
Exemplo n.º 4
0
        sshCon.ssh_get_connect(self.retry)
        self.result.append(sshCon.passWord)
        if sshCon.conStatus == True:
            #print(self.ip+':')
            tmp = sshCon.ssh_do(cmds)
            for info in tmp:
                self.result.append(info)

    def get_result(self):
        return self.result


if __name__ == '__main__':
    loggerForSsh.info("查看一次服务器信息")
    wd = reader()
    wt = writer()

    connect_info = wd.readIp()
    cmds = wd.readCmd()
    retry = wd.readRetry()

    ssh_showResult = []
    threads = []

    count = range(len(connect_info))
    # 添加线程到线程列表
    for j in connect_info:
        #print(j['ip'],j['user'],j['password'],retry)
        threads.append(myThread(j['ip'], j['user'], j['password'], retry,
                                cmds))
    # 开启新线程