一、问题背景
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 -p$password ssh -o StrictHostKeyChecking=no $username@$oaip "SHOW OA NETWORK" |grep 'IPv4 Address:'|awk '{print $3}'` oamask=`sshpass -p$password ssh -o StrictHostKeyChecking=no $username@$oaip "SHOW OA NETWORK" |grep 'Netmask:'|awk '{print $2}'` oagate=`sshpass -p$password ssh -o StrictHostKeyChecking=no $username@$oaip "SHOW OA NETWORK" |grep 'Gateway Address:'|awk '{print $3}'` clear echo -e '\n\n' 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 ] && shift $1 || shift echo ${1-0}.${2-0}.${3-0}.${4-0} } addcdr=$oaip/`mask2cdr $oamask` echo -e '\n\n' echo '******************************' echo We will scan the network echo And get can use ip now echo Please wait a moment!!! echo '******************************' fping -g $(echo `./net2ip $addcdr`)|grep 'unreachable' read -p "Enter can used ip for blade: " iloip read -p "Enter the blade bay id: " bid setip="set ebipa server $iloip $oamask $bid" setgate="set ebipa server gateway $oagate $bid" cat>autoexec.exp<<EOF #!/usr/bin/expect spawn ssh $username@$oaip expect { "(yes/no)?" { send "yes\n" expect "assword:" send "$password\n" } "assword:" { send "$password\n" } } expect "*>" send "set ebipa server $iloip $oamask $bid\n" expect "*bays?" send "YES\n" expect "*>" send "set ebipa server gateway $oagate $bid\n" sleep 2 expect "*?" send "YES\n" expect eof #interact EOF expect -f autoexec.exp
凑合着功能实现了,后面有时间统一使用python再实现下。