regex 使用正则表达式将IP地址导入防火墙配置(Comodo)

nuypyhwy  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(178)

我正在尝试将IP地址(IPv4)列表导入到我的防火墙配置中,这些列表是已知的VPN服务器,并保存在一个.txt文件中,每行一个IP地址,每个地址前后没有空格或格式。
ip-address-list.txt

  1. IP Address 1
  2. IP Address 2
  3. IP Address 3
  4. IP Address 4
  5. ...
  6. IP Address 2345
  7. IP Address 2346

地址中的每个数字都有一系列0-9之间的数字。共有四个数字系列,由三个冒号(.)分隔。IP地址的每个数字系列中并不总是有三个数字:
示例:

  1. <3 digits per number> 153.225.143.236 [xxx].[xxx].[xxx].[xxx]
  2. <1/2/3 digits per number> 10.123.1.19 [xx].[xxx].[x].[xx]

问题:每个IP地址每行都是一个唯一的地址-我需要将它们转换为一种格式,这种格式要求每个地址都要复制两次,以用于配置文件(Comodo)中的每个条目:
所需的格式为:
config.txt

  1. <Address Type="1">
  2. <IPV4 AddrType="2" AddrStart="23.82.53.58" AddrEnd="23.82.53.58" />
  3. </Address>
  4. <Address Type="1">
  5. <IPV4 AddrType="2" AddrStart="5.187.21.78" AddrEnd="5.187.21.78" />
  6. </Address>
  7. <Address Type="1">
  8. <IPV4 AddrType="2" AddrStart="123.22.14.27" AddrEnd="123.22.14.27" />
  9. </Address>

AddrStartAddrEnd的每个IP地址条目始终相同。
谁能给我指个方向?

dgsult0t

dgsult0t1#

我不知道你会在哪里/如何自动化它,但使用powershell你可以做一些这样的事情:

  1. $ipAddressList = "153.225.143.236 10.123.1.19 23.82.53.58"
  2. $ipList = New-Object Collections.Generic.List[string]
  3. $regex = [regex] '\b(?<IP>(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]))\b'
  4. $match = $regex.Match($ipAddressList)
  5. while ($match.Success) {
  6. $ipList.Add($match.Groups['IP'].Value) | out-null
  7. $match = $match.NextMatch()
  8. }
  9. $result = New-Object Collections.Generic.List[string]
  10. foreach ($ip in $ipList)
  11. {
  12. $result.Add("<Address Type""1"">< IPV4 AddrType = ""2"" AddrStart = """+$ip+""" AddrEnd = """+$ip+""" />");
  13. }
  14. Write-Host ($result -join "`n")
展开查看全部

相关问题