簡體   English   中英

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

output 像這樣

Ethe.net0/0 已啟動,線路協議已啟動 說明:CUSTOMER A
互聯網地址是 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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM