python3批量测试ssh连接是否正常
时间:2023-05-18 来源:未知 投稿:admin 点击:次
每台主机都有不同的端口和密码,您可以使用嵌套字典的方式来存储主机、端口和密码的信息。以下是更新后的示例代码:
pip install paramiko
import paramiko
def test_ssh_connection(ip, port, username, password):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port=port, username=username, password=password, timeout=5)
print(f"Successfully connected to {ip}:{port}")
ssh.close()
except paramiko.AuthenticationException:
print(f"Authentication failed for {ip}:{port}")
except paramiko.SSHException as e:
print(f"SSH connection error for {ip}:{port}: {str(e)}")
except Exception as e:
print(f"Error connecting to {ip}:{port}: {str(e)}")
# 请在这里提供主机信息,每个主机信息以(IP地址, 端口, 密码)的形式表示
hosts = [
("192.168.1.1", 2222, "password1"),
("192.168.1.2", 2223, "password2"),
("192.168.1.3", 2224, "password3")
]
username = "your_username" # 替换为实际的用户名
for host in hosts:
ip, port, password = host
test_ssh_connection(ip, port, username, password)
如果您的问题仍未解决,还可以加入服务器在线技术交流QQ群:8017413寻求帮助。
相关内容