Monday, December 18, 2017
Regular Expressions: How-to filter IP addresses

Regular Expressions: How-to filter IP addresses

0
417

This  short article explains a simple reg-Ex to extract IPv4 addresses

In this blog, we take a look at a very mundane task of wiritng a regular expression to check for or filter out IPv4 addresses.

IPv4 addresses are in the format of:    X.X.X.X

where X is a value between 0..255 (0..FE in hexadecimal).

Script 1

A lazy Reg-Ex to find IPv4 addresses

Checks for format nnn.nnn.nnn.nnn where n=0..9

#
# Read the file
#
line_list=(`cat $filename`)
#
# Loop through the lines
#
for line in “${line_list[@]}”
do
#
# Check (very loose IP address RegEx)
#
if [[ $line =~ ^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3}) ]];
then
echo “Loose IPv4 address found: $line.”
fi
done

Script 2 - More accurate search

The whole IPv4 address range is between 0.0.0.0 to 255.255.255.255.

In the Reg-Ex below, we are searching for any IPv4 address in the range 1.0.0.0 to 255.255.255.255

.

Now check for values in 0..255 range only

#
# Now check strictly
#
echo “”
echo “Now Checking Strickly… ”

for line in “${line_list[@]}”
do
#
# Check (strict IP address RegEx)
#
# if [[ $startdate =~ (201[5-6])([0][1-9]|[1][1-2])([0-2][0-9]|3[0-1]) ]];
#
if [[ $line =~ ^([1-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\.([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\.([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\.([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]) ]];
then
echo “Strict IPv4 address found: $line”
fi
done

.

Final comments

The Regular expressions used here can be used pretty much unchanged in a variety of scripting tools. The example here uses a bash shell.

But the same RegEx can be used also with grep, Perl or Python amongst others.

NO COMMENTS

This website uses cookies so that we can provide you with the best user experience and to deliver advertising messages and offers on the website that are relevant to you. To read more about the cookies we use and to change your settings see our policy, please click on the link next:

Our Cookie Policy.

This website uses cookies so that we can provide you with the best user experience and to deliver advertising messages and offers on the website that are relevant to you. By continuing to use the site, you agree to the use of cookies.

What are 'COOKIES' ?

Web Browser Cookies

A cookie is a small text file that is sent by a website to your computer or mobile/tablet where it is stored by your web browser. A cookie contains limited non-personal data, usually a unique identifier and the name of the site. This enables a website to recognise you as you move around the site and/or each time you revisit. Cookies are used for a wide variety of purposes such as to keep you logged in or to remember what's in your basket if you're shopping online, to remember your preferences and settings, to analyse how the site is used by you, and to serve advertising to you.

Cookies which are served by the website you are visiting are called "first party cookie". If they are served by another web-site providing services to that website, such as an analytics company or advertising network then they are called "Third party cookies". They will either be stored for the duration of your visit called a "session cookie" or they might remain for a fixed period, which could be months or even years, to remember you across multiple browsing sessions (known as a "persistent cookie").

Google Analytics

We use 'Google Analytics' to collect statistical information about how our websites are used. They use information such as your IP address, browser type and unique identifiers stored in (first party) cookies on your device to record how you interact with our website. We also use 'Google Demographics' data to help us tknow how many users we have, which parts of our sites are most popular, what browsers are used (so we can maximise compatibility), the country or region where our users are located, and the demographics and interests of our users. This enables us to better understand who is using our site and to ensure we are reaching our target demographic, and to improve and tailor our services accordingly.

NOTE:

For more information on cookies and privacy, please visit the UK Information Commissioner's Offce web-site at:

ICO Cookie and Privacy Information

Close