Check ip address in the security policies of multiple security devices

1, demand
Want to check whether an ip address in each firewall, network equipment security policies; network engineers need to be familiar with the traditional model of different products command log in to multiple devices to view the ip address policy. Fool-operation achieved by the python;
2, python test script principle
  1. Check ip address is reasonable
  2. ip address query
# -*- coding: utf-8 -*-
# Aduthor:CCIP-Ma
import os
import re
import sys
#print(os.listdir(r'E:\filetest'))
import_ip= input('查询的ip地址:')
def checkip(hostip):
    pat = re.compile(r'([0-9]{1,3})\.')
    r = re.findall(pat,hostip+".")
    if len(r)==4 and len([x for x in r if int(x)>=0 and int(x)<=255])==4:
        pass
    else:
        print("错误")
        sys.exit()
checkip(import_ip)
path=r'E:\a'#配置存放文件目录
def FindIp(ip):#查询ip
    for file in os.listdir(path):
        f_path=path+"\\"+file
        #print(f_path)
        f=open(f_path,'r',encoding='utf-8')
        for line in f:
            if ip in line:
                print('''-----------info---------
                策略:%s
                节点名称:%s
                '''%(line,file))
            else:
                pass
    print('没有该%s地址的策略'%ip)



FindIp(import_ip)




Guess you like

Origin www.cnblogs.com/ccip-ma/p/188c4cb99e589563c6b73a2847434d23.html