简体   繁体   中英

Python | Netmiko | Auto ping

im beginner and i have tried for a lot

code:

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'))

output like this

Ethe.net0/0 is up, line protocol is up Description: CUSTOMER A
Inte.net address is 10.254.60.69/30

how to auto ping to IP PtP using conn.send_command() based on result of show interface command?

example ping to 10.254.60.70

You get text

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

and you can get IP/MASK using string functions

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

and then you can use standard module ipaddress

import ipaddress

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

or not standard module netaddr

import netaddr

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

EDIT: Minimal working code

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 

Result:

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

This could be your sample and so easy minimal code with 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM