1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #!/bin/sh
FILE="/etc/sysconfig/network-scripts/ifcfg-ens33" TEXT="TYPE=Ethernet\n\ PROXY_METHOD=none\n\ BROWSER_ONLY=no\n\ BOOTPROTO=static\n\ DEFROUTE=yes\n\ IPV4_FAILURE_FATAL=no\n\ IPV6INIT=yes\n\ IPV6_AUTOCONF=yes\n\ IPV6_DEFROUTE=yes\n\ IPV6_FAILURE_FATAL=no\n\ IPV6_ADDR_GEN_MODE=stable-privacy\n\ NAME=ens33\n\ DEVICE=ens33\n\ ONBOOT=yes\n\ IPADDR=192.168.119.150\n\ NETMASK=255.255.255.0\n\ GATEWAY=192.168.119.2\n\ DNS1=192.168.119.2"
IPADDR="IPPADDR=$1"
IPADDR=$1 _ckIPaddr () { ckStep1=`echo $1 | awk -F"." '{print NF}'` if [ $ckStep1 -eq 4 ] || [ $ckStep1 -eq 6 ] then ckStep2=`echo $1 | awk -F"." '{if ($1!=0 && $NF!=0) split ($0,IPNUM,".")} END \ { for (k in IPNUM) if (IPNUM[k]==0) print IPNUM[k]; else if (IPNUM[k]!=0 && IPNUM[k]!~/[a-z|A-Z]/ && length(IPNUM[k])<=3 && IPNUM[k]<255 && IPNUM[k]!~/^0/) print IPNUM[k]}'| wc -l` if [ $ckStep2 -eq $ckStep1 ] then echo 0 else echo 1 fi else echo 1 fi } CK_IPADDR=`_ckIPaddr $IPADDR`
if [ $CK_IPADDR -eq 1 ] then
echo "The string $IPADDR is not a correct ipaddr!!!" exit 1 fi
echo "" > $FILE
echo -e $TEXT > $FILE
IPADDRSTR="IPADDR="$IPADDR echo -e $IPADDRSTR >> $FILE
echo "NETMASK=255.255.255.0" >> $FILE
GATEWAY="GATEWAY=" DNS1="DNS1="
sum=0 IPADDR=${IPADDR//./ } for element in $IPADDR do let "sum++" if [ $sum -ge 4 ];then break fi
GATEWAY=$GATEWAY$element"." DNS1=$DNS1$element"." done
GATEWAY=$GATEWAY"2" DNS1=$DNS1"2"
echo -e $GATEWAY >> $FILE echo -e $DNS1 >> $FILE
service network restart
|