Shell脚本之自动修改IP

分类:shell编程 阅读:37428 次

作为一名Linux SA,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者MAC绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名、ip信息、网关等配置。修改成特定的主机名在维护和管理方面也比较方便。如下脚本用途为:修改ip和主机名等相关信息,可以根据实际需求修改,举一反三!

  1. #!/bin/sh

  2. #auto Change ip netmask gateway scripts

  3. #wugk 2012-12-17

  4. cat <<EOF

  5. ++++++++自动修改ip和主机名等相关信息+++++++++

  6. ETHCONF=/etc/sysconfig/network-scripts/ifcfg-eth0

  7. HOSTS=/etc/hosts

  8. NETWORK=/etc/sysconfig/network

  9. DIR=/data/backup/`date +%Y%m%d`

  10. NETMASK=255.255.255.0

  11. +++++++++-------------------------+++++++++++

  12. EOF

  13. #Define Path 定义变量,可以根据实际情况修改

  14. ETHCONF=/etc/sysconfig/network-scripts/ifcfg-eth0

  15. HOSTS=/etc/hosts

  16. NETWORK=/etc/sysconfig/network

  17. DIR=/data/backup/`date +%Y%m%d`

  18. NETMASK=255.255.255.0

  19. echo "================================================"

  20. echo

  21. #定义change_ip函数

  22. function Change_ip ()

  23. {

  24. #判断备份目录是否存在,中括号前后都有空格,!叹号在shell表示相反的意思#

  25. if

  26. [ ! -d $DIR ];then

  27. mkdir -p $DIR

  28. fi

  29. echo "Now Change ip address ,Doing Backup Interface eth0"

  30. cp $ETHCONF $DIR

  31. grep "dhcp" $ETHCONF

  32. #如下$?用来判断上一次操作的状态,为0,表示上一次操作状态正确或者成功#

  33. if

  34. [ $? -eq 0 ];then

  35. #read -p 交互输入变量IPADDR,注冒号后有空格,sed -i 修改配置文件#

  36. read -p "Please insert ip Address:" IPADDR

  37. sed -i 's/dhcp/static/g' $ETHCONF

  38. #awk -F. 意思是以.号为分隔域,打印前三列#

  39. echo -e "IPADDR=$IPADDR\nNETMASK=$NETMASK\nGATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3}'`.254" >>$ETHCONF

  40. echo "This IP address Change success !"

  41. else

  42. echo -n "This $ETHCONF is static exist ,please ensure Change Yes or NO":

  43. read i

  44. fi

  45. if

  46. [ "$i" == "y" -o "$i" == "yes" ];then

  47. read -p "Please insert ip Address:" IPADDR

  48. count=(`echo $IPADDR|awk -F. '{print $1,$2,$3,$4}'`)

  49. #定义数组, ${#count[@]}代表获取变量值总个数#

  50. A=${#count[@]}

  51. #while条件语句判断,个数是否正确,不正确循环提示输入,也可以用[0-9]来判断ip#

  52. while

  53. [ "$A" -ne "4" ]

  54. do

  55. read -p "Please re Inster ip Address,example 192.168.0.11 ip": IPADDR

  56. count=(`echo $IPADDR|awk -F. '{print $1,$2,$3,$4}'`)

  57. A=${#count[@]}

  58. done

  59. #sed -e 可以连续修改多个参数#

  60. sed -i -e 's/^IPADDR/#IPADDR/g' -e 's/^NETMASK/#NETMASK/g' -e 's/^GATEWAY/#GATEWAY/g' $ETHCONF

  61. #echo -e \n为连续追加内容,并自动换行#

  62. echo -e "IPADDR=$IPADDR\nNETMASK=$NETMASK\nGATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3}'`.254" >>$ETHCONF

  63. echo "This IP address Change success !"

  64. else

  65. echo "This $ETHCONF static exist,please exit"

  66. exit $?

  67. fi


  68. }


  69. #定义hosts函数

  70. ############function hosts##############

  71. function Change_hosts ()

  72. {

  73. if

  74. [ ! -d $DIR ];then

  75. mkdir -p $DIR

  76. fi

  77. cp $HOSTS $DIR

  78. read -p "Please insert ip address": IPADDR

  79. host=`echo $IPADDR|sed 's/\./-/g'`

  80. cat $HOSTS |grep 127.0.0.1 |grep "$host"

  81. if

  82. [ $? -ne 0 ];then

  83. sed -i "s/127.0.0.1/127.0.0.1 $host/g" $HOSTS

  84. echo "This hosts change success "

  85. else

  86. echo "This $host IS Exist .........."

  87. fi

  88. }

  89. ###########fuction network###############

  90. #定义network函数

  91. function Change_network ()

  92. {

  93. if

  94. [ ! -d $DIR ];then

  95. mkdir -p $DIR

  96. fi

  97. cp $NETWORK $DIR

  98. read -p "Please insert ip address": IPADDR

  99. host=`echo $IPADDR|sed 's/\./-/g'`

  100. grep "$host" $NETWORK

  101. if

  102. [ $? -ne 0 ];then

  103. sed -i "s/^HOSTNAME/#HOSTNAME/g" $NETWORK

  104. echo "NETWORK=$host" >>$NETWORK

  105. else

  106. echo "This $host IS Exist .........."

  107. fi

  108. }


  109. #PS3一般为菜单提示信息#

  110. PS3="Please Select ip or hosts Menu":

  111. #select为菜单选择命令,格式为select $var in ..command.. do .... done

  112. select i in "Change_ip" "Change_hosts" "Change_network"


  113. do

  114. #case 方式,一般用于多种条件下的判断

  115. case $i in

  116. Change_ip )

  117. Change_ip

  118. ;;

  119. Change_hosts )

  120. Change_hosts

  121. ;;

  122. Change_network )

  123. Change_network

  124. ;;

  125. *)

  126. echo

  127. echo "Please Insert $0: Change_ip(1)|Change_hosts(2)|Change_network(3)"

  128. echo

  129. ;;

  130. esac


  131. done