From cb212393caf2df4b004177453eef6036df1a5a6c Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Wed, 16 Sep 2015 18:10:24 +0200 Subject: [PATCH] add missing scripts --- master/opt/script/check_switches | 66 +++++++++++++++++++++ master/opt/script/set_blade_power | 99 +++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100755 master/opt/script/check_switches create mode 100755 master/opt/script/set_blade_power diff --git a/master/opt/script/check_switches b/master/opt/script/check_switches new file mode 100755 index 0000000..c5347c8 --- /dev/null +++ b/master/opt/script/check_switches @@ -0,0 +1,66 @@ +#!/bin/bash +# +# ============================================================================ +# +# Copyright (C) THALES. All rights reserved +# Author: Laurent Mazet +# +# Gestion attente switches au boot (ping,telnet) +# ============================================================================ + +# TODO +# * +CMD="" +MODE="iter" +PROGNAME=$(basename $0) +VER=1.0 +EXCLUDE="___" + +function usage () { + echo "usage: $PROGNAME [-c command] [-h] [-i] [-o] [-t seconds] [-v] [-w] [-x] host1 host2..." + echo " -c: set command (default '$CMD')" + echo " -h: help message" + echo " -i: infinity loop mode [default]" + echo " -o: only try ones" + echo " -t: timeout" + echo " -v: version message" + echo " -w: wait mode" + exit $@ +} + +while [ $# -gt 0 ]; do + case "$1" in + -c) shift; CMD+=" -c '${1// /@@}'";; + -h) usage 0;; + -i) MODE="iter";; + -o) MODE="ones";; + -t) shift; [[ "$1" =~ [^0-9] ]] && { echo "incorrect timeout ($1)"; exit 1;}; TIMEOUT=$1;; + -v) echo "$PROGNAME: version $VER"; exit;; + -w) MODE="wait";; + *) list+=" "$1;; + esac + shift +done + +# list +[ "$list" ] || list=$(awk '{sub(/#.*/, "")} /sw[gm]/ {print $2}' /etc/hosts) +list=$(echo $list" ") + +# timeout +if [ "$TIMEOUT" ]; then + trap "echo 'timeout expired (${TIMEOUT}s)'; exit 0;" SIGVTALRM + ( sleep $TIMEOUT; kill -SIGVTALRM $$ 2>/dev/null; [ $? -eq 0 ] && rc=1 || rc=0; exit $rc) & +fi +trap "exit 0;" SIGINT + +# main loop +while true; do + for host in $list; do + stat=0 + echo -n ${host}:" " + ping -c 1 $host >&/dev/null && { log=$(eval /opt/script/connect_switch $host ${CMD//@@/ } -c exit 2>/dev/null) && stat=1; echo ${log:=soon..} | sed 's/\r/\n'$host:' /g'; } || echo + [ "$MODE" = "wait" -a $stat -eq 1 ] && list=${list/$host } + done + [ "$MODE" = "ones" ] && break; + [[ "$list" =~ [a-zA-Z0-9_-] ]] && sleep 1 || break; +done diff --git a/master/opt/script/set_blade_power b/master/opt/script/set_blade_power new file mode 100755 index 0000000..e4f59f7 --- /dev/null +++ b/master/opt/script/set_blade_power @@ -0,0 +1,99 @@ +#!/usr/bin/expect + +# ============================================================================ +# +# Copyright (C) THALES. All rights reserved +# Author: Laurent Mazet +# +# Script de controle des lames HS22 IBM +# +# 2014-12-12 V1.0 : Creation +# +# ============================================================================ + +# default value +set blade -1 +set host "" +set login "admin" +set password "thalescom02" +set prompt "system>" +set state "on" + +proc usage {rc} { + puts "usage: set_blade_power \[host\] \[-b blade\] \[-h\] \[-l login\] \[-p password\] \[-x prompt\] \[-on|-off\]" + exit $rc +} + +# process argument +set action "" +for {set i 0} {$i < [llength $argv]} {incr i} { + set arg [lindex $argv $i] + switch $arg { + -b { set action "-blade" } + -h { usage 0 } + -l { set action "-login" } + -off { set state "off" } + -on { set state "on" } + -p { set action "-password" } + -x { set action "-prompt" } + default { + switch $action { + -blade { set blade $arg } + -login { set login $arg } + -password { set password $arg } + -prompt { set prompt $arg } + default { set host $arg } + } + set action "" + } + } +} +if {$action != ""} { + puts "missing arguments" + exit 1 +} +if { $host == "" } { + usage 1 +} +if { $blade <= 0 } { + puts "missing arguments" + exit 1 +} + +# start connection +spawn telnet $host + +# login +expect { + "username:" { send "$login\r" } + timeout { send_user "connection to host failed\n"; exit 1 } + eof { send_user "connection to host failed\n"; exit 1 } +} + + +# password +expect { + "password:" { send "$password\r" } + timeout { send_user "connection to host failed\n"; exit 1 } + eof { send_user "connection to host failed\n"; exit 1 } +} + +expect { + "$prompt" { send "env -T system:blade\[$blade\]\r" } + "username:" { send_user "connection to host failed\n"; exit 1 } +} + +expect { + "OK" { send "power -$state\r" } + "empty" { send "exit\r"; send_user "\nno blade in this slot\n"; exit 0 } + "$prompt" { send_user "error\n"; exit 1 } +} + +expect { + "OK" { send "exit\r" } + "$prompt" { send_user "error\n"; exit 1 } +} + +send_user "\n" +exit 0 +# vim:set tabstop=4 shiftwidth=4 softtabstop=4: -- 2.30.2