Thursday, August 28, 2014

Stand Back I know Regular Expressions


This week I was in training for a Red Hat Certification Exam, and was tasked with setting up some of the RHEL networking components (statically).

All in all not hard, to convert from a dynamic setup to a static setup however to do so you have to know some very permanent information.

Such as your IP address.

All in all this is simple enough to find with the ip addr command however if you look at my system I have 5 interfaces (from docker, my VM network, Wireless, and local interfaces) finding your IP can be a challenge in all of that text. So you need something like grep to help at a very lease highlight what your looking for.

When you start searching the internet you'll likely find something similar to: (found here)
grep -E "(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]?)"
Now this does the job (and highlights just what I need), however it's quite the Regular Expression to remember.  If you know a bit about regular expressions you know that this can be simplified, to something like:
grep -E  "(([0-9]{1,3}\.){3}[0-9]{1,3})" 

Note: In the case of my interfaces this (shorter) grep statement does the same thing as the much longer one above. However it's not explicitly checking for valid IP's in the same way that the command above is.

Still not something I'm likely to remember of the cuff but, I at least have a chance of being able to understand what its doing at a quick glance (should I see it in a function, such as the one below). 
ip addr | awk '{print $NF "\t" $2}' | grep -E  "(([0-9]{1,3}\.){3}[0-9]{1,3})" |  sed -e 's/\/[0-9]\{1,2\}/ /g'

Its the little things (like opening a web browser)

Sometimes as your developing a solution/script for a problem your faced with interesting challenges where the dumbest workaround (opening a ...