GREP/FIND IPS IN A FILE
########################

CITATION: http://www.unix.com/shell-programming-scripting/185469-grep-ip-address-file.html

*** ANSWER – FIND IPS ANYWHERE IN A FILE (valid ips as i understand) – puts its finds on a new line – BEST ONE ***:

grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'

 

TO JOIN LINES THAT ARE SEPERATED BY NEWLINES INTO A SINGLE LINE – SPACE SEPERATED JOIN:
cat file | xargs

TO JOIN LINES THAT ARE SEPERATED BY NEWLINES INTO A SINGLE LINE – COMMA SEPERATED JOIN:
cat file | xargs | sed -e ‘s/ /, /g’

*** ANSWER (SINGLE LINE, SPACE SEPERATED) – THE BEST METHOD FROM ABOVE WITH JOINING TO SINGLE LINE ADDITION ***:

grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | xargs

 

*** ANSWER (SINGLE LINE OUTPUT, COMMA SEPERATED) – THE BEST METHOD FROM ABOVE WITH JOINING TO SINGLE LINE ADDITION ***:

grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | xargs | sed -e 's/ /, /g'

 

— BAD ANSWER – FIND IPS THAT START A LINE IN A FILE (valid ips as i understand) – not as good one —:
‘^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$’
example1:
———-

IMAGINE THIS FILE (IPS THAT START IN A LINE, OR BEGIN IN THE LINE BUT NOT IN THE START OFTHE LINE)

# cat ip12
11.22.33.44
192.68.1.2
helo
l
72.34.34.200
333.444.555.666
12.23e.544.423
myip1 11.22.33.44
myip2 33.44.55.66 #fine this IP should also be listed

 

# cat ip12 | grep '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$'
11.22.33.44
192.68.1.2
72.34.34.200
333.444.555.666

 

**** FIND IPS ANYWHERE **** <— BEST ONE

# cat ip12 | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
11.22.33.44
192.68.1.2
72.34.34.200
11.22.33.44
33.44.55.66

 

example2:
———-

# cat test
hello
nothing
4.4.4.5 whatever 192.168.7.5
hello 7.7.7.7 why 10.5.6.7 192.5.6.3
10.51.30.5 and 10.5.4.2,10.3.4.5,99.99.99.99
12.123.41.3
999.999.999.999 and 1.1.1.1,2.2.2.2 or 3.3.3.3
with 888.888.888.888-5.5.5.5
with 299.299.299.299
no 288.277.299.299,-1.-1.-1.-1
299.236.123.280

 

THIS ONE ONLY FINDS IPS IF THE LINE STARTS WITH THEM (not as good one)

# cat test | grep '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$'
12.123.41.3
299.236.123.280

 

**** FIND IPS ANYWHERE **** <— BEST ONE – amazing how it found the only correct ones, and even if they are seperated by a comma

# cat test | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
4.4.4.5
192.168.7.5
7.7.7.7
10.5.6.7
192.5.6.3
10.51.30.5
10.5.4.2
10.3.4.5
99.99.99.99
12.123.41.3
1.1.1.1
2.2.2.2
3.3.3.3
5.5.5.5
99.236.123.28

 

**** FIND IPS ANYWHERE, and output to a new line comma seperated ****

# cat test | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | xargs | sed -e 's/ /, /g'
4.4.4.5, 192.168.7.5, 7.7.7.7, 10.5.6.7, 192.5.6.3, 10.51.30.5, 10.5.4.2, 10.3.4.5, 99.99.99.99, 12.123.41.3, 1.1.1.1, 2.2.2.2, 3.3.3.3, 5.5.5.5, 99.236.123.28

example3:
———-

FIND ALL OF THE IPS USING YOUR SYSTEM (ips listed more then once with multiple sessions only list their ip once with this script):

smbstatus | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | sort | uniq -d

 

SINGLE LINE OUTPUT (join output with commas using paste)

smbstatus | grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | sort | uniq -d | xargs | sed 's/ /, /g'

 

NOTE: bash navigating the cursor big lines:

control-a: to start
control-e: to end
alt-b: back a word
alt-f: forward a word

Leave a Reply

Your email address will not be published. Required fields are marked *