python ssh多交换机
时间: 2023-06-29 13:04:24 浏览: 129
要在 Python 中使用 SSH 连接多个交换机,可以使用 Paramiko 库。以下是一个简单的示例代码:
```python
import paramiko
# 定义 SSH 连接参数
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
username = "your_username"
password = "your_password"
# 定义交换机列表,包括 IP 和端口号
switches = [
{"ip": "10.0.0.1", "port": 22},
{"ip": "10.0.0.2", "port": 22},
{"ip": "10.0.0.3", "port": 22}
]
# 循环连接每个交换机,并执行命令
for switch in switches:
try:
ssh.connect(switch["ip"], port=switch["port"], username=username, password=password)
stdin, stdout, stderr = ssh.exec_command("show version")
print(stdout.read().decode())
ssh.close()
except:
print("Failed to connect to switch: " + switch["ip"])
```
这个示例代码连接了三个交换机,使用 SSH 连接执行了 `show version` 命令,并打印输出结果。你可以根据自己的需求修改此代码。
阅读全文
相关推荐


















