Network script & bash ip validator
Stefan Wold
ratler at lunar-linux.org
Thu Feb 3 12:32:45 UTC 2005
Good idea, but wrong place to add it. A check is already done to see if
ifconfig successfully set an ipaddress, otherwise it will tell you it
failed. Ifconfig would not accept invalid ipaddress's, which is
something your little hack does.
[0-9]{1,3} would allow addresses like 999.999.999.999.
If something like this really is needed a better place to add it would be
in the "lnet" tool, not in the start up scripts.
Sincerely
Stefan Wold
n Thu, 3 Feb 2005, Couannette wrote:
> Hi
>
> I modified /etc/init.d/network, adding an IP validator function.
> What do you think of that ?
>
> Best regards,
>
> Couannette
>
> --- network 2003-12-18 11:07:08.000000000 +0100
> +++ network.m 2005-02-03 12:16:19.757771241 +0100
> @@ -6,10 +6,27 @@
> # description: Activates/Deactivates all network interfaces configured to \
> # start at boot time.
> # probe: true
> -
> +set -x
> shopt -s extglob
> CONFIGS=/etc/config.d/network
>
> +to_lower () { tr 'A-Z' 'a-z' <<< "$*"; }
> +ip_validator () {
> + _IFS=$IFS
> + IFS=. numbers=( $1 )
> + IFS=$_IFS
> + if [ ${#numbers[@]} == 4 ] ; then
> + for i in ${numbers[*]} ; do
> + if [[ ! "$i" =~ "[0-9]{1,3}" ]] ; then
> + return 1
> + fi
> + done
> + else
> + return 1
> + fi
> + return 0
> +}
> +
> chkresult() {
> if [ $? -eq "-1" ] ; then
> echo -e $RESULT_FAIL
> @@ -67,23 +84,25 @@
> return -1;
> fi;
>
> - case $ADDRESS in
> - [dD][hH][cC][pP])
> + address=$(to_lower $ADDRESS)
> + case $address in
> + dhcp)
> if [ -z $DHCP_CLIENT ]; then
> echo -n "DHCP client not specified";
> return -1;
> fi;
> - case $DHCP_CLIENT in
> - [dD][hH][cC][pP][cC][dD])
> + dhcp_client=$(to_lower $DHCP_CLIENT)
> + case $dhcp_client in
> + dhcpcd)
> PIDFILE=/etc/dhcpc/dhcpcd-$1.pid
> RUNFILE=/usr/sbin/dhcpcd
> ;;
> - [dD][hH][cC][lL][iI][eE][nN][tT])
> + dhclient)
> PIDFILE=/var/run/dhclient-$1.pid
> RUNFILE=/sbin/dhclient
> DHCP_OPTIONS="$DHCP_OPTIONS -pf $PIDFILE -lf
> /var/state/dhcp/dhclient-$1.leases"
> ;;
> - [uU][dD][hH][cC][pP][cC])
> + udhcpc)
> PIDFILE=/var/run/udhcpc-$1.pid
> RUNFILE=/sbin/udhcpc
> DHCP_OPTIONS="$DHCP_OPTIONS -p $PIDFILE -i"
> @@ -114,7 +133,11 @@
> fi;
> fi;
> ;;
> -
> ?([0-9])?([0-9])[0-9].?([0-9])?([0-9])[0-9].?([0-9])?([0-9])[0-9].?([0-9])?([0-9])[0-9])
> + *) # we must validate IP addr
> + if ! ip_validator $address ; then
> + echo -n "IP address $address is not valid"
> + return -1
> + fi
> if [ ! -x /sbin/ifconfig ]; then
> echo -n "ifconfig not installed";
> return -1;
> @@ -152,14 +175,16 @@
>
> . $CONFIGS/$1;
>
> - case $ADDRESS in
> - [dD][hH][cC][pP])
> - case $DHCP_CLIENT in
> - [dD][hH][cC][pP][cC][dD])
> + address=$(to_lower $ADDRESS)
> + case $address in
> + dhcp)
> + dhcp_client=$(to_lower $DHCP_CLIENT)
> + case $dhcp_client in
> + dhcpcd)
> KILLCMD="dhcpcd -k"
> PIDFILE=/etc/dhcpc/dhcpcd-$1.pid
> ;;
> - [dD][hH][cC][lL][iI][eE][nN][tT])
> + dhclient)
> PIDFILE=/var/run/dhclient-$1.pid
> ;;
> esac
> @@ -187,7 +212,11 @@
> esac
> fi;
> ;;
> -
> ?([0-9])?([0-9])[0-9].?([0-9])?([0-9])[0-9].?([0-9])?([0-9])[0-9].?([0-9])?([0-9])[0-9])
> + *) # we must validate IP addr
> + if ! ip_validator $address ; then
> + echo -n "IP address $address is not valid"
> + return -1
> + fi
> if [ `ifconfig | grep $1 | wc -l` -eq 1 ]; then
> if [ ! -x /sbin/ifconfig ]; then
> echo -n "ifconfig not installed";
More information about the Lunar
mailing list