吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 362|回复: 16
收起左侧

[求助] 请教各位编程大佬,Python正则匹配文本问题

[复制链接]
baigei2333 发表于 2024-6-4 10:22
各位大佬好,本人python小白,有一个问题想请教各位大佬。如下示例文本,我想找到每条rule name中存在service telnet或service ssh或service tcp_3389的,记录下rule name名字写入excel文件中,请问用正则表达式可以实现吗??如果用textfsm可以很容易实现,但是因为环境问题,不方便安装其他包,
<GD0H3FW10-WW01>display security-policy ip
Security-policy ip

rule 37 name SRM202307100275
  action pass
  disable
  logging enable
  counting enable
  description 护网临时限制
  source-zone Trust
  destination-zone Untrust
  source-ip Net_192.168.232.0
  destination-ip 3.1.1.1
  service tcp_3389

rule 38 name SRM202307100275-2
  action drop
  disable
  logging enable
  counting enable
  description 限制非 sjyh01用户访问
  source-zone Trust
  destination-zone Untrust
  destination-ip 1.1.2.1
  service icmp_any
  service tcp_20060

rule 36 name cy_test
  action pass
  disable
  logging enable
  counting enable
  source-zone Trust
  destination-zone Untrust
  source-ip ip_192.168.232.214
  service http
  service ssh
  service icmp_any

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
ManaCola + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

知心 发表于 2024-6-4 10:37
用字符串分割实现更好吧
SMRC86 发表于 2024-6-4 10:40
可以使用正则表达式实现。以下是一个使用Python的示例代码:

```python
import re

text = '''
rule 37 name SRM202307100275
action pass
disable
logging enable
counting enable
description 护网临时限制
source-zone Trust
destination-zone Untrust
source-ip Net_192.168.232.0
destination-ip 3.1.1.1
service tcp_3389

rule 38 name SRM202307100275-2
action drop
disable
logging enable
counting enable
description 限制非 sjyh01用户访问
source-zone Trust
destination-zone Untrust
destination-ip 1.1.2.1
service icmp_any
service tcp_20060

rule 36 name cy_test
action pass
disable
logging enable
counting enable
source-zone Trust
destination-zone Untrust
source-ip ip_192.168.232.214
service http
service ssh
service icmp_any
'''

pattern = r'rule \d+ name (\S+).*?(service (telnet|ssh|tcp_3389))'
matches = re.findall(pattern, text, re.DOTALL)

result = [match[0] for match in matches]
print(result)
```

这段代码会输出:

```
['SRM202307100275', 'cy_test']
```

接下来,你可以将结果写入Excel文件中。
wapjsx 发表于 2024-6-4 10:44
可以把这段文本需要 的内容标红或标其他容易区分的颜色吗?

我来试试。
 楼主| baigei2333 发表于 2024-6-4 10:46
wapjsx 发表于 2024-6-4 10:44
可以把这段文本需要 的内容标红或标其他容易区分的颜色吗?

我来试试。

需要的内容就是每段rule name中只要存在service telnet或service ssh或service tcp_3389的,就提取对应的rule name
 楼主| baigei2333 发表于 2024-6-4 10:48
SMRC86 发表于 2024-6-4 10:40
可以使用正则表达式实现。以下是一个使用Python的示例代码:

```python

不行啊,输出的是['SRM202307100275', 'SRM202307100275-2']
只有午安 发表于 2024-6-4 11:08
本帖最后由 只有午安 于 2024-6-4 11:26 编辑

报意思,没有注意看要求
SMRC86 发表于 2024-6-4 11:08
baigei2333 发表于 2024-6-4 10:48
不行啊,输出的是['SRM202307100275', 'SRM202307100275-2']

试试这个呢,我身边没有电脑,只能大概判断了




import re

text = """<GD0H3FW10-WW01>display security-policy ip
Security-policy ip

rule 37 name SRM202307100275
  action pass
  disable
  logging enable
  counting enable
  description 护网临时限制
  source-zone Trust
  destination-zone Untrust
  source-ip Net_192.168.232.0
  destination-ip 3.1.1.1
  service tcp_3389

rule 38 name SRM202307100275-2
  action drop
  disable
  logging enable
  counting enable
  description 限制非 sjyh01用户访问
  source-zone Trust
  destination-zone Untrust
  destination-ip 1.1.2.1
  service icmp_any
  service tcp_20060

rule 36 name cy_test
  action pass
  disable
  logging enable
  counting enable
  source-zone Trust
  destination-zone Untrust
  source-ip ip_192.168.232.214
  service http
  service ssh
  service icmp_any"""

rule_names = re.findall(r"rule \d+ name ([^\n]+)(?=\n  action)", text)
filtered_rule_names = [name for name in rule_names if re.search(r"service telnet|service ssh|service tcp_3389", text)]

print(filtered_rule_names)
wapjsx 发表于 2024-6-4 11:15
baigei2333 发表于 2024-6-4 10:46
需要的内容就是每段rule name中只要存在service telnet或service ssh或service tcp_3389的,就提取对应的 ...

尴尬啦~~~  rule name 是哪个?我不懂!
捷豹网络丶贱仔 发表于 2024-6-4 11:21
[Python] 纯文本查看 复制代码
import re
import pandas as pd

# 示例文本
text = """
<GD0H3FW10-WW01>display security-policy ip
Security-policy ip

rule 37 name SRM202307100275
  action pass
  disable
  logging enable
  counting enable
  description 护网临时限制
  source-zone Trust
  destination-zone Untrust
  source-ip Net_192.168.232.0
  destination-ip 3.1.1.1
  service tcp_3389

rule 38 name SRM202307100275-2
  action drop
  disable
  logging enable
  counting enable
  description 限制非 sjyh01用户访问
  source-zone Trust
  destination-zone Untrust
  destination-ip 1.1.2.1
  service icmp_any
  service tcp_20060

rule 36 name cy_test
  action pass
  disable
  logging enable
  counting enable
  source-zone Trust
  destination-zone Untrust
  source-ip ip_192.168.232.214
  service http
  service ssh
  service icmp_any
"""

# 正则表达式匹配规则
rule_pattern = re.compile(r'rule \d+ name (.*?)\n(.*?)(?=\nrule|\Z)', re.DOTALL)
service_pattern = re.compile(r'service (telnet|ssh|tcp_3389)')

# 提取符合条件的规则名称
rule_names = []
for match in rule_pattern.finditer(text):
    rule_name = match.group(1)
    rule_body = match.group(2)
    if service_pattern.search(rule_body):
        rule_names.append(rule_name)

# 写入Excel文件
df = pd.DataFrame(rule_names, columns=['Rule Name'])
df.to_excel('rules_with_services.xlsx', index=False)

print("符合条件的规则名称已写入rules_with_services.xlsx")
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-28 11:01

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表