spid3r logo
Home Info Notes Projects Talk Pictures Videos

Welcome to the Internet

Baby Dunkin..

Dunking the baby in the pool!

Baby dunk

Posted Sun 14th of February - Updated Sun 14th of February

Bash validate IP Address

Here is some code i found to validate ip addresses in bash.

Works good too..
#!/bin/bash
IPADDR="192.168.0.1"

function valid_ip(){
  ERROR=0
    oldIFS=$IFS
    IFS=.
    set -f
    set -- $1
    if [ $# -eq 4 ]
    then
      for seg
      do
        case $seg in
            ""|*[!0-9]*) ERROR=1;break ;;
            ## Segment empty or non-numeric char
            *) [ $seg -gt 255 ] && ERROR=2 ;;
        esac
      done
    else
      ERROR=3 ## Not 4 segments
    fi
    IFS=$oldIFS
    set +f
    return $ERROR
}

if valid_ip $IPADDR ; then
  echo "IP addr is OK"
else
  echo "IP addr is invalid"
fi

Posted Sat 13th of February - Updated Sat 13th of February

The first post of my new site

This is my first post of my new site.

I hope there wil be more as ive spent a couple of hours working on this new site!

<welcome>Welcom to my new site</welcome>
Posted Sat 13th of February
Current

Talk

Sub Menu

Option 1

Option 2

Option 3

Option 4