linux下批量绑定ip地址命令/shell脚本 ipassign.sh

#!/bin/bash
readvariables() {echo -n "Enter interface (available: $avifaces): "read interfaceecho -n "Enter C Class (i.e 208.98.0): "read subnetecho -n "Enter Last octet of first IP (i.e 2): "read startecho -n "Enter Last octet of last IP (i.e 62): "read endecho -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’`readvariablesecho -n "Enter clone number (default 0): "read cloneif [ -z "$clone" ]; thenclone=0fiecho -e "n" >> /etc/network/interfacesuntil [ "$start" -gt "$end" ]; do echo -e "auto $interface:$cloneniface $interface:$clone inet staticntaddress $subnet.$startntnetmask $maskntnetwork $subnet.0ntbroadcast $subnet.255n"; 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’`readvariablesecho -n "Enter CLONENUM (default 0): "read lineecho -e "IPADDR_START=$subnet.$startnIPADDR_END=$subnet.$endnNETMASK=$masknBROADCAST=$subnet.$broadcastnCLONENUM_START=$linen" > /etc/sysconfig/network-scripts/ifcfg-${interface}-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=$masknns=nreseller=nstatus=freenvalue=" > /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 startelse 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" ; donefi}
redhatbased=`cat /etc/*version /etc/*release 2>/dev/null | egrep -chi "CentOS|RedHat|Fedora"`if [ $redhatbased -ge "1" ]; then redhatassignelif [ `uname` == "FreeBSD" ]; then freebsdassignelif [ -f /etc/debian_version ]; then        debianassignfi

发表评论

电子邮件地址不会被公开。 必填项已用*标注