1、需求
想查某个ip地址是否在各个防火墙、网络设备有安全策略;传统模式需要熟悉不同产品命令的网络工程师登录多个设备查看该ip地址的策略。通过python实现傻瓜式操作;
2、python测试脚本原理
- 查看ip地址是否合理
- 查询的ip地址
# -*- 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)
x
1
# -*- coding: utf-8 -*-
2
# Aduthor:CCIP-Ma
3
import os
4
import re
5
import sys
6
#print(os.listdir(r‘E:\filetest‘))
7
import_ip= input(‘查询的ip地址:‘)
8
def checkip(hostip):
9
pat = re.compile(r‘([0-9]{1,3})\.‘)
10
r = re.findall(pat,hostip+".")
11
if len(r)==4 and len([x for x in r if int(x)>=0 and int(x)<=255])==4:
12
pass
13
else:
14
print("错误")
15
sys.exit()
16
checkip(import_ip)
17
path=r‘E:\a‘#配置存放文件目录
18
def FindIp(ip):#查询ip
19
for file in os.listdir(path):
20
f_path=path+"\\"+file
21
#print(f_path)
22
f=open(f_path,‘r‘,encoding=‘utf-8‘)
23
for line in f:
24
if ip in line:
25
print(‘‘‘-----------info---------
26
策略:%s
27
节点名称:%s
28
‘‘‘%(line,file))
29
else:
30
pass
31
print(‘没有该%s地址的策略‘%ip)
32
33
34
35
FindIp(import_ip)
36
原文地址:https://www.cnblogs.com/ccip-ma/p/188c4cb99e589563c6b73a2847434d23.html
时间: 2024-10-02 06:08:03