shell实现hp刀片ilo地址配置

一、问题背景

HP刀片在使用KVM远程console管理过程中,要求刀片配置的ILO地址和HP OA地址在同一网段。由于IP地址资源紧张,刀筐OA口默认配置上了地址,刀片没有配置,都是使用的时候临时配置。所以一旦物理机出现问题,想要通过远程管理口查看主机所处的状态就比较麻烦。先要通过ping工具查询预留地址是否可用。再在OA界面上进行配置,比较影响效率。

二、脚本实现

该脚本写的比较凑合,杂糅了shell、C语言、expect 三者实现的,具体代码如下:

#!/bin/bash
#code from www.361way.com
#auto set the Blade ilo address
usage( )
{
  echo "usage: 0 oaip username  password"
}
if [# -lt 3 ]; then
   usage
   exit 1
fi
oaip=1
username=2
password=3
oaip=`sshpass -ppassword  ssh -o StrictHostKeyChecking=no     username@oaip "SHOW OA NETWORK" |grep 'IPv4 Address:'|awk '{print 3}'`
oamask=`sshpass -ppassword  ssh -o StrictHostKeyChecking=no   username@oaip "SHOW OA NETWORK" |grep 'Netmask:'|awk '{print 2}'`
oagate=`sshpass -ppassword  ssh -o StrictHostKeyChecking=no   username@oaip "SHOW OA NETWORK" |grep 'Gateway Address:'|awk '{print 3}'`
clear
echo -e 'nn'
echo '****************************'
echo  'The Blade box information:'
echo '****************************'
echo  IP ADDR:oaip
echo  NETMASK:  oamask
echo  GATEway:oagate
mask2cdr ( )
{
   # Assumes there's no "255." after a non-255 byte in the mask
   local x={1##*255.}
   set -- 0^^^128^192^224^240^248^252^254^(( ({#1} -{#x})*2 )) {x%%.*}
   x={1%%3*}
   echo(( 2 + ({#x}/4) ))
}
cdr2mask ( )
{
   # Number of args to shift, 255..255, first non-255 byte, zeroes
   set -- (( 5 - (1 / 8) )) 255 255 255 255 (( (255 << (8 - (1 % 8))) & 255 )) 0 0 0
   [ 1 -gt 1 ] && shift1 || shift
   echo {1-0}.{2-0}.{3-0}.{4-0}
}
addcdr=oaip/`mask2cdroamask`
echo -e 'nn'
echo '******************************'
echo   We will scan the network
echo   And get can use ip now
echo   Please wait a moment!!!
echo '******************************'
fping -g (echo `./net2ipaddcdr`)|grep 'unreachable'
read -p "Enter can used ip for blade: " iloip
read -p "Enter the blade bay id: " bid
setip="set ebipa server iloipoamask  bid"
setgate="set ebipa server gatewayoagate  bid"
cat>autoexec.exp<"
send "set ebipa serveriloip oamaskbidn"
expect "*bays?"
send "YESn"
expect "*>"
send "set ebipa server gateway oagatebidn"
sleep 2
expect "*?"
send "YESn"
expect eof
#interact
EOF
expect -f autoexec.exp

凑合着功能实现了,后面有时间统一使用python再实现下。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注