Mohammedz.com

For Linux and Shell scripting.


3 Comments

Check IP format

#! /bin/bash

# check format of ip address
function iptest {
case “$*” in
“”|*[!0-9.]*) return 1;;
esac
local IFS=.
set — $*
[ $# -eq 4 ] &&
[ ${1:-555} -le 255 ] && [ ${2:-555} -le 255 ] &&
[ ${3:-555} -le 255 ] && [ ${4:-555} -le 255 ]
}

if iptest $1; then
echo “IP format is okay”
else
echo “Wrong ip format”
fi

—————————————————————-

[abdurahiman@49 sed]$ sh check_ip 192.168.1
Wrong ip format
[abdurahiman@49 sed]$ sh check_ip 192.168.1.2
IP format is okay
[abdurahiman@49 sed]$

—————————————————————-