clacker
Group: Members
Posts: 570
Joined: June 2004 |
|
Posted: May 28 2005,00:28 |
|
Quote (friedgold @ May 06 2005,16:12) | Does any one know how the best way to check if a string is a valid ip address?
[ -z "$(echo "$i" | awk '/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$/')" ]
should check the string is in the form ###.###.###.### where the #'s are nos, but it doesn't rule out stuff like 999.999.999.999. |
Here is a neat way I saw using the cut command to parse out the string:
Code Sample | #!/bin/bash # pass the ip string as an argument to this script
if echo $1 | grep -q "^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$" then for i in 1 2 3 4 do export dotpart=` echo $1 | cut -f $i -d '.' ` if [ $dotpart -lt 0 -o $dotpart -gt 255 ] then echo "ip address was bad" exit 1 fi done echo "ip address was good" else echo "ip address was bad" exit 1 fi exit 0 |
|