Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19881579
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: LINUX

2006-12-04 11:19:47

#!/bin/sh
#For basic network config, Include Security Setting,root Password Setting,
#Print the script's help information
#History:
#1. add support MS300x blade server.
#   本脚本的使用方法
function output_usage()
{

         echo Usage as:
         echo $0 blade_type cfgdata_file_name
         echo blade_type define the type of blade server,it should be one of MD/ME/USC/EPG,and should be same defined in cfgdata file
         echo You should define the config parameter and save it in a config data file
         echo If you do not specify config data file,/usr/local/etc/basic_cfg.dat will be used ad config data file defaultly
}

#This function to check whether a IP Address is valid
#检查IP的有效性
function bad_ipaddr()
{
         if [ $# -ne 1 ]
         then
            echo "Usage:chk_ipaddr IP_ADDRESS"
            return 0
         fi
         echo $1 | sed 's/\./\ /g' | wc -w > field_cnt_tmp
         read ip_field_cnt < field_cnt_tmp
         if [ $ip_field_cnt -ne 4 ]
         then
            return 0
         fi
         rm -f field_cnt_tmp
         echo $1 | wc -c > char_cnt_tmp
         read char_cnt < char_cnt_tmp
         rm -f char_cnt_tmp
         dot_cnt=0
         pointer_cnt=1
         while [ $pointer_cnt -lt $char_cnt ]
         do
               echo $1 | cut -c$pointer_cnt > char_tmp
               read cur_char < char_tmp
               if [ $cur_char = "." ]
               then
                  dot_cnt=`expr $dot_cnt + 1`
               fi
               pointer_cnt=`expr $pointer_cnt + 1`
               rm -f char_tmp
         done
         if [ $dot_cnt -ne 3 ]
         then
               return 0
         fi
         echo $1 | cut -d . -f 1 > tmp_f1
         echo $1 | cut -d . -f 2 > tmp_f2
         echo $1 | cut -d . -f 3 > tmp_f3
         echo $1 | cut -d . -f 4 > tmp_f4
         read field1 < tmp_f1
         read field2 < tmp_f2
         read field3 < tmp_f3
         read field4 < tmp_f4
         rm -f tmp_f1 tmp_f2 tmp_f3 tmp_f4
         if [  -z $field1  ]
         then
            return 0
         fi
         if [  -z $field2  ]
         then
            return 0
         fi
         if [  -z $field3 ]
         then
            return 0
         fi
         if [ -z $field4 ]
         then
            return 0
         fi
         expr $field1 + 10 > /dev/null 2>&1
         if [ $? -ne 0 ]
         then
            return 0
         fi
         expr $field2 + 10 > /dev/null 2>&1
         if [ $? -ne 0 ]
         then
            return 0
         fi
         expr $field3 + 10 > /dev/null 2>&1
         if [ $? -gt 0 ]
         then
            return 0
         fi
         expr $field4 + 10 > /dev/null 2>&1
         if [ $? -ne 0 ]
         then
            return 0
         fi
         if [ $field1 -gt 255 ]
         then
            return 0
         fi
         if [ $field2 -gt 255 ]
         then
            return 0
         fi
         if [ $field3 -gt 255 ]
         then
            return 0
         fi
         if [ $field4 -gt 255 ]
         then
            return 0
         fi
         return 1
}
#This function is to check whether all parameters that defined in config data file are OK
function chk_all_parameter()
{
         ip_addr_set_ok=0
         if bad_ipaddr $IPADDR
         then
            echo "IPADDR=${IPADDR} is error"
            ip_addr_set_ok=1
         fi
         if bad_ipaddr $BROADCAST
         then
            echo "BROADCAST=${BROADCAST} is error"
            ip_addr_set_ok=1
         fi
         if bad_ipaddr $NETMASK
         then
            echo "NETMASK=${NETMASK} is error"
            ip_addr_set_ok=1
         fi
         if bad_ipaddr $NETWORK
         then
            echo "NETWORK=${NETWORK} is error"
            ip_addr_set_ok=1
         fi
         if bad_ipaddr $GATEWAY
         then
            echo "GATEWAY=${GATEWAY} is error"
            ip_addr_set_ok=1
         fi
         if [ $BLADE_TYPE == "USC" ];
         then
            if bad_ipaddr $ETH0_IPADDR
            then
               echo "ETH0_IPADDR=${ETH0_IPADDR} is error"
               ip_addr_set_ok=1
            fi
            if bad_ipaddr $ETH0_BROADCAST
            then
               echo "ETH0_BROADCAST=${ETH0_BROADCAST} is error"
               ip_addr_set_ok=1
            fi
            if bad_ipaddr $ETH0_NETMASK
            then
               echo "ETH0_NETMASK=${ETH0_NETMASK} is error"
               ip_addr_set_ok=1
            fi
            if bad_ipaddr $ETH0_NETWORK
            then
               echo "ETH0_NETWORK=${ETH0_NETWORK} is error"
               ip_addr_set_ok=1
            fi
         fi
         return ${ip_addr_set_ok}
}
#Create ifcfg-eth1 for MD/EPG/USC blade
function create_ifcfg_eth1()
{
         echo "DEVICE=eth1" > ${ETH_FOLDER}/ifcfg-eth1
         echo "BOOTPROTO=static"  >> ${ETH_FOLDER}/ifcfg-eth1
         echo "BROADCAST=${BROADCAST}"  >> ${ETH_FOLDER}/ifcfg-eth1
         echo "IPADDR=${IPADDR}" >> ${ETH_FOLDER}/ifcfg-eth1
         echo "NETMASK=${NETMASK}" >> ${ETH_FOLDER}/ifcfg-eth1
         echo "NETWORK=${NETWORK}" >> ${ETH_FOLDER}/ifcfg-eth1
         echo "GATEWAY=${GATEWAY}" >> ${ETH_FOLDER}/ifcfg-eth1
         echo "ONBOOT=yes" >> ${ETH_FOLDER}/ifcfg-eth1
}

#Create ifcfg-bond0/ifcfg-eth1/ifcfg-eth2/modules.conf(845/875) for ME blade
#Create ifcfg-bond0/ifcfg-eth0/ifcfg-eth1/modprobe.conf(3002/3003) for ME blade
function ME_BondConfig()
{
         #Create ifcfg-bond0 for ME BLADE
         echo "DEVICE=bond0" > ${ETH_FOLDER}/ifcfg-bond0
         echo "IPADDR=${IPADDR}" >> ${ETH_FOLDER}/ifcfg-bond0
         echo "NETMASK=${NETMASK}" >> ${ETH_FOLDER}/ifcfg-bond0
         echo "NETWORK=${NETWORK}" >> ${ETH_FOLDER}/ifcfg-bond0
         echo "BROADCAST=${BROADCAST}" >> ${ETH_FOLDER}/ifcfg-bond0
         echo "GATEWAY=${GATEWAY}" >> ${ETH_FOLDER}/ifcfg-bond0
         echo "ONBOOT=yes" >> ${ETH_FOLDER}/ifcfg-bond0
         echo "BOOTPROTO=none" >> ${ETH_FOLDER}/ifcfg-bond0
         echo "USERCTL=no" >> ${ETH_FOLDER}/ifcfg-bond0
         #Create ifcfg-eth1 for ME blade
         echo "DEVICE=eth1" > ${ETH_FOLDER}/ifcfg-eth1
         echo "USERCTL=no" >> ${ETH_FOLDER}/ifcfg-eth1
         echo "ONBOOT=yes" >> ${ETH_FOLDER}/ifcfg-eth1
         echo "MASTER=bond0" >> ${ETH_FOLDER}/ifcfg-eth1
         echo "SLAVE=yes" >> ${ETH_FOLDER}/ifcfg-eth1
         echo "BOOTPROTO=none" >> ${ETH_FOLDER}/ifcfg-eth1
         knl_ver=`uname -r`
         echo $knl_ver | cut -d . -f 1 > tmp_f1
         echo $knl_ver | cut -d . -f 2 > tmp_f2
         read primary_ver < tmp_f1
         read minor_ver   < tmp_f2
         rm -f tmp_f1 tmp_f2
         if [ $minor_ver -lt 5 ]
         then
            #Create ifcfg-eth2 for ME blade under 2.4.x 845/875 board
            echo "DEVICE=eth2" > ${ETH_FOLDER}/ifcfg-eth2
            echo "USERCTL=no" >> ${ETH_FOLDER}/ifcfg-eth2
            echo "ONBOOT=yes" >> ${ETH_FOLDER}/ifcfg-eth2
            echo "MASTER=bond0" >> ${ETH_FOLDER}/ifcfg-eth2
            echo "SLAVE=yes" >> ${ETH_FOLDER}/ifcfg-eth2
            echo "BOOTPROTO=none" >> ${ETH_FOLDER}/ifcfg-eth2
            #Update /etc/modules.conf
            grep -v bond ${HOSTS_FOLDER}/modules.conf > ${HOSTS_FOLDER}/modules.conf.tmp
            echo "alias bond0 bonding" >> ${HOSTS_FOLDER}/modules.conf.tmp
            echo "options bond0 mode=1 miimon=100 primary=eth1 updelay=100" >> ${HOSTS_FOLDER}/modules.conf.tmp
            mv ${HOSTS_FOLDER}/modules.conf.tmp ${HOSTS_FOLDER}/modules.conf
         else
            #Create ifcfg-eth0 for ME blade under 2.6.x 3002/3003 board
            echo "DEVICE=eth0" > ${ETH_FOLDER}/ifcfg-eth0
            echo "USERCTL=no" >> ${ETH_FOLDER}/ifcfg-eth0
            echo "ONBOOT=yes" >> ${ETH_FOLDER}/ifcfg-eth0
            echo "MASTER=bond0" >> ${ETH_FOLDER}/ifcfg-eth0
            echo "SLAVE=yes" >> ${ETH_FOLDER}/ifcfg-eth0
            echo "BOOTPROTO=none" >> ${ETH_FOLDER}/ifcfg-eth0
            #Update /etc/modprobe.conf
            grep -v bond ${HOSTS_FOLDER}/modprobe.conf > ${HOSTS_FOLDER}/modprobe.conf.tmp
            echo "alias bond0 bonding" >> ${HOSTS_FOLDER}/modprobe.conf.tmp
            echo "options bond0 mode=1 miimon=100 primary=eth1 updelay=100" >> ${HOSTS_FOLDER}/modprobe.conf.tmp
            mv ${HOSTS_FOLDER}/modprobe.conf.tmp ${HOSTS_FOLDER}/modprobe.conf
         fi
}

#Create hosts for MD/ME/USC/EPG blade
function create_hosts()
{
         echo "127.0.0.1 localhost" > ${HOSTS_FOLDER}/hosts
         echo "${IPADDR} ${HOSTNAME}" >> ${HOSTS_FOLDER}/hosts
}

#Create network for MD/ME/USC/EPG blade
function create_network()
{
         echo "NETWORKING=yes" > ${NETWORK_FOLDER}/network
         echo "HOSTNAME=${HOSTNAME}" >> ${NETWORK_FOLDER}/network
}

#Create ifcfg-eth0 for USC blade
function create_ifcfg_eth0()
{
         echo "DEVICE=eth0" > ${ETH_FOLDER}/ifcfg-eth0
         echo "BOOTPROTO=static"  >> ${ETH_FOLDER}/ifcfg-eth0
         echo "BROADCAST=${ETH0_BROADCAST}"  >> ${ETH_FOLDER}/ifcfg-eth0
         echo "IPADDR=${ETH0_IPADDR}" >> ${ETH_FOLDER}/ifcfg-eth0
         echo "NETMASK=${ETH0_NETMASK}" >> ${ETH_FOLDER}/ifcfg-eth0
         echo "NETWORK=${ETH0_NETWORK}" >> ${ETH_FOLDER}/ifcfg-eth0
         echo "ONBOOT=yes" >> ${ETH_FOLDER}/ifcfg-eth0
}
#Create /root/.bash_profile for all MD/ME/EPG/USC balde
function create_bashprofile()
{
         grep -v "/usr/local/ms_ma:/usr/local/mc_ma"  /root/.bash_profile > /root/bash_temp
         echo "export PATH=\$PATH:\$HOME/bin:/usr/local/mysql/bin:/sbin:/usr/local/ms_ma:/usr/local/mc_ma" >> /root/bash_temp
         echo "export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:/usr/local/ms_ma:/usr/local/mc_ma" >>  /root/bash_temp
         mv -f /root/bash_temp /root/.bash_profile
}

#Add user and set security for each blade
function securitySet()
{
         #Set the min length of password
         grep -v "PASS_MIN_LEN" /etc/login.defs > /etc/login.defs.tmp
         echo "PASS_MIN_LEN    ${PASS_MIN_LEN}" >> /etc/login.defs.tmp
         mv /etc/login.defs.tmp /etc/login.defs
         #Change password of root
         passwd root
}

function TZ_NTP_Set()
{
         cp -f /usr/share/zoneinfo/${TZ_FILE_NAME} /etc/localtime         
         #Update /etc/ntp.conf
         grep -v "server" /etc/ntp.conf > /etc/ntp.conf.tmp
         echo "server ${NTP_SVR1_ADDR} prefer  #local clock" >> /etc/ntp.conf.tmp
         echo "server ${NTP_SVR2_ADDR} prefer  " >> /etc/ntp.conf.tmp
         mv /etc/ntp.conf.tmp /etc/ntp.conf
         #synchronize the time with the NTP servers
         service ntpd stop
         ntpdate ${NTP_SVR1_ADDR}
         service ntpd start
}

#To check whether you had typed parameter correctly
#  脚本执行开始处,如果没有待任何参数, 则调用函数output_usage,输出用法并退出 $# 表示传入参数的个数,不包含shell 脚本名
if [ $# -eq 0 ]
   then
   output_usage
   exit 1
fi

if [ $# -eq 1 ]
#如果只有一个参数, 被认识只给出了blade type,没有给出配置文件,要检查默认的配置文件是否存在
   then
   if [ ! -e /usr/local/etc/basic_cfg.dat ]
      then
      echo "file /usr/local/etc/basic_cfg.dat does not exist or you should specify a config data file"
      output_usage
      exit 1
   else
      # /usr/local/etc/basic_cfg.dat was used as config file defaultly
      config_date_file=/usr/local/etc/basic_cfg.dat
   fi
else
   #check the config data file whether exist
    #如果给出2个以上的参数, 则把第2个参数当作配置文件,检查它是否存在.通过man test 可以获取各个具体参数的意思.
   if [ ! -e $2 ]
      then
      echo "File $2 does not exist"
      output_usage
      exit 1    #Error status
   else
      #include config data file
      config_date_file=$2  
   fi
fi
#执行配置文件
. ${config_date_file}

#确认第一个参数和配置文件中的板子类型一致.

if [ $1 != $BLADE_TYPE ];
then
   echo In config data file BLADE_TYPE=$BLADE_TYPE
   output_usage
   exit 1
fi


while [ 0 ]
do
#检查参数
   if chk_all_parameter
   then
      break
   else
      echo "Do you want to call vi to edit ${config_date_file}?Yes/No"
      read confirm_edit_data
      if [  $confirm_edit_data == "Yes" ]
      then
         vi ${config_date_file}
         . ${config_date_file}
      else
         echo "Exit the install"
         exit 1
      fi
   fi
done

#The following variable is used for debug or formal release
#For formal used,they should be set to
#ETH_FOLDER=/etc/sysconfig/network-scripts
#HOSTS_FOLDER=/etc
#NETWORK_FOLDER=/etc/sysconfig
#for debug,thay are all set to /tmp

formal_release=1
if [ $formal_release == 1 ];
   then
   ETH_FOLDER=/etc/sysconfig/network-scripts
   HOSTS_FOLDER=/etc
   NETWORK_FOLDER=/etc/sysconfig
else
   ETH_FOLDER=/tmp
   HOSTS_FOLDER=/tmp
   NETWORK_FOLDER=/tmp
fi

case $1 in
MD)
    create_ifcfg_eth1
    create_hosts
    create_network
    ;;
ME)
    create_hosts
    create_network
    ME_BondConfig
    ;;
EPG)
    create_ifcfg_eth1
    create_hosts
    create_network
    ;;
USC)
    create_ifcfg_eth1
    create_hosts
    create_network
    create_ifcfg_eth0
    ;;
*)
   echo "BLADE_TYPE should be one of MD/ME/EPG/USC"
   output_usage
   exit 1
   ;;
esac
hostname ${HOSTNAME}
#create_bashprofile

securitySet

service network restart

#TZ_NTP_Set
#echo "Basic network and system security setting finished,now reboot"
logout



配置文件的样板:
主要是板子类型,密码长度,IP方面的一些定义.
#this file is executed by mv_basic_cfg.sh, like
#       /usr/local/bin/mv_basic_cfg.sh blade_type config_file_name
BLADE_TYPE=EPG                    #define the blade server type(MD/ME/USC/EPG)

PASS_MIN_LEN=8

HOSTNAME=TKDDEPG01         #specify the host name of the blade
IPADDR=10.50.48.30
BROADCAST=10.50.48.255
NETMASK=255.255.255.0
NETWORK=10.50.48.0
GATEWAY=10.50.48.1

#The following item only for USC to config ETH0
ETH0_IPADDR=192.168.0.1
ETH0_BROADCAST=192.168.0.255
ETH0_NETMASK=255.255.255.0
ETH0_NETWORK=192.168.0.0

#******************************************************************************
#*                     below is the comment                                     *
#******************************************************************************
#BLADE_TYPE=   #define the blade server type, it should be as same as command parameter 1.
#it can be MD, ME, USC or EPG.
#example: BLADE_TYPE=EPG

#PASS_MIN_LEN=
#define minimum length of password may be used, it will affect /etc/login.defs
#example: PASS_MIN_LEN=8

#HOSTNAME=
#specify hostname, it will affect /etc/hosts and /etc/sysconfig/network and take effect immediately.
#example: HOSTNAME=TKDDEPG01

阅读(3373) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~