我有文本文件内容,里面有一些日志,有一些私有IP地址,我需要使用正则表达式来查找所有这些IP,或者是否可以使用其他方式?
file.txt
172.31.255.2 Jan 22 20:29:55 local0 info 1 2019 Jan 22 17:29:54 ANK-IGW_LC7_Internet1--NAT44-[SessionbasedAD 6 10.194.97.168 internet1-185.106.28.145 19691 15821-185.60.216.35 443] [SessionbasedAD 6 10.194.97.168 internet1-185.106.28.145 57156 29896-185.60.216.11 443] [SessionbasedAD 6 10.193.1.203 internet1-185.106.28.121 43201 9340-146.0.239.19 443] [SessionbasedAD 17 10.191.35.247 internet1-185.106.28.169 22729 11691-93.91.201.207 53] [基于会话的AD 6 10.191.5.249 internet1-185.106.28.97 5541 56078-95.170.215.120 80] [基于会话的AD 6 10.191.8.140 internet1-185.106.28.73 4319 15989-5.129.235.102 55820] [基于会话的AD 6 10.195。 0.168 internet1-185.106.28.1 36823 47893-216.58.212.46 443] [SessionbasedAD 17 10.194.131.2 internet1-185.106.28.49 54713 43535-212.237.115.209 443] [SessionbasedAD 6 10.193.1.179 internet1-185.106.28.145 47574 8226-130.193.149.19 443] [基于会话的广告17 10.191.192.220 internet1-185.106.28.121 16709 49433-93.91.201.207 53] [基于会话的AD 6 10.193.1.179 internet1-185.106.28.145 35656 47013-130.193.149.11 443] [基于会话的AD 17 10.191.8.140 internet1-185.106.28.73 15989 25885-39.42.98.88 62066] [基于会话的AD 6 10.194。 99.170 internet1-185.106.28.1 39010 9907-172.217.169.180 443] [基于会话的广告6 10.195.8.136 internet1-185.106.28.193 39866 56438-212.237.115.33 443] 172.31.255.2 Jan 22 20:29:55 local0 info 1 2019 Jan 22 17:29:54 ANK-IGW_LC7_Internet1--NAT44-[SessionbasedAD 6 10.191.7.99 internet1-185.106.28.161 3471 9214-130.193.149.17 80] [Sessionbased WD 6 10.195.3.115 internet1-185.106.28.65 4021 46771-216.239.36.126 443] [基于会话的WD 6 10.191.4.152 internet1-185.106.28.209 45775 3524-88.85.66.146 443] [基于会话的WD 17 10.191.100.160 internet1-185.106.28.41 26447 13671-93.91 .201.207 53] [基于会话的WD 6 10.191.7.99 internet1-185.106.28.161 54379 27461-130.193.149.8 80] [基于会话的WD 6 10.193.2.24 internet1-185.106.28.209 1525 58284-80.67.85.29 443] [基于会话的WD 6 10.193.2.24 internet1- 185.106.28.209 48488 42817-80.67.85.29 443] [基于会话的WD 6 10.193.2.24 internet1-185.106.28.209 48486 59626-80.67.85.29 443] [基于会话的WD 6 10.193.2.24 internet1-185.106.28.209 48494 22776-80.67.85.29 443] [基于会话的WD 6 10.195.5.37 internet1-185.106.28.65 6493 51461-185.60.216.52 443] [基于会话的WD 17 10.194.100.133 internet1-185.106.28.113 63561 17683-119.28.52.85 8011] [基于会话的WD 6 10.193.6.28 internet1-185.106.28.17 54066 20671-185.60.216.19 443] [基于会话的WD 6 10.195.5.117 internet1-185.106.28.209 1893 5015-212.237.115.33 443] [基于会话的WD 6 10.194.130.163 internet1-185.106.28.65 36203 65366-172.217.169.162 443] [基于会话的WD 6 10.191.5.233 internet1-185.106.28.41 50332 25093-185.60.216.19 443]
例如,我必须找到以10.191 ||开头的IP。 10.192 || 10.193 || 10.194
现在我正在使用以下代码查找特定的IP ruby.file
file_path = './file.txt'
string = '10.195.0.168'
File.open(file_path) do |f|
f.readlines.each { |line|
if line[string]
puts line
puts string
end
}
end
答案 0 :(得分:0)
您可以使用此正则表达式查找您以开头的所有IP,“ IP开头为10.191 || 10.192 || 10.193 || 10.194” ,
\b10\.19[1-4]\.\d+\.\d+
Ruby代码演示
re = /\b10\.19[1-4]\.\d+\.\d+/m
str = '172.31.255.2 Jan 22 20:29:55 local0 info 1 2019 Jan 22 17:29:54 ANK-IGW_LC7_Internet1 - - NAT44 - [SessionbasedAD 6 10.194.97.168 internet1 - 185.106.28.145 19691 15821 - 185.60.216.35 443 ][SessionbasedAD 6 10.194.97.168 internet1 - 185.106.28.145 57156 29896 - 185.60.216.11 443 ][SessionbasedAD 6 10.193.1.203 internet1 - 185.106.28.121 43201 9340 - 146.0.239.19 443 ][SessionbasedAD 17 10.191.35.247 internet1 - 185.106.28.169 22729 11691 - 93.91.201.207 53 ][SessionbasedAD 6 10.191.5.249 internet1 - 185.106.28.97 5541 56078 - 95.170.215.120 80 ][SessionbasedAD 6 10.191.8.140 internet1 - 185.106.28.73 4319 15989 - 5.129.235.102 55820 ][SessionbasedAD 6 10.195.0.168 internet1 - 185.106.28.1 36823 47893 - 216.58.212.46 443 ][SessionbasedAD 17 10.194.131.2 internet1 - 185.106.28.49 54713 43535 - 212.237.115.209 443 ][SessionbasedAD 6 10.193.1.179 internet1 - 185.106.28.145 47574 8226 - 130.193.149.19 443 ][SessionbasedAD 17 10.191.192.220 internet1 - 185.106.28.121 16709 49433 - 93.91.201.207 53 ][SessionbasedAD 6 10.193.1.179 internet1 - 185.106.28.145 35656 47013 - 130.193.149.11 443 ][SessionbasedAD 17 10.191.8.140 internet1 - 185.106.28.73 15989 25885 - 39.42.98.88 62066 ][SessionbasedAD 6 10.194.99.170 internet1 - 185.106.28.1 39010 9907 - 172.217.169.180 443 ][SessionbasedAD 6 10.195.8.136 internet1 - 185.106.28.193 39866 56438 - 212.237.115.33 443 ] 172.31.255.2 Jan 22 20:29:55 local0 info 1 2019 Jan 22 17:29:54 ANK-IGW_LC7_Internet1 - - NAT44 - [SessionbasedAD 6 10.191.7.99 internet1 - 185.106.28.161 3471 9214 - 130.193.149.17 80 ][SessionbasedWD 6 10.195.3.115 internet1 - 185.106.28.65 4021 46771 - 216.239.36.126 443 ][SessionbasedWD 6 10.191.4.152 internet1 - 185.106.28.209 45775 3524 - 88.85.66.146 443 ][SessionbasedWD 17 10.191.100.160 internet1 - 185.106.28.41 26447 13671 - 93.91.201.207 53 ][SessionbasedAD 6 10.191.7.99 internet1 - 185.106.28.161 54379 27461 - 130.193.149.8 80 ][SessionbasedWD 6 10.193.2.24 internet1 - 185.106.28.209 1525 58284 - 80.67.85.29 443 ][SessionbasedWD 6 10.193.2.24 internet1 - 185.106.28.209 48488 42817 - 80.67.85.29 443 ][SessionbasedWD 6 10.193.2.24 internet1 - 185.106.28.209 48486 59626 - 80.67.85.29 443 ][SessionbasedWD 6 10.193.2.24 internet1 - 185.106.28.209 48494 22776 - 80.67.85.29 443 ][SessionbasedWD 6 10.195.5.37 internet1 - 185.106.28.65 6493 51461 - 185.60.216.52 443 ][SessionbasedWD 17 10.194.100.133 internet1 - 185.106.28.113 63561 17683 - 119.28.52.85 8011 ][SessionbasedWD 6 10.193.6.28 internet1 - 185.106.28.17 54066 20671 - 185.60.216.19 443 ][SessionbasedWD 6 10.195.5.117 internet1 - 185.106.28.209 1893 5015 - 212.237.115.33 443 ][SessionbasedWD 6 10.194.130.163 internet1 - 185.106.28.65 36203 65366 - 172.217.169.162 443 ][SessionbasedWD 6 10.191.5.233 internet1 - 185.106.28.41 50332 25093 - 185.60.216.19 443 ]'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
根据需要打印所有IP。
10.194.97.168
10.194.97.168
10.193.1.203
10.191.35.247
10.191.5.249
10.191.8.140
10.194.131.2
10.193.1.179
10.191.192.220
10.193.1.179
10.191.8.140
10.194.99.170
10.191.7.99
10.191.4.152
10.191.100.160
10.191.7.99
10.193.2.24
10.193.2.24
10.193.2.24
10.193.2.24
10.194.100.133
10.193.6.28
10.194.130.163
10.191.5.233
答案 1 :(得分:0)
您可以使用grep
。
puts File.readlines("file.txt").grep(/ 10\.19[1-4][^\s]+ /)