Validate IP

#!/bin/bash

# What a nice pice of shell-programing... thx to Chris F.A. Johnson @ comp.unix.shell
is_ip() {
	case "$*" in
		""|*[!0-9.]*|*[!0-9]) return 1 ;;
	esac
	local IFS=.
	set -- $*
	[ $# -eq 4 ] &&
	[ ${1:-666} -le 255 ] && [ ${2:-666} -le 255 ] &&
	[ ${3:-666} -le 255 ] && [ ${4:-666} -le 254 ]
}

ip=
until is_ip $ip; do
  echo -n "Please enter the highest ip in the range: "
  read ip
done
This entry was posted in Bash. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.