首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python \ Netmiko _ Auto ping

Python \ Netmiko _ Auto ping
EN

Stack Overflow用户
提问于 2020-07-01 08:28:49
回答 3查看 1.4K关注 0票数 0

我是初学者,我试过很多

代码:

代码语言:javascript
复制
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'))

输出像这样

以太0/0上升,线路协议上升描述:客户A 互联网地址为10.254.60.69/30

如何根据PtP命令的结果使用conn.send_command()对IP show interface进行自动切换?

例ping到10.254.60.70

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-07-01 10:13:31

你收到短信

代码语言:javascript
复制
text = '''Ethernet0/0 is up, line protocol is up Description: CUSTOMER A
Internet address is 10.254.60.70/30'''

并且可以使用字符串函数获得IP/MASK

代码语言:javascript
复制
address = text.split(' ')[-1]
print(address)  # 10.254.60.70/30

然后可以使用标准模块ipaddress地址

代码语言:javascript
复制
import ipaddress

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

或者不是标准模块netaddr

代码语言:javascript
复制
import netaddr

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

编辑:最小工作代码

代码语言:javascript
复制
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 

结果:

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2022-01-12 08:42:09

这可能是Netmiko的示例和非常简单的最小代码:

代码语言:javascript
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2022-03-19 19:35:09

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62672725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档