ipmitool的shell小工具

简介

ipmitool 是一种可用在 linux 系统下的命令行方式的 ipmi 平台管理工具,通过它可以实现获取传感器的信息、显示系统日志内容、网络远程开关机等功能。

应用场景

批量远程控制服务器开机关机,下次启动pxe,配置bmc账号密码等等

脚本内容

[root@test01 ~]# cat ipmi_tool.sh 
#!/bin/sh
#description: ipmitool command tool
#author: liuxin
#date: 20180117

function usage()
{
  cat << EOF
ipmi_tool.sh: ipmitool command tool
Usage: ipmi_tool.sh [options...] <command>
  -f                  The file of remote host name for LAN interface
  -u                  Remote session username, default value in this shell
  -p                  Remote session password, default value in this shell

Help options:
  -h                  Show this help message
EOF
  return 0
}

function get_params()
{

  [[ $# -lt 1 ]] && usage && exit 0
  while getopts :hu:p:f: opt
  do
    case "$opt" in
    h) usage && exit 0 ;;
    u) USERNAME="$OPTARG" ;;
    p) PASSWORD="$OPTARG" ;;
    f|-file) HOST_FILE="$OPTARG" ;;
    *) echo "Unknown option: $opt" && exit 1 ;;
    esac
  done
  shift $[ $OPTIND - 1 ]

  COMMAND="$@"
  if [ -z "$COMMAND" ]
  then
    echo "command is null" && exit 1
  fi

  return 0
}

function print_params()
{
  echo "USERNAME: $USERNAME"
  echo "PASSWORD: $PASSWORD"
  echo "HOST_FILE: $HOST_FILE"
  echo "COMMAND: $COMMAND"
  return 0
}

function ipmitool_run()
{
  cat $HOST_FILE |grep -v '^#' |while read ip
  do
    received=$(ping $ip -c 5 -i 0.01 |egrep -o '[0-9] received' |awk '{print $1}')
    echo -en "$ip\t$COMMAND:\t"
    if [ $received -gt 0 ]
    then
      echo "ipmitool -I lanplus -U $USERNAME -P $PASSWORD -H $ip $COMMAND"
      #ipmitool -I lanplus -U $USERNAME -P $PASSWORD -H $ip $COMMAND
    else
      echo "Unreachable"
    fi
  done
  return 0
}

function main
{
  USERNAME="root"
  PASSWORD="123456"
  HOST_FILE=""
  COMMAND=""
  
  get_params $@
  ipmitool_run
  #print_params
  return 0  
}

#--------------------
main $@

[root@test01 ~]# cat ip.txt 
192.168.1.1
192.168.1.2
192.168.1.60

[root@test01 ~]# ./ipmi_tool.sh -f ./ip.txt chassis power reset
192.168.1.1 chassis power reset:    ipmitool -I lanplus -U root -P 123456 -H 192.168.1.1 chassis power reset
192.168.1.2 chassis power reset:    Unreachable
192.168.1.60    chassis power reset:    ipmitool -I lanplus -U root -P 123456 -H 192.168.1.60 chassis power reset

[root@test01 ~]# ./ipmi_tool.sh -u admin -p pass -f ./ip.txt chassis power reset
192.168.1.1 chassis power reset:    ipmitool -I lanplus -U admin -P pass -H 192.168.1.1     chassis power reset
192.168.1.2 chassis power reset:    Unreachable
192.168.1.60    chassis power reset:    ipmitool -I lanplus -U admin -P pass -H 192.168.1.60   chassis power reset
    原文作者:小小运维
    原文地址: https://www.jianshu.com/p/42252941f40c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞