背景說明
工作中經常會遇到一次上幾十台、幾百台服務器的情況
每當到這個時候小伙伴們拿台筆記本和一根網線,一台服務器、一台服務器的去修改idrac IP
為了節約這個工作量,利用dell的racadm工具,寫了下面這個腳本。只要運行起這個腳本,后面要做的就只是插拔網線的工作
安裝racadm工具包
[root@localhost ~]# curl -s http://linux.dell.com/repo/hardware/dsu/bootstrap.cgi | bash [root@localhost ~]# yum -y install srvadmin-all
准備工作
在采購時dell會提供一份服務器的sn列表,會根據這個列表規划好每台服務器的idrac IP,我們可以將此轉換成以逗號分隔的CSV文件如下:
[root@localhost ~]# cat idrac_ip_list AAAAAAA,10.10.10.2,255.255.255.0,10.10.10.1 BBBBBBB,10.10.10.3,255.255.255.0,10.10.10.1
腳本
[root@localhost ~]# cat set_idrac_ip.sh #!/usr/bin/env bash # @Author : Eric Winn # @Email : eng.eric.winn@gmail.com # @Time : 2018-07-27 14:47 # @Version : 1.0 # @File : set_idrac_ip # @Software : PyCharm # the file of idrac idrac_ip_list_file=${1} if [ ! -f "${idrac_ip_list_file}" ]; then echo "idrac_ip_list_file is not found!!!!" echo "$0 [idrac_ip_list_file]" exit 127 fi # idrac Default infomation idrac_default_ip=${2:-192.168.0.120} idrac_default_user=${3:-root} idrac_default_pass=${4:-calvin} # racadm bin racadm_bin=/opt/dell/srvadmin/sbin/racadm # install check srvadmin_install="curl -s http://linux.dell.com/repo/hardware/dsu/bootstrap.cgi | bash\nyum -y install srvadmin-all" test ! `ls ${racadm_bin} 2> /dev/null` && printf "Please run these commands as root to install racadm.\n\e[1;31m${srvadmin_install}\e[0m\n" && exit 1 RACADM="${racadm_bin} -r $idrac_default_ip -u $idrac_default_user -p $idrac_default_pass" # idrac history idrac_sn_history=() # idrac set set_idrac_ip(){ # get SN sn=`${RACADM} get BIOS.SysInformation.SystemServiceTag |grep SystemServiceTag |awk -F '=' '{print $2}'` if [ "${sn}s" == "s" ]; then echo "Get idrac sn is field!" return 1 fi # We only need the first 7 characters sn=${sn:0:7} echo "sn ======> ${sn}" for h_sn in ${idrac_sn_history[*]} do if [ "$h_sn" == "${sn}" ]; then echo "The ${sn} is already set." return 0 fi done # get new idrac_net from idrac_ip_list_file new_idrac_net=(`grep ${sn} $idrac_ip_list_file|awk -F ',' '{print $2,$3,$4}'`) if [ "${new_idrac_net}s" == "s" ]; then echo "The ${sn} is not in the ${idrac_ip_list_file}" return 1 fi echo "Setting the new ip: ${new_idrac_net[*]}" # set idrac ip ${RACADM} setniccfg -s ${new_idrac_net[*]} | grep successfully if [ $? -eq 0 ]; then idrac_sn_history=(${idrac_sn_history[*]} ${sn}) return 0 else echo "Set is field!" return 1 fi } # check internet check_internet() { pings="" echo -e "Connecting ...\c" while [ "${pings}s" == "s" ] do pings=`ping -c 2 $idrac_default_ip |awk 'NR==6 {print $4}'` if [ "${pings}s" == "s" ]; then echo -e ".\c" else echo fi done return 0 } # main function main() { while true do check_internet if [ $? -eq 0 ]; then echo "Let's starting set" set_idrac_ip if [ $? -eq 0 ]; then echo echo "Now , Please change to a new server" sleep 5 else echo echo "Please check it." sleep 15 fi fi done } main
運行
注:后面的idrac_ip是csv文件名
[root@localhost ~]# sh set_idrac_ip.sh idrac_ip Connecting ... Let's starting set sn ======> AAAAAAA Setting the new ip: 10.10.10.2 255.255.255.0 10.10.10.1 Static IP configuration enabled and modified successfully Now , Please change to a new server