0

我是初学者,我已经尝试了很多

代码 :

conn = netmiko.ConnectHandler(ip='10.254.60.10', device_type='cisco_ios', 
                                username='user', password='P@ssw0rd')

print (conn.send_command('show interface Ethernet0/0 | i line|Des|Int'))

像这样输出

Ethernet0/0 已启动,线路协议已启动 说明:客户 A
Internet 地址为 10.254.60.69/30

如何conn.send_command()根据show interface命令结果自动 ping 到 IP PtP?

例如 ping 到 10.254.60.70

4

2 回答 2

2

你得到文本

text = '''Ethernet0/0 is up, line protocol is up Description: CUSTOMER A
Internet address is 10.254.60.70/30'''

你可以IP/MASK使用字符串函数

address = text.split(' ')[-1]
print(address)  # 10.254.60.70/30

然后你可以使用标准模块ipaddress

import ipaddress

net = ipaddress.ip_interface(address)
ip = str(net.network.broadcast_address)
print( ip )   # 10.254.60.71 

或非标准模块netaddr

import netaddr

net = netaddr.IPNetwork(address)
ip = str(net.broadcast)
print( ip )   # 10.254.60.71 

编辑:最小的工作代码

text = '''Ethernet0/0 is up, line protocol is up Description: CUSTOMER A
Internet address is 10.254.60.69/30'''

address = text.split(' ')[-1]
print(address)  # 10.254.60.69/30

print('\n--- ipaddress ---\n')

import ipaddress

net = ipaddress.ip_interface(address)

print('ip  :', net.ip )   # 10.254.60.69 
print('ip+1:', net.ip+1 ) # 10.254.60.70
print('ip-1:', net.ip-1 ) # 10.254.60.68

#bip = net.network.broadcast_address
bip = str(net.network.broadcast_address)
print('bip :', bip )      # 10.254.60.71 

print('\n--- netaddr ---\n')

import netaddr

net = netaddr.IPNetwork(address)

print('ip  :', net.ip )   # 10.254.60.69 
print('ip+1:', net.ip+1 ) # 10.254.60.70
print('ip-1:', net.ip-1 ) # 10.254.60.68

bip = net.broadcast
#bip = str(net.broadcast)
print('bip :', bip )      # 10.254.60.71 

结果:

10.254.60.69/30

--- ipaddress ---

ip  : 10.254.60.69
ip+1: 10.254.60.70
ip-1: 10.254.60.68
bip : 10.254.60.71

--- netaddr ---

ip  : 10.254.60.69
ip+1: 10.254.60.70
ip-1: 10.254.60.68
bip : 10.254.60.71
于 2020-07-01T10:13:31.663 回答
0

这可能是您的示例,也是使用Netmiko的简单代码:

from netmiko import ConnectHandler

cisco_Router = {
    "device_type": "cisco_ios",
    "host": "your_router_ip",
    "username": "your_username",
    "password": "your_password"}

with ConnectHandler(**cisco_Router) as net_connect:

    result = net_connect.send_command("ping 4.2.2.4")
    net_connect.disconnect()

print(result)
于 2022-01-12T08:42:09.373 回答