PC Server远程管理卡用户管理脚本实现

it2024-11-30  20

1.IPMI工作原理图: 

2.脚本实现流程图:

3.适配服务器机型:

 4.演示效果:

 

 5.实现代码:

1 #!/usr/bin/env bash 2 3 # Author : JACK ZHAO 4 # Date : 2017/11/7 5 # Description : This script is userd to manage the PC server remote management card account. 6 7 ###Global variable### 8 WORKDIR=`pwd` 9 SSH_PORT=22 10 IPMI_PORT=623 11 instool=${WORKDIR}/instool3.0-pre/instool.sh 12 export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin 13 export LC_ALL=C 14 15 /bin/stty erase ^H 16 [ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions 17 [ ! -d ${WORKDIR}/temp ] && mkdir ${WORKDIR}/temp 18 19 ###Common function### 20 colour (){ 21 case $1 in 22 default) 23 echo -ne "\033[0m" 24 ;; 25 default_red) 26 echo -ne "\033[31m" 27 ;; 28 esac 29 } 30 31 aciton_false(){ 32 action "$1" /bin/false 33 } 34 35 action_true(){ 36 action "$1" /bin/true 37 } 38 39 ###Defining countdown### 40 readsecond(){ 41 for i in `seq -w 20 -1 1 `;do 42 echo -ne "\b\b\b\b\b\b\b\b\b\b\b\b\bCountdown:\033[1;34m${i}\033[0ms" 43 sleep 1 44 done 45 } 46 47 ###Check the IP format### 48 check_ip_format(){ 49 ipcalc -cs $1 50 if [ $? -eq 0 ];then 51 return 0 52 else 53 return $RC 54 fi 55 } 56 57 ###Check whether IP is online### 58 check_ip_alive(){ 59 ping -c 2 -w 2 $1 &>/dev/null 60 if [ $? -eq 0 ];then 61 return 0 62 else 63 return $RC 64 fi 65 } 66 67 ###Check Port### 68 check_port(){ 69 nc -vuz $1 $2 &>/dev/null 70 if [ $? -eq 0 ];then 71 return 0 72 else 73 return 110 74 fi 75 } 76 77 ###Summary of inspection conditions### 78 chenck_ip_port(){ 79 check_ip_format $1 80 if [ $? -ne 0 ];then 81 aciton_false "$1 Invalid IP." 82 return 110 83 fi 84 check_ip_alive $1 85 if [ $? -ne 0 ];then 86 aciton_false "$1 Network connection failed." 87 return 110 88 fi 89 check_port $IP $IPMI_PORT 90 if [ $? -ne 0 ];then 91 aciton_false "$1 UDP 623 Port connection failed." 92 return 110 93 fi 94 return 0 95 } 96 97 ###Public function 1### 98 enter_account_ip(){ 99 while true;do 100 read -p "Input Your IP:" IP 101 chenck_ip_port ${IP} 102 [ $? -ne 0 ] && enter_account_ip 103 read -p "Input Your Administrator:" USERNAME 104 read -s -p "Input Your Password:" PASSWORD 105 echo "" 106 read -p "Are You Sure?(y/n):" ANSWER 107 if [ -z $USERNAME ] && [ -z ${PASSWORD} ];then 108 colour default_red 109 echo "Username and password cannot be empty." 110 colour default 111 continue 112 fi 113 case ${ANSWER} in 114 y|Y|yes|YES) break;; 115 *) ;; 116 esac 117 done 118 } 119 120 ###Check the DELL IPMI state### 121 check_dell_ipmi(){ 122 DELL_IPMI_STATUS=`sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet ${2}@$1 racadm get iDRAC.IPMILan.Enable | tail -n1 | awk -F "=" '{print$2}'` 123 if [ "$DELL_IPMI_STATUS" == "Disabled" ];then 124 sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet ${2}@$1 racadm set iDRAC.IPMILan.Enable Enabled 125 if [ $? -ne 0 ];then 126 aciton_false "$1 Open the IPMI server failure." 127 exit 110 128 fi 129 fi 130 } 131 132 ###User list### 133 query_user_list(){ 134 echo "-----------------------User List---------------------------" 135 ipmitool -I lanplus -U ${USERNAME} -P ${PASSWORD} -H ${IP} user list 136 if [ $? -ne 0 ];then 137 colour default_red 138 echo "ERROR: Incorrect username or password,Please try again!" 139 colour default 140 fi 141 return 110 142 } 143 144 ###Use help### 145 useage(){ 146 cat << EOF 147 +------------------------------------------------------------------------------+ 148 |Notice:Please save the server IP address to the file in the following format. | 149 |For example: | 150 |--------------+--------------+--------------+---------------+-----------------| 151 |Create user:              | 152 |IP Login_username Login_password new_username new_password | 153 |192.168.70.125 root calvin userid hdli2016 | 154 |...           | 155 |Update user:           | 156 |IP Login_username Login_password update_username new_password | 157 |192.168.70.125 root calvin userid hdli2016 | 158 |...           | 159 |Delete user:              | 160 |IP Login_username Login_password delete_username | 161 |192.168.70.125 root calvin userid      | 162 |...             | 163 |Disable user:              | 164 |IP Login_username Login_password Disable_username | 165 |192.168.70.125 root calvin userid      | 166 |... | 167 |--------------+--------------+--------------+---------------+-----------------| 168 |NAME | 169 | PC_RAC_ACCOUNTS_MGT_TOOL.sh version 1.0.0 | 170 |Detailed | 171 | http://www.cnblogs.com/changmingzhao/p/7826333.html | 172 +------------------------------------------------------------------------------+ 173 EOF 174 readsecond 175 } 176 177 ###Check whether the user exists### 178 check_user_exist(){ 179 ipmitool -I lanplus -U $1 -P $2 -H $3 user list | grep -wv "ID" | awk '{print$1"\t"$2}' | egrep -v "true|false" > ${WORKDIR}/temp/user_info.temp 2> /dev/null 180 if `grep -wq "$4" ${WORKDIR}/temp/user_info.temp`;then 181 return 0 182 else 183 return 110 184 fi 185 } 186 187 ###Check the username and password### 188 check_login_username_passwd(){ 189 ipmitool -I lanplus -U $1 -P $2 -H $3 user list &> /dev/null 190 if [ $? -eq 0 ];then 191 return 0 192 else 193 return 110 194 fi 195 } 196 197 ###2.3 Adding HP server ILO users### 198 add_user_hp(){ 199 check_login_username_passwd $2 $3 $1 200 if [ $? -ne 0 ];then 201 aciton_false "$1 Incorrect Login account." 202 else 203 check_user_exist $2 $3 $1 $4 204 if [ $? -eq 0 ];then 205 aciton_false "$1 User:$4 Already Exist." 206 else 207 sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $2@$1 create /map1/accounts1 username=$4 password=$5 name=$4 group=admin,config,oemhp_vm,oemhp_rc,oemhp_power &> /dev/null 208 if [ $? -eq 0 ];then 209 SN=`ipmitool -I lanplus -U $4 -P $5 -H $1 fru list | grep -aw "Product Serial" | awk -F ":" '{print$2}' | head -n1 | sed 's#[[:space:]]##g'` 210 action_true "$1 SN:$SN User:$4" 211 else 212 aciton_false "$1 Unknown Error." 213 fi 214 fi 215 fi 216 } 217 218 ###2.2 Adding Inspur server BMC users### 219 add_user_inspur(){ 220 check_login_username_passwd $2 $3 $1 221 if [ $? -ne 0 ];then 222 aciton_false "$1 Incorrect Login account." 223 else 224 check_user_exist $2 $3 $1 $4 225 if [ $? -eq 0 ];then 226 aciton_false "$1 User:$4 Already Exist." 227 else 228 ${instool} $1 $2 $3 -lan -user -list | awk '{print$1}' | grep -v "ID" > ${WORKDIR}/temp/user_id_inspur.temp 2> /dev/null 229 for ((i=2;i++;i<=15));do 230 grep -qw $i ${WORKDIR}/temp/user_id_inspur.temp && continue 231 ${instool} $1 $2 $3 -lan -user -addm $i $4 $5 admin &> /dev/null 232 break 233 done 234 ${instool} $1 $4 $5 -lan -user -list &>/dev/null 235 if [ $? -eq 0 ];then 236 SN=`ipmitool -I lanplus -U $4 -P $5 -H $1 fru list | grep "Product Serial" | awk -F ":" '{print$2}'` 237 action_true "$1 SN:$SN User:$4" 238 else 239 aciton_false "$1 Unknown Error." 240 fi 241 fi 242 fi 243 } 244 245 ###2.1/2.4/2.5 Adding Lenovo, Huawei, DELL server to remote management users### 246 add_user_common(){ 247 check_login_username_passwd $2 $3 $1 248 if [ $? -ne 0 ];then 249 aciton_false "$1 Incorrect Login account." 250 else 251 check_user_exist $2 $3 $1 $4 252 if [ $? -eq 0 ];then 253 aciton_false "$1 User:$4 Already Exist." 254 else 255 USERID=`awk '{print$1}' ${WORKDIR}/temp/user_info.temp` 256 for ((i=2;i++;i<=13));do 257 echo $USERID | grep -qw $i && continue 258 ipmitool -I lanplus -U $2 -P $3 -H $1 user set name $i $4 &>/dev/null 259 ipmitool -I lanplus -U $2 -P $3 -H $1 user set password $i $5 &>/dev/null 260 ipmitool -I lanplus -U $2 -P $3 -H $1 user priv $i 4 &>/dev/null 261 ipmitool -I lanplus -U $2 -P $3 -H $1 channel setaccess 1 $i callin=on ipmi=on link=on privilege=4 262 ipmitool -I lanplus -U $2 -P $3 -H $1 user enable $i &>/dev/null 263 if [ ${CHOICE_STEP2} -eq 25 ];then 264 sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $2@$1 racadm set idrac.users.${i}.Privilege 0x1ff &>/dev/null 265 fi 266 ipmitool -I lanplus -U $2 -P $3 -H $1 user test $i 16 $5 &>/dev/null 267 STATUS_RC=$? 268 break 269 done 270 if [ $STATUS_RC -eq 0 ];then 271 if [ ${CHOICE_STEP2} -eq 25 ];then 272 sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $2@$1 racadm get BIOS.SysInformation.SystemServiceTag | tail -n1 > ${WORKDIR}/temp/dell_sn.temp 273 SN=`awk -F "=" '{print$2}' ${WORKDIR}/temp/dell_sn.temp` 274 else 275 SN=`ipmitool -I lanplus -U $2 -P $3 -H $1 fru list | grep -w "Product Serial" | awk -F ":" '{print$2}'` 276 fi 277 action_true "$1 SN:$SN User:$4" 278 else 279 aciton_false "$1 Unknown Error." 280 fi 281 fi 282 fi 283 } 284 285 ###2.Add the user's common function### 286 add_batch_task(){ 287 while true;do 288 read -p "[Please enter the path to the IP list File]:" FILE_PATH 289 if [ ! -e ${FILE_PATH} ] || [ -z ${FILE_PATH} ]; then 290 colour default_red 291 echo "Error: ${FILE_PATH}: No such file or directory." 292 colour default 293 continue 294 fi 295 read -p "Are You Sure?(y/n):" ANSWER 296 case ${ANSWER} in 297 y|Y|yes|YES) echo "Adding user.This may take serveral minutes,Please wait......";; 298 *) continue;; 299 esac 300 sed '1d' ${FILE_PATH} > ${WORKDIR}/temp/list.temp 2>/dev/null 301 while read line;do 302 IP=`echo $line | awk '{print $1}'` 303 chenck_ip_port ${IP} 304 if [ $? -ne 0 ];then 305 continue 306 fi 307 LOGIN_USERNAME=`echo $line | awk '{print $2}'` 308 LOGIN_PASSWORD=`echo $line | awk '{print $3}'` 309 NEW_USERNAME=`echo $line | awk '{print $4}'` 310 NEW_PASSWORD=`echo $line | awk '{print $5}'` 311 if [ ${CHOICE_STEP2} -eq 21 ];then 312 add_user_common $IP $LOGIN_USERNAME $LOGIN_PASSWORD $NEW_USERNAME $NEW_PASSWORD 313 elif [ ${CHOICE_STEP2} -eq 22 ];then 314 add_user_inspur $IP $LOGIN_USERNAME $LOGIN_PASSWORD $NEW_USERNAME $NEW_PASSWORD 315 elif [ ${CHOICE_STEP2} -eq 23 ];then 316 add_user_hp $IP $LOGIN_USERNAME $LOGIN_PASSWORD $NEW_USERNAME $NEW_PASSWORD 317 elif [ ${CHOICE_STEP2} -eq 24 ];then 318 add_user_common $IP $LOGIN_USERNAME $LOGIN_PASSWORD $NEW_USERNAME $NEW_PASSWORD 319 elif [ ${CHOICE_STEP2} -eq 25 ];then 320 check_dell_ipmi $IP $LOGIN_USERNAME $LOGIN_PASSWORD 321 add_user_common $IP $LOGIN_USERNAME $LOGIN_PASSWORD $NEW_USERNAME $NEW_PASSWORD 322 fi 323 done < ${WORKDIR}/temp/list.temp 324 break 325 done 326 } 327 328 ###3.1/3.2/3.3/3.4/3.5Modify Lenovo, Inspur, HP, Huawei, Dell server user password### 329 update_user_common(){ 330 check_login_username_passwd $2 $3 $1 331 if [ $? -ne 0 ];then 332 aciton_false "$1 Incorrect Login account." 333 else 334 check_user_exist $2 $3 $1 $4 335 if [ $? -ne 0 ];then 336 aciton_false "$1 User:$4 Not Exists." 337 else 338 USER_ID=`ipmitool -I lanplus -U $2 -P $3 -H $1 user list | grep -w "$UPDATE_USERNAME" | awk '{print$1}'` 339 ipmitool -I lanplus -U $2 -P $3 -H $1 user set password $USER_ID $5 &>/dev/null 340 ipmitool -I lanplus -U $2 -P $3 -H $1 user test $USER_ID 16 $5 &>/dev/null 341 if [ $? -eq 0 ];then 342 if [ ${CHOICE_STEP3} -eq 35 ];then 343 sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $2@$1 racadm get BIOS.SysInformation.SystemServiceTag | tail -n1 > ${WORKDIR}/temp/dell_sn.temp 344 SN=`awk -F "=" '{print$2}' ${WORKDIR}/temp/dell_sn.temp` 345 else 346 SN=`ipmitool -I lanplus -U $2 -P $3 -H $1 fru list | grep -w "Product Serial" | awk -F ":" '{print$2}' | head -n1 | sed 's#[[:space:]]##g'` 347 fi 348 action_true "$1 SN:$SN User:$4" 349 else 350 aciton_false "$1 Unknown Error." 351 fi 352 fi 353 fi 354 } 355 356 ###3.Modifying the user password public function### 357 update_batch_task(){ 358 while true;do 359 read -p "[Please enter the path to the IP list File]:" FILE_PATH 360 if [ ! -e ${FILE_PATH} ] || [ -z ${FILE_PATH} ];then 361 colour default_red 362 echo "Error: ${FILE_PATH}: No such file or directory." 363 colour default 364 continue 365 fi 366 sed '1d' ${FILE_PATH} > ${WORKDIR}/temp/list.temp 2>/dev/null 367 read -p "Are You Sure?(y/n):" ANSWER 368 case ${ANSWER} in 369 y|Y|yes|YES) echo "Updating user.This may take serveral minutes,Please wait......";; 370 *) continue;; 371 esac 372 while read line;do 373 IP=`echo $line | awk '{print $1}'` 374 chenck_ip_port ${IP} 375 if [ $? -ne 0 ];then 376 continue 377 fi 378 LOGIN_USERNAME=`echo $line | awk '{print $2}'` 379 LOGIN_PASSWORD=`echo $line | awk '{print $3}'` 380 UPDATE_USERNAME=`echo $line | awk '{print $4}'` 381 NEW_PASSWORD=`echo $line | awk '{print $5}'` 382 if [ ${CHOICE_STEP3} -eq 35 ];then 383 check_dell_ipmi $IP $LOGIN_USERNAME $LOGIN_PASSWORD 384 fi 385 update_user_common $IP $LOGIN_USERNAME $LOGIN_PASSWORD $UPDATE_USERNAME $NEW_PASSWORD 386 done < ${WORKDIR}/temp/list.temp 387 break 388 done 389 } 390 391 ###4.1 Delete lenovo server IMM users### 392 delete_user_lenovo(){ 393 check_port $1 $SSH_PORT 394 if [ $? -ne 0 ];then 395 aciton_false "$1 TCP 22 Port connection failed." 396 else 397 check_login_username_passwd $2 $3 $1 398 if [ $? -ne 0 ];then 399 aciton_false "$1 Incorrect Login account." 400 else 401 check_user_exist $2 $3 $1 $4 402 if [ $? -ne 0 ];then 403 aciton_false "$1 User:$4 Not Exists." 404 else 405 USER_ID_LENOVO=`sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $2@$1 -n "users" | grep -w "$4" | awk '{print$1}'` 406 sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $2@$1 users -${USER_ID_LENOVO} -clear &> /dev/null 407 if [ $? -eq 0 ];then 408 SN=`ipmitool -I lanplus -U $2 -P $3 -H $1 fru list | grep "Product Serial" | awk -F ":" '{print$2}'` 409 action_true "$1 SN:$SN User:$4" 410 else 411 aciton_false "$1 Unknown Error." 412 fi 413 fi 414 fi 415 fi 416 } 417 418 ###4.2 Delete the Inspur server BMS user### 419 delete_user_inspur(){ 420 check_login_username_passwd $2 $3 $1 421 if [ $? -ne 0 ];then 422 aciton_false "$1 Incorrect Login account." 423 else 424 check_user_exist $2 $3 $1 $4 425 if [ $? -ne 0 ];then 426 aciton_false "$1 User:$4 Not Exists." 427 else 428 USER_ID=`${instool} $1 $2 $3 -lan -user -list |grep -w "$4" | awk '{print$1}'` 429 ${instool} $1 $2 $3 -lan -user -del ${USER_ID} 430 if [ $? -eq 0 ];then 431 SN=`ipmitool -I lanplus -U $2 -P $3 -H $1 fru list | grep "Product Serial" | awk -F ":" '{print$2}'` 432 action_true "$1 SN:$SN User:$4" 433 else 434 aciton_false "$1 Unknown Error." 435 fi 436 fi 437 fi 438 } 439 440 ###4.3 Delete the HP server BMS user### 441 delete_user_hp(){ 442 check_login_username_passwd $2 $3 $1 443 if [ $? -ne 0 ];then 444 aciton_false "$1 Incorrect Login account." 445 else 446 check_user_exist $2 $3 $1 $4 447 if [ $? -ne 0 ];then 448 aciton_false "$1 User:$4 Not Exists." 449 else 450 sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $2@$1 delete /map1/accounts1/$4 &> /dev/null 451 check_user_exist $2 $3 $1 $4 452 if [ $? -ne 0 ];then 453 SN=`ipmitool -I lanplus -U $2 -P $3 -H $1 fru list | grep -aw "Product Serial" | awk -F ":" '{print$2}' | head -n1 | sed 's#[[:space:]]##g'` 454 action_true "$1 SN:$SN User:$4" 455 else 456 aciton_false "$1 Unknown Error." 457 fi 458 fi 459 fi 460 } 461 462 ###4.4 Delete the HUAWEI server BMS user### 463 delete_user_huawei(){ 464 check_login_username_passwd $2 $3 $1 465 if [ $? -ne 0 ];then 466 aciton_false "$1 Incorrect Login account." 467 else 468 check_user_exist $2 $3 $1 $4 469 if [ $? -ne 0 ];then 470 aciton_false "$1 User:$4 Not Exists." 471 else 472 /usr/bin/expect << EOF 473 set timeout 20 474 log_user 0 475 spawn sshpass -p $3 ssh -o StrictHostKeyChecking=no -o LogLevel=quiet ${2}@$1 476 expect "*iBMC:/->" 477 send "ipmcset -t user -d deluser -v ${4}\r" 478 expect "Input your password:" 479 send "${3}\r" 480 expect "*iBMC:/->" 481 send "exit\r" 482 expect "*#" 483 EOF 484 check_user_exist $2 $3 $1 $4 485 if [ $? -ne 0 ];then 486 SN=`ipmitool -I lanplus -U $2 -P $3 -H $1 fru list | grep -aw "Product Serial" | awk -F ":" '{print$2}' | head -n1 | sed 's#[[:space:]]##g'` 487 action_true "$1 SN:$SN User:$4" 488 else 489 aciton_false "$1 Unknown Error." 490 fi 491 fi 492 fi 493 } 494 495 ###4.Delete server remote management card user common function ### 496 delete_batch_task(){ 497 while true;do 498 read -p "[Please enter the path to the IP list File]:" FILE_PATH 499 if [ ! -e ${FILE_PATH} ] || [ -z ${FILE_PATH} ];then 500 colour default_red 501 echo "Error: ${FILE_PATH}: No such file or directory." 502 colour default 503 continue 504 fi 505 sed '1d' ${FILE_PATH} > ${WORKDIR}/temp/list.temp 2>/dev/null 506 read -p "Are You Sure?(y/n):" ANSWER 507 case ${ANSWER} in 508 y|Y|yes|YES) echo "Deleting user.This may take serveral minutes,Please wait......";; 509 *) continue;; 510 esac 511 cat ${WORKDIR}/temp/list.temp | while read line;do 512 IP=`echo $line | awk '{print $1}'` 513 chenck_ip_port ${IP} 514 if [ $? -ne 0 ];then 515 continue 516 fi 517 LOGIN_USERNAME=`echo $line | awk '{print $2}'` 518 LOGIN_PASSWORD=`echo $line | awk '{print $3}'` 519 DELETE_USERNAME=`echo $line | awk '{print $4}'` 520 if [ ${CHOICE_STEP4} -eq 41 ];then 521 delete_user_lenovo $IP $LOGIN_USERNAME $LOGIN_PASSWORD $DELETE_USERNAME 522 elif [ ${CHOICE_STEP4} -eq 42 ];then 523 delete_user_inspur $IP $LOGIN_USERNAME $LOGIN_PASSWORD $DELETE_USERNAME 524 elif [ ${CHOICE_STEP4} -eq 43 ];then 525 delete_user_hp $IP $LOGIN_USERNAME $LOGIN_PASSWORD $DELETE_USERNAME 526 elif [ ${CHOICE_STEP4} -eq 44 ];then 527 delete_user_huawei $IP $LOGIN_USERNAME $LOGIN_PASSWORD $DELETE_USERNAME 528 fi 529 done 530 break 531 done 532 } 533 534 ###5.1/5.2/5.3/5.4/5.5 Disable Lenovo, Inspur, HP, Huawei, Dell server user### 535 disable_user_common(){ 536 check_login_username_passwd $2 $3 $1 537 if [ $? -ne 0 ];then 538 aciton_false "$1 Incorrect Login account." 539 else 540 check_user_exist $2 $3 $1 $4 541 if [ $? -ne 0 ];then 542 aciton_false "$1 User:$4 Not Exists." 543 else 544 USER_ID=`ipmitool -I lanplus -U $2 -P $3 -H $1 user list | grep -w "${DISABLE_USERNAME}" | awk '{print$1}'` 545 ipmitool -I lanplus -U $2 -P $3 -H $1 user disable $USER_ID &>/dev/null 546 if [ $? -eq 0 ];then 547 if [ ${CHOICE_STEP5} -eq 55 ];then 548 SN=`sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet ${2}@$1 racadm get BIOS.SysInformation.SystemServiceTag | tail -n1 | awk -F "=" '{print$2}'` 549 else 550 SN=`ipmitool -I lanplus -U $2 -P $3 -H $1 fru list | grep -w "Product Serial" | awk -F ":" '{print$2}' | head -n1 | sed 's#[[:space:]]##g'` 551 fi 552 action_true "$1 SN:$SN User:$4" 553 else 554 aciton_false "$1 Unknown Error." 555 fi 556 fi 557 fi 558 } 559 560 ###5. Disable server remote management card user common function ### 561 disable_batch_task(){ 562 while true;do 563 read -p "[Please enter the path to the IP list File]:" FILE_PATH 564 if [ ! -e ${FILE_PATH} ] || [ -z ${FILE_PATH} ];then 565 colour default_red 566 echo "Error: ${FILE_PATH}: No such file or directory." 567 colour default 568 continue 569 fi 570 sed '1d' ${FILE_PATH} >${WORKDIR}/temp/list.temp 2>/dev/null 571 read -p "Are You Sure?(y/n):" ANSWER 572 case ${ANSWER} in 573 y|Y|yes|YES) echo "Disabling user.This may take serveral minutes,Please wait......";; 574 *) continue;; 575 esac 576 while read line;do 577 IP=`echo $line | awk '{print $1}'` 578 chenck_ip_port ${IP} 579 if [ $? -ne 0 ];then 580 continue 581 fi 582 LOGIN_USERNAME=`echo $line | awk '{print $2}'` 583 LOGIN_PASSWORD=`echo $line | awk '{print $3}'` 584 DISABLE_USERNAME=`echo $line | awk '{print $4}'` 585 if [ ${CHOICE_STEP5} -eq 55 ];then 586 check_dell_ipmi $IP $LOGIN_USERNAME $LOGIN_PASSWORD 587 fi 588 disable_user_common $IP $LOGIN_USERNAME $LOGIN_PASSWORD $DISABLE_USERNAME 589 done < ${WORKDIR}/temp/list.temp 590 break 591 done 592 } 593 594 ###6.1/6.2/6.3/6.4/6.5 enable Lenovo, Inspur, HP, Huawei, Dell server user### 595 enable_user_common(){ 596 check_login_username_passwd $2 $3 $1 597 if [ $? -ne 0 ];then 598 aciton_false "$1 Incorrect Login account." 599 else 600 check_user_exist $2 $3 $1 $4 601 if [ $? -ne 0 ];then 602 aciton_false "$1 User:$4 Not Exists." 603 else 604 USER_ID=`ipmitool -I lanplus -U $2 -P $3 -H $1 user list | grep -w "${DISABLE_USERNAME}" | awk '{print$1}'` 605 ipmitool -I lanplus -U $2 -P $3 -H $1 user enable $USER_ID &>/dev/null 606 if [ $? -eq 0 ];then 607 if [ ${CHOICE_STEP6} -eq 65 ];then 608 sshpass -p $3 ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $2@$1 racadm get BIOS.SysInformation.SystemServiceTag | tail -n1 > ${WORKDIR}/temp/dell_sn.temp 609 SN=`awk -F "=" '{print$2}' ${WORKDIR}/temp/dell_sn.temp` 610 else 611 SN=`ipmitool -I lanplus -U $2 -P $3 -H $1 fru list | grep -w "Product Serial" | awk -F ":" '{print$2}' | head -n1 | sed 's#[[:space:]]##g'` 612 fi 613 action_true "$1 SN:$SN User:$4" 614 else 615 aciton_false "$1 Unknown Error." 616 fi 617 fi 618 fi 619 } 620 621 ###6. enable server remote management card user common function ### 622 enable_batch_task(){ 623 while true;do 624 read -p "[Please enter the path to the IP list File]:" FILE_PATH 625 if [ ! -e ${FILE_PATH} ] || [ -z ${FILE_PATH} ];then 626 colour default_red 627 echo "Error: ${FILE_PATH}: No such file or directory." 628 colour default 629 continue 630 fi 631 sed '1d' ${FILE_PATH} > ${WORKDIR}/temp/list.temp 2>/dev/null 632 read -p "Are You Sure?(y/n):" ANSWER 633 case ${ANSWER} in 634 y|Y|yes|YES) echo "Enabling user.This may take serveral minutes,Please wait......";; 635 *) continue;; 636 esac 637 while read line;do 638 IP=`echo $line | awk '{print $1}'` 639 chenck_ip_port ${IP} 640 if [ $? -ne 0 ];then 641 continue 642 fi 643 LOGIN_USERNAME=`echo $line | awk '{print $2}'` 644 LOGIN_PASSWORD=`echo $line | awk '{print $3}'` 645 DISABLE_USERNAME=`echo $line | awk '{print $4}'` 646 if [ ${CHOICE_STEP6} -eq 65 ];then 647 check_dell_ipmi $IP $LOGIN_USERNAME $LOGIN_PASSWORD 648 fi 649 enable_user_common $IP $LOGIN_USERNAME $LOGIN_PASSWORD $DISABLE_USERNAME 650 done < ${WORKDIR}/temp/list.temp 651 break 652 done 653 } 654 655 ###1.query user action### 656 step1_1(){ 657 enter_account_ip 658 query_user_list 659 } 660 661 step1_5(){ 662 enter_account_ip 663 check_dell_ipmi $IP $USERNAME $PASSWORD 664 query_user_list 665 } 666 ###2.add user action### 667 step2_1(){ 668 add_batch_task 669 echo -e "\nIt's Completed." 670 rm -f ${WORKDIR}/temp/*.temp 671 } 672 673 ###3.update user action### 674 step3_1(){ 675 update_batch_task 676 echo -e "\nIt's Completed." 677 rm -f ${WORKDIR}/temp/*.temp 678 } 679 680 ###4.delete user action### 681 step4_1(){ 682 delete_batch_task 683 echo -e "\nIt's Completed." 684 rm -f ${WORKDIR}/temp/*.temp 685 } 686 687 ###5.disable user action### 688 step5_1(){ 689 disable_batch_task 690 echo -e "\nIt's Completed." 691 rm -f ${WORKDIR}/temp/*.temp 692 } 693 694 ###6.enable user action### 695 step6_1(){ 696 enable_batch_task 697 echo -e "\nIt's Completed." 698 rm -f ${WORKDIR}/temp/*.temp 699 } 700 701 ###Two level menu1### 702 menu_level2(){ 703 cat << EOF 704 -------------------------------------------------------------- 705 [${CHOICE_STEP0}1] ## For Lenovo SystemX 706 [${CHOICE_STEP0}2] ## For Inspur YingXin 707 [${CHOICE_STEP0}3] ## For HP ProLiant 708 [${CHOICE_STEP0}4] ## For Huawei RH 709 [${CHOICE_STEP0}5] ## For DELL PowerEdge 710 [01] ## Return To Previous Menu... 711 [00] ## Exit 712 -------------------------------------------------------------- 713 EOF 714 } 715 716 ###Two level menu2### 717 menu_level2_1(){ 718 cat << EOF 719 -------------------------------------------------------------- 720 [${CHOICE_STEP0}1] ## For Lenovo SystemX 721 [${CHOICE_STEP0}2] ## For Inspur YingXin 722 [${CHOICE_STEP0}3] ## For HP ProLiant 723 [${CHOICE_STEP0}4] ## For Huawei RH 724 [01] ## Return To Previous Menu... 725 [00] ## Exit 726 -------------------------------------------------------------- 727 EOF 728 } 729 730 ###query user### 731 step1(){ 732 while true;do 733 menu_level2 734 read -p "Input Your Choice:" CHOICE 735 read -p "Are You Sure?(y/n):" ANSWER 736 case ${ANSWER} in 737 y|Y|yes|YES) echo "Your Choice Is:[${CHOICE}]";sleep 1;; 738 *) CHOICE="other";; 739 esac 740 case ${CHOICE} in 741 11) step1_1;exit 0;; 742 12) step1_1;exit 0;; 743 13) step1_1;exit 0;; 744 14) step1_1;exit 0;; 745 15) step1_5;exit 0;; 746 01) clear;menu_level1_action;exit 0;; 747 00) echo "You've quit the program.";exit 0;; 748 *) clear;; 749 esac 750 done 751 } 752 753 ###add user### 754 step2(){ 755 while true;do 756 menu_level2 757 read -p "Input Your Choice:" CHOICE_STEP2 758 read -p "Are You Sure?(y/n):" ANSWER 759 case ${ANSWER} in 760 y|Y|yes|YES) echo "Your Choice Is:[${CHOICE_STEP2}]";sleep 1;; 761 *) CHOICE_STEP2="other";; 762 esac 763 case ${CHOICE_STEP2} in 764 21) step2_1;exit 0;; 765 22) step2_1;exit 0;; 766 23) step2_1;exit 0;; 767 24) step2_1;exit 0;; 768 25) step2_1;exit 0;; 769 01) clear;menu_level1_action;exit 0;; 770 00) echo "You've quit the program.";exit 0;; 771 *) clear;; 772 esac 773 done 774 } 775 776 ###update user### 777 step3(){ 778 while true;do 779 menu_level2 780 read -p "Input Your Choice:" CHOICE_STEP3 781 read -p "Are You Sure?(y/n):" ANSWER 782 case ${ANSWER} in 783 y|Y|yes|YES) echo "Your Choice Is:[${CHOICE_STEP3}]";sleep 1;; 784 *) CHOICE_STEP3="other";; 785 esac 786 case ${CHOICE_STEP3} in 787 31) step3_1;exit 0;; 788 32) step3_1;exit 0;; 789 33) step3_1;exit 0;; 790 34) step3_1;exit 0;; 791 35) step3_1;exit 0;; 792 01) clear;menu_level1_action;exit 0;; 793 00) exit 0;; 794 *) clear;; 795 esac 796 done 797 } 798 799 ###delete user### 800 step4(){ 801 while true;do 802 menu_level2_1 803 read -p "Input Your Choice:" CHOICE_STEP4 804 read -p "Are You Sure?(y/n):" ANSWER 805 case ${ANSWER} in 806 y|Y|yes|YES) echo "Your Choice Is:[${CHOICE_STEP4}]";sleep 1;; 807 *) CHOICE_STEP4="other";; 808 esac 809 case ${CHOICE_STEP4} in 810 41) step4_1;exit 0;; 811 42) step4_1;exit 0;; 812 43) step4_1;exit 0;; 813 44) step4_1;exit 0;; 814 01) clear;menu_level1_action;exit 0;; 815 00) exit 0;; 816 *) clear;; 817 esac 818 done 819 } 820 821 ###disable user### 822 step5(){ 823 while true;do 824 menu_level2 825 read -p "Input Your Choice:" CHOICE_STEP5 826 read -p "Are You Sure?(y/n):" ANSWER 827 case ${ANSWER} in 828 y|Y|yes|YES) echo "Your Choice Is:[${CHOICE_STEP5}]";sleep 1;; 829 *) CHOICE_STEP5="other";; 830 esac 831 case ${CHOICE_STEP5} in 832 51) step5_1;exit 0;; 833 52) step5_1;exit 0;; 834 53) step5_1;exit 0;; 835 54) step5_1;exit 0;; 836 55) step5_1;exit 0;; 837 01) clear;menu_level1_action;exit 0;; 838 00) exit 0;; 839 *) clear;; 840 esac 841 done 842 } 843 844 ###enable user### 845 step6(){ 846 while true;do 847 menu_level2 848 read -p "Input Your Choice:" CHOICE_STEP6 849 read -p "Are You Sure?(y/n):" ANSWER 850 case ${ANSWER} in 851 y|Y|yes|YES) echo "Your Choice Is:[${CHOICE_STEP6}]";sleep 1;; 852 *) CHOICE_STEP6="other";; 853 esac 854 case ${CHOICE_STEP6} in 855 61) step6_1;exit 0;; 856 62) step6_1;exit 0;; 857 63) step6_1;exit 0;; 858 64) step6_1;exit 0;; 859 65) step6_1;exit 0;; 860 01) clear;menu_level1_action;exit 0;; 861 00) exit 0;; 862 *) clear;; 863 esac 864 done 865 } 866 867 ###First level menu### 868 menu_level1(){ 869 cat << EOF 870 ############################################################## 871 # BMC Accounts Batch MGT Tool V1.0.0 # 872 # Release Date:2017-11-1     # 873 # NOTE: This script is user to manage the PC server remote # 874 # management cart account.for Lenovo SystemX,Inspur Yingxin, # 875 # HP ProLiant,Huawei RH,DELL PowerEdage.     # 876 # --XX.Tech ITDC itop_Mon # 877 ############################################################## 878 -------------------------------------------------------------- 879 [1] ## Query user 880 [2] ## Add user 881 [3] ## Update user 882 [4] ## Remove user 883 [5] ## Disable user 884 [6] ## Enable user 885 [0] ## Exit 886 -------------------------------------------------------------- 887 EOF 888 } 889 890 ###First level menu action### 891 menu_level1_action(){ 892 while true;do 893 clear;menu_level1 894 read -p "Input Your Choice(m for help):" CHOICE_STEP0 895 read -p "Are You Sure?(y/n):" ANSWER 896 case ${ANSWER} in 897 y|Y|yes|YES) echo "Your Choice Is:[${CHOICE_STEP0}]";sleep 1;; 898 *) CHOICE_STEP0="other";; 899 esac 900 case ${CHOICE_STEP0} in 901 1) step1;exit 0;; 902 2) step2;exit 0;; 903 3) step3;exit 0;; 904 4) step4;exit 0;; 905 5) step5;exit 0;; 906 6) step6;exit 0;; 907 m|M) useage;; 908 0) exit 0;; 909 *) clear;; 910 esac 911 done 912 } 913 914 ###User permission check### 915 if [ `id -u` -ne 0 ];then 916 action "FAILED: Please Login As Root To Run This Script!" /bin/false 917 exit 110 918 fi 919 920 ###Operating system version check### 921 if [ `cat /etc/redhat-release | egrep -c "6.4|6.5|6.6|6.8"` -eq 0 ]; then 922 action "FAILED: Please Run This Script On RHEL6.8!" /bin/false 923 exit 110 924 fi 925 926 ###sshpass packet inspection### 927 SSHPASS=`rpm -qa | grep -wc sshpass` 928 if [ ${SSHPASS} -eq 0 ];then 929 action "Please install the sshpass package." /bin/false 930 exit 110 931 fi 932 933 ###expect packet inspection### 934 rpm -qa | grep -qw expect 935 if [ $? -ne 0 ];then 936 action "Please install the expect package." /bin/false 937 exit 110 938 fi 939 940 ###ipmitool check### 941 IPMITOOL_VERSION=`rpm -qa | grep -w ipmitool | awk -F "-" '{print$2}'` 942 if [ -z ${IPMITOOL_VERSION} ];then 943 action "There is no IPMITOOL installed in the local system." /bin/false 944 exit 110 945 elif [ "${IPMITOOL_VERSION}" != "1.8.15" ];then 946 action "Please update the IPMITOOL version to 1.8.15" /bin/false 947 exit 110 948 fi 949 950 ###Program entrance### 951 menu_level1_action

 

转载于:https://www.cnblogs.com/changmingzhao/p/7826333.html

相关资源:各显卡算力对照表!
最新回复(0)