Skip to content
Home » Power of grep and Regex: match IP Addresses with Ease

Power of grep and Regex: match IP Addresses with Ease

  • Linux
  • 3 min read

Introduction to grep and Regular Expressions

Grep is a powerful command-line tool that allows users to search for specific patterns within a file or a stream of data. When combined with regular expressions, or regex, the capabilities of grep become almost limitless. With regex, you can search for complex patterns, such as valid IP addresses, with just a few keystrokes.

For example, let’s say we have a log file from the website “itvraag.nl” and we want to search for all the IP addresses that have accessed the site. We can use the following command:

grep -E '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}' logfile.txt

This command will search the “logfile.txt” for any strings that match the pattern of four groups of one to three digits, separated by periods. This will effectively search for any valid IP addresses within the file.

Tips for Using grep and Regex for IP Address Searching

  1. Use the “-E” flag to enable regex searching.
  2. Use the “.” escape sequence to match a literal period.
  3. Use the “{n,m}” quantifier to match a range of repetitions.
  4. Use the “[0-9]” character class to match any single digit.
  5. Use the “^” and “$” anchors to match the beginning and end of a line, respectively.

Here are a few more examples of using grep and regex to search for IP addresses:

# Search for IP addresses at the beginning of a line
grep -E '^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}' logfile.txt

# Search for IP addresses at the end of a line
grep -E '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$' logfile.txt

# Search for IP addresses that have accessed "itvraag.nl"
grep -E 'itvraag\\.nl.*[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}' logfile.txt

# Search for IP addresses within a certain range (e.g. 192.168.0.0 - 192.168.255.255)
grep -E '192\\.168\\.[0-9]{1,3}\\.[0-9]{1,3}' logfile.txt

Be more accurate

The example above was a quick and dirty fix, because it will also show invalid IP addresses like 999.999.999.999

Let’s take another example, 10.100.34.x. The regular expression ^10\\.100\\.34\\.[0-9]{1,3}$ will match any IP address that starts with “10.100.34.” and is followed by one to three digits. This includes invalid IP addresses like 10.100.34.999, which is outside the range of valid IP addresses (0-255).

To match only valid IP addresses, you can use the following regular expression:

^10\\.100\\.34\\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$

This regular expression will match any IP address that starts with “10.100.34.” and is followed by a digit in the range 0-255. This ensures that the IP address is valid.

To use this regular expression with the grep command, you can use the following syntax:

grep -E "^10\\.100\\.34\\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$" file.txt

This will search the file file.txt for any lines that match the regular expression and print the matching lines to the terminal.

The last part of the regular expression is used to match a digit in the range 0-255.

This part of the regular expression is made up of five different subexpressions, each separated by a vertical bar (|). These subexpressions are used to match different ranges of digits:

  1. [0-9]: This subexpression will match any single digit from 0 to 9.
  2. [1-9][0-9]: This subexpression will match any two-digit number starting with 1 to 9 (e.g., 10, 11, 12, …, 99).
  3. 1[0-9][0-9]: This subexpression will match any three-digit number starting with 100 (e.g., 100, 101, 102, …, 199).
  4. 2[0-4][0-9]: This subexpression will match any three-digit number starting with 200 to 249 (e.g., 200, 201, 202, …, 249).
  5. 25[0-5]: This subexpression will match any three-digit number starting with 250 to 255 (e.g., 250, 251, 252, …, 255).

Together, these subexpressions will match any digit in the range 0-255.

Leave a Reply

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

20 + thirteen =