import paramiko

创建SSH客户端对象

ssh_client = paramiko.SSHClient()

添加host key

ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

连接到目标设备

ssh_client.connect(hostname='192.168.1.1', port=22, username='admin', password='admin')

def run_ssh_command(command): # 执行命令 stdin, stdout, stderr = ssh_client.exec_command(command)

# 打印输出结果
output = stdout.read().decode()
print(output)

执行命令

run_ssh_command('system-view') run_ssh_command('netconf') run_ssh_command('quit')

关闭SSH连接

ssh_client.close(

对下面的代码优化import paramikodef run_ssh_commandcommand # 创建SSH客户端对象 ssh_client = paramikoSSHClient # 添加host key ssh_clientset_missing_host_key_policyparamikoAutoAddPolicy # 连接到目标设备 ssh_cl

原文地址: http://www.cveoy.top/t/topic/fbl1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录