linux下批量绑定ip地址命令/shell脚本 ipassign.sh
#!/bin/bash
readvariables() {
echo -n "Enter interface (available: $avifaces): "
read interface
echo -n "Enter C Class (i.e 208.98.0): "
read subnet
echo -n "Enter Last octet of first IP (i.e 2): "
read start
echo -n "Enter Last octet of last IP (i.e 62): "
read end
echo -n "Enter subnetmask (Default: 255.255.255.192): "
read mask
if [ -z ${mask} ]; then
mask=255.255.255.192
fi
broadcast=255
}
debianassign() {
avifaces=`ifconfig | egrep "^[a-z]" | egrep -v "^lo " | awk '{ print $1 }' | tr '\n' ' ' | sed 's/ $//g'`
readvariables
echo -n "Enter clone number (default 0): "
read clone
if [ -z "$clone" ]; then
clone=0
fi
echo -e "\n" >> /etc/network/interfaces
until [ "$start" -gt "$end" ]; do echo -e "auto $interface:$clone\niface $interface:$clone inet static\n\taddress $subnet.$start\n\tnetmask $mask\n\tnetwork $subnet.0\n\tbroadca
st $subnet.255\n"; let "start += 1"; let "clone += 1"; done >> /etc/network/interfaces
}
redhatassign() {
avifaces=`ifconfig | egrep "^[a-z]" | egrep -v "^lo " | awk '{ print $1 }' | tr '\n' ' ' | sed 's/ $//g'`
readvariables
echo -n "Enter CLONENUM (default 0): "
read line
echo -e "IPADDR_START=$subnet.$start\nIPADDR_END=$subnet.$end\nNETMASK=$mask\nBROADCAST=$subnet.$broadcast\nCLONENUM_START=$line\n" > /etc/sysconfig/network-scripts/ifcfg-${inte
rface}-range0
/etc/rc.d/init.d/network restart
}
freebsdassign() {
avifaces=`ifconfig | egrep "^[a-z0-9]{1,5}\:" | cut -f1 -d':' | egrep -v "lo0" | tr '\n' ' ' | sed 's/ $//g'`
readvariables
if [ -d "/usr/local/directadmin" ]; then
curr=$start
until [ "$curr" -gt "$end" ]; do echo "${subnet}.${curr}" >> /usr/local/directadmin/data/admin/ip.list ; echo -e "gateway=\nnetmask=$mask\nns=\nreseller=\nstatus=free\nvalue="
> /usr/local/directadmin/data/admin/ips/${subnet}.${curr} ; mask=255 ; let "curr += 1" ; done
chown diradmin:diradmin /usr/local/directadmin/data/admin/ips/*
/usr/local/directadmin/scripts/boot.sh stop
/usr/local/directadmin/scripts/boot.sh start
else
curr=$start
until [ "$curr" -gt "$end" ]; do ifconfig $interface add $subnet.$curr netmask $mask ; let "curr += 1" ; done
if [ `cat /etc/rc.conf | grep -ch "alias[0-9]=" 2>/dev/null` -eq "0" ]; then
line=0
else
lastalias=`grep "^ifconfig_${interface}_alias[0-9]" /etc/rc.conf | sed "s/ifconfig_${interface}_alias//g" | cut -f1 -d'=' | sort -n | tail -1`
line=`expr $lastalias + 1`
fi
curr=$start
until [ "$curr" -gt "$end" ]; do echo "ifconfig_${interface}_alias${line}=\"inet $subnet.$curr netmask $mask\"" >> /etc/rc.conf ; let "curr += 1" ; let "line += 1" ; done
fi
}
redhatbased=`cat /etc/*version /etc/*release 2>/dev/null | egrep -chi "CentOS|RedHat|Fedora"`
if [ $redhatbased -ge "1" ]; then
redhatassign
elif [ `uname` == "FreeBSD" ]; then
freebsdassign
elif [ -f /etc/debian_version ]; then
debianassign
fi