grep -Eoa "\b(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]?)\b" Filetosearch.txt

Regex to reliably search a file for valid IP addresses (and external IP addresses)

Command returns valid IP addresses. Append the following regex to additionally filter out NAT and reserved IP addresses | grep -Ev "^0|\.0[0-9]|^10\.|^127\.|^169\.|^172\.(1[6-9]|2[0-9]|3[01])|^192.168.|^2(2[4-9]|3[0-9])|^2(4[0-9]|5[0-5])"

By: jsbrown
2014-11-02 19:50:54

These Might Interest You

  • Doubt it actually generates valid mac addresses but this version doesn't need any external commands so it runs much faster. Much shorter as well. Show Sample Output

    h=0123456789ABCDEF;for c in {1..12};do echo -n ${h:$(($RANDOM%16)):1};if [[ $((c%2)) = 0 && $c != 12 ]];then echo -n :;fi;done;echo
    dabom · 2010-12-05 01:35:03 0
  • Gets the internal and external IP addresses of all your interfaces, or the ones given as arguments Show Sample Output

    ips(){ for if in ${1:-$(ip link list|grep '^.: '|cut -d\ -f2|cut -d: -f1)};do cur=$(ifconfig $if|grep "inet addr"|sed 's/.*inet addr:\([0-9\.]*\).*/\1/g');printf '%-5s%-15s%-15s\n' $if $cur $(nc -s $cur 128 2>/dev/null||echo $cur);done;}
    frozenfire · 2009-08-07 10:04:46 0
  • Working with lists of IP addresses it is sometimes useful to summarize a count of how many times an IP address appears in the file. This example, summarizeIP, uses another function "verifyIP" previously defined in to ensure only valid IP addresses get counted. The summary list is presented in count order starting with highest count. Show Sample Output

    function summaryIP() { < $1 awk '{print $1}' | while read ip ; do verifyIP ${ip} && echo ${ip}; done | awk '{ip_array[$1]++} END { for (ip in ip_array) printf("%5d\t%s\n", ip_array[ip], ip)}' | sort -rn; }
    mpb · 2015-05-01 16:45:05 1
  • First set the variable $hexchars: hexchars="0123456789ABCDEF" Change the number in the first for loop if you need less then 1200 mac addresses Show Sample Output

    for i in {0..1200}; do for i in {1..12} ; do echo -n ${hexchars:$(( $RANDOM % 16 )):1} ; done | sed -e 's/\(..\)/:\1/g' | sed 's/.\(.*\)/\1/' ; echo; done
    Raymii · 2010-12-04 16:44:11 0
  • Given a file of FQDN, this simple command resolves the IP addresses of those Useful for log files or anything else that outputs domain names. Show Sample Output

    awk < '{ system("resolveip -s " $1) }'
    alanwilliamson · 2009-08-17 08:09:39 3
  • Extracts ip addressess from file using sed. Uses a tag(ip) to grep the IP lines after extracting. Must be a way to just output regex matched on sed. Show Sample Output

    sed -n 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/\nip&\n/gp' ips.txt | grep ip | sed 's/ip//'| sort | uniq
    rubenmoran · 2010-05-23 11:26:32 2

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: