Python |网米科 |自动ping

Python | Netmiko | Auto ping

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

代码:

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 is up, line protocol is up Description: CUSTOMER A
Internet address is 10.254.60.69/30

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

示例 ping 到 10.254.60.70

您收到短信

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

你可以使用字符串函数IP/MASK得到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

这可能是您的示例和使用 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)
I haven't write it for netmiko yet, but I often use this code for paramiko.

import threading
from ping3 import ping
from queue import Queue
from ipaddress import ip_network, ip_address
import paramiko
import time
from termcolor import colored
import sys
import os
import subprocess

file1 = open('PING_OK.txt', 'w')
file2 = open('PING_NOK.txt', 'w')

hosts=[]
f1 = open('hostfile.txt', 'r')
devices= f1.readlines()
#print(devices)

for i in devices:
    i = i.split()
    hosts.append(i[0])

hosts_live = []
q = Queue()


for host in hosts:
    q.put(host)
enter = "\r\n"

def ping2(ip_address):
    from pythonping import ping
    output = ping(ip_address, verbose=True)

    output_list =output.rtt_avg_ms
    print(output_list)

    if output_list == 2000:
        print("erişim yok"+ip_address)
        file2.write(ip_address+"\n")
    else:
        print ("erişim var"+ip_address)
        file1.write(ip_address + "\n")

 def worker():
     while True:
         host = q.get()
         ping2(host)
         time.sleep(2)
         q.task_done()

 for i in range(1):#aynı anda bağlantı 15 ten fazla girilmemeli #
     t = threading.Thread(target=worker)
     t.deamon = True
     t.start()

 q.join()

 file1.close()
 file2.close()