dimanche 27 janvier 2019

Ruby: Using a regular expression to find some IP address in text file?

I have text file content some logs inside it there are some private IP address , I need to find all those IPs by Using regular expression or if there are other ways i can use ?

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 ][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 ]


for example i have to find IPs start with 10.191 || 10.192 || 10.193 ||10.194

For Now i am using the below code to find specific 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

Aucun commentaire:

Enregistrer un commentaire