目的:通过python自动化管理 华为交换机,超时设置了很长时间也不行
错误:每次只能执行commands里面的第一条命令,就显示SSH连接错误:SSH session not active,尝试了多种方法都不行,希望能求到一份解决办法
import paramiko
# SSH连接参数
hostname = ''
port = 22
username = ''
password = ''
# 创建SSH客户端
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# 连接SSH服务器
client.connect(hostname, port, username, password)
# 执行多条命令
commands = [
'display version',
'display interfaces',
'display arp'
]
for command in commands:
stdin, stdout, stderr = client.exec_command(command)
# 读取命令输出
output = stdout.read().decode('utf-8')
print(output)
except paramiko.AuthenticationException as e:
print(f"身份验证失败:{e}")
except paramiko.SSHException as e:
print(f"SSH连接错误:{e}")
except paramiko.ChannelException as e:
print(f"通道错误:{e}")
finally:
# 关闭SSH客户端
client.close()