如何将变量发送到Netmiko发送命令

时间:2020-03-04 19:04:49

标签: python netmiko

我正在尝试运行python脚本,以从用户那里获取cisco交换机接口ID ex:Gi1 / 0/1的输入,并将其输入脚本(显示接口,并将命令发送到cisco switch。

我知道input()函数将在执行脚本时接受输入,但是我不知道如何获取该输入并将其与“ show”命令合并

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这是一个小脚本,用于检查“显示界面xxx”

from netmiko import ConnectHandler 
cisco = { 
 'device_type': 'cisco_ios', 
 'host': 'cisco.domain.com', 
 'username': 'admin', 
 'password': 'cisco123', 
 } 

try:
    net_connect = ConnectHandler(**cisco)
    net_connect.enable()
    interface =input('Pls enter the interface name: ')
    output = net_connect.send_command("show interface " + interface)
    print('*'*10)
    print(output)
    net_connect.disconnect()
except:
    print(cisco['host']+' not connected!')

如您所见,这非常简单。基本上,我们在.send_command()内发送一个字符串。因此,您可以按以下方式放置要发送的任何字符串:

interface = input('Pls enter the interface name: ')
output = net_connect.send_command("show interface " + interface)