import paramiko import time List_AuthFail = [] List_TimeOut = [] def SSHTest(ip): ssh = paramiko.SSHClient() # 首次访问时保存公钥 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 设置登录用户名和密码及端口 try: ssh.connect(hostname=ip, port=22, username='root', password='abcdef') channel = ssh.invoke_shell() channel.send('ls') time.sleep(2) temp = channel.recv(9999999).decode() ssh.close() #抓取异常 except paramiko.ssh_exception.AuthenticationException: print(ip, ' 认证失败') List_AuthFail.append(ip) return except TimeoutError: print(ip, ' 访问超时') List_TimeOut.append(ip) return SSHTest('www.arssra.cn') print('认证失败的设备有:', List_AuthFail) print('访问超时失败的设备有:', List_TimeOut)