最近收到一个任务,突然觉得用shell来完成似乎比较方便,于是学着写了这个批量创建linux 虚拟ip的程序,这个在rhel有效,在ubunto上是不可以。
 
 
 
  
  1. #!/bin/bash 
  2.  
  3. ####################################################################################################### 
  4. #   Author      :   lkl_1981             
  5. #   Email       :   lkl_1981@163.com 
  6. #   QQ      :   37547134 
  7. #   Create time :   2012-07-01   
  8. #   Last time   :   2012-07-02 
  9. #   Useage      :   create batch virtual ip  only suppot c class ip  of currnet version  
  10. ######################################################################################################### 
  11.  
  12.  
  13. #eth="ifcfg-eth0" 
  14. #ip_start=50 
  15. #ip_length=10 
  16.  
  17. print_help() 
  18.         echo "Usage: vmeth.sh [OPTION]" 
  19.         echo "e.g.: vmeth -i ifcfg-eth0 -ips 192.168.0.200 -iplen 5" 
  20.  
  21. while [ $# -gt 0 ]; do   
  22.     case $1 in   
  23.         -h|--help)   
  24.             print_help   
  25.             exit 0   
  26.             ;;     
  27.         -V|--version)   
  28.             print_version   
  29.             exit 0   
  30.             ;;     
  31.         -ips|--ipstart)   
  32.             ip_start=$2   
  33.             shift 2   
  34.             ;;     
  35.         -iplen|--iplength)   
  36.             ip_length=$2   
  37.             shift 2   
  38.             ;;     
  39.         -i|--interface)   
  40.             eth=$2   
  41.             shift 2   
  42.             ;;     
  43.         -v|--verbose)   
  44.             verbose=true   
  45.             shift   
  46.             ;;     
  47.         --) 
  48.             shift   
  49.             break   
  50.             ;;   
  51.         *)   
  52.             echo "Internal Error: option processing error: $1" 1>&2   
  53.             exit 1   
  54.             ;;   
  55.     esac   
  56. done   
  57.  
  58. #echo $eth 
  59. #echo $ip_start 
  60. #echo $ip_length 
  61.  
  62. #:<<BLOCK 
  63.  
  64. ip=() 
  65.  
  66. ip[0]=`echo $ip_start|cut -d "." -f1` 
  67. ip[1]=`echo $ip_start|cut -d "." -f2` 
  68. ip[2]=`echo $ip_start|cut -d "." -f3` 
  69. ip[3]=`echo $ip_start|cut -d "." -f4` 
  70.  
  71.  
  72. eth_post=`expr substr $eth 7 4` 
  73. hwaddr=`ifconfig $eth_post|grep -i "hwaddr"|awk '{print $5}'` 
  74.  
  75. for((i=1;i<=ip_length;i++));do 
  76.  
  77. echo "DEVICE=$eth_post:"$i>>$eth":"$i 
  78. echo "BOOTPROTO=static">>$eth":"$i 
  79. echo "HWADDR="$hwaddr>>$eth":"$i 
  80. echo "IPADDR="${ip[0]}"."${ip[1]}"."${ip[2]}"."${ip[3]}>>$eth":"$i 
  81. echo "NETMASK=255.255.255.0">>$eth":"$i 
  82. echo "ONBOOT=yes">>$eth":"$i 
  83.  
  84. let "ip[3]=${ip[3]}+1" 
  85. done 
  86. #BLOCK