function to verify an IP address - can be used at the shell prompt or in a shell script

function verifyIP() { octet="(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])"; ip4="^$octet\.$octet\.$octet\.$octet$"; [[ ${1} =~ $ip4 ]] && return 0 || return 1; }
When processing IP addresses in the shell (or shell script) it is useful to be able to verify that the value of data is an IP address (an not some random string or non-sensible IP address).
Sample Output
$ verifyIP 1.2.3.4; if [ $? = 0 ]; then echo "is IP"; else echo "is NOT ip"; fi
is IP

$ verifyIP not_an.IP ; if [ $? = 0 ]; then echo "is IP"; else echo "is NOT ip"; fi
is NOT ip

$ verifyIP 255.255.255.255 ; if [ $? = 0 ]; then echo "is IP"; else echo "$1 is NOT ip"; fi
is IP

$ verifyIP 256.255.255.255 ; if [ $? = 0 ]; then echo "is IP"; else echo "$1 is NOT ip"; fi
 is NOT ip

verifyIP 10.255.255.1255 ; if [ $? = 0 ]; then echo "is IP"; else echo "$1 is NOT ip"; fi
 is NOT ip

$ verifyIP a_random_string ; if [ $? = 0 ]; then echo "is IP"; else echo "$1 is NOT ip"; fi
 is NOT ip

$ verifyIP 192.168.1.1 && echo "is IP" || echo "$1 is NOT ip"
is IP

$ verifyIP the.quick.and.the.not.quick && echo "is IP" || echo "$1 is NOT ip"
 is NOT ip

1
By: mpb
2015-05-01 12:22:57

These Might Interest You

  • simple function i found somewhere to open vim help page from the shell use :h like you would in vim


    -1
    function :h { vim +":h $1" +'wincmd o' +'nnoremap q :q!<CR>' ;}
    Bonster · 2013-04-04 08:12:28 0
  • Bash process substitution which curls the website 'hashbang.sh' and executes the shell script embedded in the page. This is obviously not the most secure way to run something like this, and we will scold you if you try. The smarter way would be: Download locally over SSL > curl https://hashbang.sh >> hashbang.sh Verify integrty with GPG (If available) > gpg --recv-keys 0xD2C4C74D8FAA96F5 > gpg --verify hashbang.sh Inspect source code > less hashbang.sh Run > chmod +x hashbang.sh > ./hashbang.sh


    5
    sh <(curl hashbang.sh)
    lrvick · 2015-03-15 21:02:01 2
  • Running this command turns shell tracing and shell verbose debugging on or off. Not only does it do that, it also uses your terminals builtin method of setting colors to make debugging much easier. It looks at the current shell options contained in the $- special bash variable and that lets this function set the opposite of the current value. So from the shell you could do a: setx; echo "y" | ( cat -t ) | echo "d"; setx and it will turn on debbuggin. This is an amazingly useful function that is perfect to add system-wide by adding it to /etc/profile or /etc/bashrc.. You can run it from the shell, and you can also use it in your shell scripts like my .bash_profile - http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html Show Sample Output


    1
    function setx(){ sed '/[xv]/!Q2' <<< $- && { set +xv; export PS4=">>> "; } || { export PS4="`tput setaf 3`>>> `tput sgr0`"; set -xv; }; }
    AskApache · 2010-02-14 01:25:44 1
  • Some commands (such as sed and perl) have options to support in-place editing of files, but many commands do not. This shell function enables any command to change files in place. See the sample output for many examples. The function uses plain sh syntax and works with any POSIX shell or derivative, including zsh and bash. Show Sample Output


    1
    inplace() { eval F=\"\$$#\"; "$@" > "$F".new && mv -f "$F".new "$F"; }
    inof · 2010-04-09 11:36:31 8

What Others Think

That's useful. It's a good regex, but the function could manage a bit of tidying. . First, let's get rid of ip4 -- there's no need for it: function verifyIP() { octet="(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])"; [[ ${1} =~ ^$octet\.$octet\.$octet\.$octet$ ]] && return 0 || return 1; } . Next, drop the && return 0... The bash regex test [[ returns 0 or 1 already: function verifyIP() { octet="(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])"; [[ ${1} =~ ^$octet\.$octet\.$octet\.$octet$ ]]; } . Also the braces around $1 aren't needed function verifyIP() { octet="(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])"; [[ $1 =~ ^$octet\.$octet\.$octet\.$octet$ ]]; } . That's 117 characters rather than 155. . Personally, I'd also make two other changes: octet -> o, and I'd make the variable local to avoid polluting the environment. function verifyIP() { local o='(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])'; [[ $1 =~ ^$o\.$o\.$o\.$o$ ]]; }
flatcap · 160 weeks ago
@flatcap: thanks for your comments. Nifty trimming! :-)
mpb · 160 weeks ago
But 10.1 is a valid IP address Maybe this should wrap a function that returns IP address in dotted quad format. dottedquad "10.1" --> 10.0.0.1 verifyIP 10.0.0.1 --> true
miniker84 · 135 weeks and 2 days ago
@miniker84 I checked that VerifyIP function works for 10. addresses: function verifyIP() { octet="(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])"; ip4="^$octet\.$octet\.$octet\.$octet$"; [[ ${1} =~ $ip4 ]] && return 0 || return 1; } verifyIP 10.168.1.1 && echo true || echo false true IMHO, "10.1" is not an IP address and fails the test.
mpb · 129 weeks and 3 days ago

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?

commandlinefu.com 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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: