netmiko 自动判断设备类型python_netmiko使用案例8 自动检测设备类型或者协议

autodetect_ssh.py

#!/usr/bin/env python

from netmiko import SSHDetect, Netmiko

from getpass import getpass

device = {

'device_type': 'autodetect',

'host': '10.0.1.7',

'username': 'admin',

'password': getpass(),

}

guesser = SSHDetect(**device)

best_match = guesser.autodetect()

print(best_match) # Name of the best device_type to use further

print(guesser.potential_matches) # Dictionary of the whole matching result

device['device_type'] = best_match

connection = Netmiko(**device)

print(connection.find_prompt())

image.png

autodetect_snmp.py

from netmiko.snmp_autodetect import SNMPDetect

from netmiko import Netmiko

from getpass import

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: from netmiko import ConnectHandler# Define device connection parameters device_params = { 'device_type': 'cisco_ios', 'ip': '192.168.1.1', 'username': 'admin', 'password': 'cisco123', }# Establish a connection to the device connection = ConnectHandler(**device_params)# Execute the 'reload' command on the device output = connection.send_command('reload')# Print the command output print(output)# Close the connection to the device connection.disconnect() ### 回答2: Netmiko是一个Python网络自动化库,它提供了与不同网络设备交互的功能。下面是一个使用Netmiko编写的Python脚本,用于自动重启Cisco网络设备。 ```python from netmiko import ConnectHandler # 定义设备的连接信息 device = { 'device_type': 'cisco_ios', 'ip': '设备IP地址', 'username': '设备用户名', 'password': '设备密码', 'secret': '设备特权模式密码', } try: # 连接设备 net_connect = ConnectHandler(**device) # 进入特权模式 net_connect.enable() # 发送重启命令 output = net_connect.send_command('reload') # 打印重启结果 print(output) # 断开设备连接 net_connect.disconnect() except Exception as e: print(f"连接设备失败: {str(e)}") ``` 在脚本中,我们首先定义了要连接的Cisco设备的信息,包括设备类型、IP地址、用户名、密码和特权模式密码。然后,使用`ConnectHandler`函数建立与设备的连接。 接下来,我们通过`enable`方法进入设备的特权模式,以便发送重启命令。然后,使用`send_command`方法发送`reload`命令来重启设备。最后,我们将打印出重启结果。 如果连接设备失败,将会捕获异常并打印出错误消息。 请注意,为了运行此脚本,需要首先安装Netmiko库,可以使用`pip install netmiko`命令进行安装。此外,脚本中的相关设备信息需要根据实际情况进行修改。 ### 回答3: 可以使用Netmiko模块编写一个Python脚本来实现自动重启Cisco网络设备。下面是一个示例脚本: ```python from netmiko import ConnectHandler # 定义设备信息 device = { 'device_type': 'cisco_ios', 'ip': '设备IP地址', 'username': '用户名', 'password': '密码', 'secret': '特权密码', } # 连接设备 connection = ConnectHandler(**device) # 进入特权模式 connection.enable() # 发送reload命令 output = connection.send_command('reload') # 根据设备输出判断重启是否成功 if 'System is going down for reboot' in output: print('设备正在重启...') else: print('设备重启失败!') # 断开与设备的连接 connection.disconnect() ``` 在上面的脚本中,我们首先定义了要连接的Cisco设备的相关信息,例如设备类型、IP地址、用户名和密码。然后使用`ConnectHandler`函数连接到设备,并使用`enable`方法进入特权模式。 接下来,可以使用`send_command`函数发送`reload`命令来重启设备。脚本会返回设备的输出信息。通过判断输出中是否包含特定的字符串,我们可以确定设备是否正在重启。 最后,使用`disconnect`方法断开与设备的连接。 需要注意的是,为了使用Netmiko模块,您需要在运行脚本之前先安装该模块。可以使用以下命令来安装Netmiko: ``` pip install netmiko ``` 希望以上信息能够对您有所帮助,如果有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值