1.IPMI工作原理图:

2.脚本实现流程图:

3.适配服务器机型:

4.演示效果:

5.实现代码:

  1. #!/usr/bin/env bash
  2.  
  3. # Author : JACK ZHAO
  4. # Date : //
  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=
  10. IPMI_PORT=
  11. instool=${WORKDIR}/instool3.-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 $ 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 - `;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
  44. done
  45. }
  46.  
  47. ###Check the IP format###
  48. check_ip_format(){
  49. ipcalc -cs $
  50. if [ $? -eq ];then
  51. return
  52. else
  53. return $RC
  54. fi
  55. }
  56.  
  57. ###Check whether IP is online###
  58. check_ip_alive(){
  59. ping -c -w $ &>/dev/null
  60. if [ $? -eq ];then
  61. return
  62. else
  63. return $RC
  64. fi
  65. }
  66.  
  67. ###Check Port###
  68. check_port(){
  69. nc -vuz $ $ &>/dev/null
  70. if [ $? -eq ];then
  71. return
  72. else
  73. return
  74. fi
  75. }
  76.  
  77. ###Summary of inspection conditions###
  78. chenck_ip_port(){
  79. check_ip_format $
  80. if [ $? -ne ];then
  81. aciton_false "$1 Invalid IP."
  82. return
  83. fi
  84. check_ip_alive $
  85. if [ $? -ne ];then
  86. aciton_false "$1 Network connection failed."
  87. return
  88. fi
  89. check_port $IP $IPMI_PORT
  90. if [ $? -ne ];then
  91. aciton_false "$1 UDP 623 Port connection failed."
  92. return
  93. fi
  94. return
  95. }
  96.  
  97. ###Public function ###
  98. enter_account_ip(){
  99. while true;do
  100. read -p "Input Your IP:" IP
  101. chenck_ip_port ${IP}
  102. [ $? -ne ] && 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 $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet ${}@$ racadm get iDRAC.IPMILan.Enable | tail -n1 | awk -F "=" '{print$2}'`
  123. if [ "$DELL_IPMI_STATUS" == "Disabled" ];then
  124. sshpass -p $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet ${}@$ racadm set iDRAC.IPMILan.Enable Enabled
  125. if [ $? -ne ];then
  126. aciton_false "$1 Open the IPMI server failure."
  127. exit
  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 ];then
  137. colour default_red
  138. echo "ERROR: Incorrect username or password,Please try again!"
  139. colour default
  140. fi
  141. return
  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. |
  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 $ -P $ -H $ user list | grep -wv "ID" | awk '{print$1"\t"$2}' | egrep -v "true|false" > ${WORKDIR}/temp/user_info.temp > /dev/null
  180. if `grep -wq "$4" ${WORKDIR}/temp/user_info.temp`;then
  181. return
  182. else
  183. return
  184. fi
  185. }
  186.  
  187. ###Check the username and password###
  188. check_login_username_passwd(){
  189. ipmitool -I lanplus -U $ -P $ -H $ user list &> /dev/null
  190. if [ $? -eq ];then
  191. return
  192. else
  193. return
  194. fi
  195. }
  196.  
  197. ###2.3 Adding HP server ILO users###
  198. add_user_hp(){
  199. check_login_username_passwd $ $ $
  200. if [ $? -ne ];then
  201. aciton_false "$1 Incorrect Login account."
  202. else
  203. check_user_exist $ $ $ $
  204. if [ $? -eq ];then
  205. aciton_false "$1 User:$4 Already Exist."
  206. else
  207. sshpass -p $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $@$ create /map1/accounts1 username=$ password=$ name=$ group=admin,config,oemhp_vm,oemhp_rc,oemhp_power &> /dev/null
  208. if [ $? -eq ];then
  209. SN=`ipmitool -I lanplus -U $ -P $ -H $ 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 $ $ $
  221. if [ $? -ne ];then
  222. aciton_false "$1 Incorrect Login account."
  223. else
  224. check_user_exist $ $ $ $
  225. if [ $? -eq ];then
  226. aciton_false "$1 User:$4 Already Exist."
  227. else
  228. ${instool} $ $ $ -lan -user -list | awk '{print$1}' | grep -v "ID" > ${WORKDIR}/temp/user_id_inspur.temp > /dev/null
  229. for ((i=;i++;i<=));do
  230. grep -qw $i ${WORKDIR}/temp/user_id_inspur.temp && continue
  231. ${instool} $ $ $ -lan -user -addm $i $ $ admin &> /dev/null
  232. break
  233. done
  234. ${instool} $ $ $ -lan -user -list &>/dev/null
  235. if [ $? -eq ];then
  236. SN=`ipmitool -I lanplus -U $ -P $ -H $ 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 $ $ $
  248. if [ $? -ne ];then
  249. aciton_false "$1 Incorrect Login account."
  250. else
  251. check_user_exist $ $ $ $
  252. if [ $? -eq ];then
  253. aciton_false "$1 User:$4 Already Exist."
  254. else
  255. USERID=`awk '{print$1}' ${WORKDIR}/temp/user_info.temp`
  256. for ((i=;i++;i<=));do
  257. echo $USERID | grep -qw $i && continue
  258. ipmitool -I lanplus -U $ -P $ -H $ user set name $i $ &>/dev/null
  259. ipmitool -I lanplus -U $ -P $ -H $ user set password $i $ &>/dev/null
  260. ipmitool -I lanplus -U $ -P $ -H $ user priv $i &>/dev/null
  261. ipmitool -I lanplus -U $ -P $ -H $ channel setaccess $i callin=on ipmi=on link=on privilege=
  262. ipmitool -I lanplus -U $ -P $ -H $ user enable $i &>/dev/null
  263. if [ ${CHOICE_STEP2} -eq ];then
  264. sshpass -p $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $@$ racadm set idrac.users.${i}.Privilege 0x1ff &>/dev/null
  265. fi
  266. ipmitool -I lanplus -U $ -P $ -H $ user test $i $ &>/dev/null
  267. STATUS_RC=$?
  268. break
  269. done
  270. if [ $STATUS_RC -eq ];then
  271. if [ ${CHOICE_STEP2} -eq ];then
  272. sshpass -p $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $@$ 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 $ -P $ -H $ 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. ###.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 >/dev/null
  301. while read line;do
  302. IP=`echo $line | awk '{print $1}'`
  303. chenck_ip_port ${IP}
  304. if [ $? -ne ];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 ];then
  312. add_user_common $IP $LOGIN_USERNAME $LOGIN_PASSWORD $NEW_USERNAME $NEW_PASSWORD
  313. elif [ ${CHOICE_STEP2} -eq ];then
  314. add_user_inspur $IP $LOGIN_USERNAME $LOGIN_PASSWORD $NEW_USERNAME $NEW_PASSWORD
  315. elif [ ${CHOICE_STEP2} -eq ];then
  316. add_user_hp $IP $LOGIN_USERNAME $LOGIN_PASSWORD $NEW_USERNAME $NEW_PASSWORD
  317. elif [ ${CHOICE_STEP2} -eq ];then
  318. add_user_common $IP $LOGIN_USERNAME $LOGIN_PASSWORD $NEW_USERNAME $NEW_PASSWORD
  319. elif [ ${CHOICE_STEP2} -eq ];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/.5Modify Lenovo, Inspur, HP, Huawei, Dell server user password###
  329. update_user_common(){
  330. check_login_username_passwd $ $ $
  331. if [ $? -ne ];then
  332. aciton_false "$1 Incorrect Login account."
  333. else
  334. check_user_exist $ $ $ $
  335. if [ $? -ne ];then
  336. aciton_false "$1 User:$4 Not Exists."
  337. else
  338. USER_ID=`ipmitool -I lanplus -U $ -P $ -H $ user list | grep -w "$UPDATE_USERNAME" | awk '{print$1}'`
  339. ipmitool -I lanplus -U $ -P $ -H $ user set password $USER_ID $ &>/dev/null
  340. ipmitool -I lanplus -U $ -P $ -H $ user test $USER_ID $ &>/dev/null
  341. if [ $? -eq ];then
  342. if [ ${CHOICE_STEP3} -eq ];then
  343. sshpass -p $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $@$ 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 $ -P $ -H $ 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. ###.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 >/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 ];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 ];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 $ $SSH_PORT
  394. if [ $? -ne ];then
  395. aciton_false "$1 TCP 22 Port connection failed."
  396. else
  397. check_login_username_passwd $ $ $
  398. if [ $? -ne ];then
  399. aciton_false "$1 Incorrect Login account."
  400. else
  401. check_user_exist $ $ $ $
  402. if [ $? -ne ];then
  403. aciton_false "$1 User:$4 Not Exists."
  404. else
  405. USER_ID_LENOVO=`sshpass -p $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $@$ -n "users" | grep -w "$4" | awk '{print$1}'`
  406. sshpass -p $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $@$ users -${USER_ID_LENOVO} -clear &> /dev/null
  407. if [ $? -eq ];then
  408. SN=`ipmitool -I lanplus -U $ -P $ -H $ 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 $ $ $
  421. if [ $? -ne ];then
  422. aciton_false "$1 Incorrect Login account."
  423. else
  424. check_user_exist $ $ $ $
  425. if [ $? -ne ];then
  426. aciton_false "$1 User:$4 Not Exists."
  427. else
  428. USER_ID=`${instool} $ $ $ -lan -user -list |grep -w "$4" | awk '{print$1}'`
  429. ${instool} $ $ $ -lan -user -del ${USER_ID}
  430. if [ $? -eq ];then
  431. SN=`ipmitool -I lanplus -U $ -P $ -H $ 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 $ $ $
  443. if [ $? -ne ];then
  444. aciton_false "$1 Incorrect Login account."
  445. else
  446. check_user_exist $ $ $ $
  447. if [ $? -ne ];then
  448. aciton_false "$1 User:$4 Not Exists."
  449. else
  450. sshpass -p $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $@$ delete /map1/accounts1/$ &> /dev/null
  451. check_user_exist $ $ $ $
  452. if [ $? -ne ];then
  453. SN=`ipmitool -I lanplus -U $ -P $ -H $ 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 $ $ $
  465. if [ $? -ne ];then
  466. aciton_false "$1 Incorrect Login account."
  467. else
  468. check_user_exist $ $ $ $
  469. if [ $? -ne ];then
  470. aciton_false "$1 User:$4 Not Exists."
  471. else
  472. /usr/bin/expect << EOF
  473. set timeout
  474. log_user
  475. spawn sshpass -p $ ssh -o StrictHostKeyChecking=no -o LogLevel=quiet ${}@$
  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 $ $ $ $
  485. if [ $? -ne ];then
  486. SN=`ipmitool -I lanplus -U $ -P $ -H $ 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. ###.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 >/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 ];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 ];then
  521. delete_user_lenovo $IP $LOGIN_USERNAME $LOGIN_PASSWORD $DELETE_USERNAME
  522. elif [ ${CHOICE_STEP4} -eq ];then
  523. delete_user_inspur $IP $LOGIN_USERNAME $LOGIN_PASSWORD $DELETE_USERNAME
  524. elif [ ${CHOICE_STEP4} -eq ];then
  525. delete_user_hp $IP $LOGIN_USERNAME $LOGIN_PASSWORD $DELETE_USERNAME
  526. elif [ ${CHOICE_STEP4} -eq ];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 $ $ $
  537. if [ $? -ne ];then
  538. aciton_false "$1 Incorrect Login account."
  539. else
  540. check_user_exist $ $ $ $
  541. if [ $? -ne ];then
  542. aciton_false "$1 User:$4 Not Exists."
  543. else
  544. USER_ID=`ipmitool -I lanplus -U $ -P $ -H $ user list | grep -w "${DISABLE_USERNAME}" | awk '{print$1}'`
  545. ipmitool -I lanplus -U $ -P $ -H $ user disable $USER_ID &>/dev/null
  546. if [ $? -eq ];then
  547. if [ ${CHOICE_STEP5} -eq ];then
  548. SN=`sshpass -p $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet ${}@$ racadm get BIOS.SysInformation.SystemServiceTag | tail -n1 | awk -F "=" '{print$2}'`
  549. else
  550. SN=`ipmitool -I lanplus -U $ -P $ -H $ 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. ###. 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 >/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 ];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 ];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 $ $ $
  597. if [ $? -ne ];then
  598. aciton_false "$1 Incorrect Login account."
  599. else
  600. check_user_exist $ $ $ $
  601. if [ $? -ne ];then
  602. aciton_false "$1 User:$4 Not Exists."
  603. else
  604. USER_ID=`ipmitool -I lanplus -U $ -P $ -H $ user list | grep -w "${DISABLE_USERNAME}" | awk '{print$1}'`
  605. ipmitool -I lanplus -U $ -P $ -H $ user enable $USER_ID &>/dev/null
  606. if [ $? -eq ];then
  607. if [ ${CHOICE_STEP6} -eq ];then
  608. sshpass -p $ ssh -o StrictHostKeyChecking=no -n -o LogLevel=quiet $@$ 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 $ -P $ -H $ 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. ###. 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 >/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 ];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 ];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. ###.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. ###.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

PC Server远程管理卡用户管理脚本实现的更多相关文章

  1. 使用mysqlbinlog server远程备份binlog的脚本

    #注意,备份机到远程mysql服务器需要免密钥登录,此脚本放到计划任务中每五分钟执行一次,避免mysqlbinlog server进程长时间挂掉无人知晓   cat backup_binlog.sh ...

  2. 【shell 练习4】编写Shell用户管理脚本(二)

    一.创建.删除.查看用户,随机生成八位数密码 #!/bin/bash #Author:yanglt #!/bin/bash #Author:yanglt #Blog:https://www.cnblo ...

  3. 【shell 练习3】用户管理脚本(一)

    一.创建十个用户,密码为八位 [root@localhost ~]# cat UserManger02.sh #!/bin/bash . /etc/init.d/functions [ $UID -n ...

  4. 管理Windows Server 2008本地用户和组

    下面介绍Windows Server 2008本地用户和组的管理包括创建用户.删除用户.重设密码.将用户添加到组.普通用户跟管理员的区别 .用户配置文件包括桌面上文件,桌面背景,桌面上图标,IE设置, ...

  5. linux高频操作: host,用户管理,免密登陆,管道,文件权限,脚本,防火墙,查找

    1. 修改hosts和hostname 2. 用户管理 3. 免秘登陆 4. 文件末尾添加 >> 5. 设置可执行文件 6. 任何地方调用 7. Centos6 永久关闭防火墙 8. Ce ...

  6. 本地sh脚本创建以及利用ssh server远程运行sh脚本

    想要同时运行多个非本地的sh脚本,用来实现运行同一网段下多机程序的集成,可以通过在每台机器上写sh脚本,再在本机上运行一个启动远程机器sh的脚本 首先需要在所有机器上安装openssh-server ...

  7. shell脚本之用户管理

    #!/usr/bin/env bash ############################### # 脚本名称 : userManager.sh # # 脚本功能 : 账号管理 # # 脚本参数 ...

  8. Windows Server 2008 R2入门之用户管理

    一.用户账户概述: ”用户”是计算机的使用者在计算机系统中的身份映射,不同的用户身份拥有不同的权限,每个用户包含一个名称和一个密码: 在Windows中,每个用户帐户有一个唯一的安全标识符(Secur ...

  9. Linux实战教学笔记14:用户管理初级(下)

    第十四节 用户管理初级(下) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,用户查询相关命令id,finger,users,w,who,last,lastlog,gr ...

随机推荐

  1. IOS 制作常用代码的快捷方式

    输入可以变的对象或类型,要用<#name#>

  2. Jenkins配置(Jenkins如何与maven项目进行连用)

    一同事,在问关于Jenkins搭建后,他是如何与我们项目进行连用的,如何通过Jenkins去编译我们的项目的,现在介绍下如何通过Jenkins持续编译我们的项目 配置过程 1.确定我们已经搭建好了Je ...

  3. HDU(4394),数论上的BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4394 思路很巧妙,要找到m,可以这样思考,n的个位是有m的个位决定的,从0-9搜一遍,满足情况的话就继 ...

  4. 2017.11.18 C语言的算法分析题目

    算法分析 1. 选定实验题目,仔细阅读实验要求,设计好输入输出,按照分治法的思想构思算法,选取合适的存储结构实现应用的操作. 2. 设计的结果应在Visual C++ 实验环境下实现并进行调试.(也可 ...

  5. theano提示:g++ not detected的解决办法

    导入theano包后出现如下警告: WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execu ...

  6. 第42章 电源管理—实现低功耗—零死角玩转STM32-F429系列

    第42章     电源管理—实现低功耗 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...

  7. Spring Bean依赖但注入(autowired或resource)时NullPointerException(xml和annotation混用的场景下)

    项目中同时使用了xml和annotation的方式管理Spring Bean 启动时候报NullPointerException,依赖注入失败! 参考: http://fly0wing.iteye.c ...

  8. python显示灰度图

    import matplotlib import matplotlib.pyplot as plt %matplotlib inline im=plt.imread('../lena.jpg', py ...

  9. 7-4 python 接口开发(提供mock服务)

    1.登录接口开发(数据存在数据库中)  接口开发做mock(模拟功能) tools.py import pymysql def my_db(sql): conn = pymysql.connect(h ...

  10. LNMP+HAProxy+Keepalived负载均衡 - LNMP基础环境准备

    环境版本说明: 服务器系统:CentOS 7.5: ``` cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) # 输出结果 `` ...