| 1 | #!/usr/bin/expect |
| 2 | |
| 3 | # ============================================================================ |
| 4 | # |
| 5 | # Copyright (C) THALES. All rights reserved |
| 6 | # Author: Laurent Mazet |
| 7 | # |
| 8 | # Script de controle des lames HS22 IBM |
| 9 | # |
| 10 | # 2014-12-12 V1.0 : Creation |
| 11 | # |
| 12 | # ============================================================================ |
| 13 | |
| 14 | # default value |
| 15 | set blade -1 |
| 16 | set host "" |
| 17 | set login "admin" |
| 18 | set password "thalescom02" |
| 19 | set prompt "system>" |
| 20 | set state "on" |
| 21 | |
| 22 | proc usage {rc} { |
| 23 | puts "usage: set_blade_power \[host\] \[-b blade\] \[-h\] \[-l login\] \[-p password\] \[-x prompt\] \[-on|-off\]" |
| 24 | exit $rc |
| 25 | } |
| 26 | |
| 27 | # process argument |
| 28 | set action "" |
| 29 | for {set i 0} {$i < [llength $argv]} {incr i} { |
| 30 | set arg [lindex $argv $i] |
| 31 | switch $arg { |
| 32 | -b { set action "-blade" } |
| 33 | -h { usage 0 } |
| 34 | -l { set action "-login" } |
| 35 | -off { set state "off" } |
| 36 | -on { set state "on" } |
| 37 | -p { set action "-password" } |
| 38 | -x { set action "-prompt" } |
| 39 | default { |
| 40 | switch $action { |
| 41 | -blade { set blade $arg } |
| 42 | -login { set login $arg } |
| 43 | -password { set password $arg } |
| 44 | -prompt { set prompt $arg } |
| 45 | default { set host $arg } |
| 46 | } |
| 47 | set action "" |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | if {$action != ""} { |
| 52 | puts "missing arguments" |
| 53 | exit 1 |
| 54 | } |
| 55 | if { $host == "" } { |
| 56 | usage 1 |
| 57 | } |
| 58 | if { $blade <= 0 } { |
| 59 | puts "missing arguments" |
| 60 | exit 1 |
| 61 | } |
| 62 | |
| 63 | # start connection |
| 64 | spawn telnet $host |
| 65 | |
| 66 | # login |
| 67 | expect { |
| 68 | "username:" { send "$login\r" } |
| 69 | timeout { send_user "connection to host failed\n"; exit 1 } |
| 70 | eof { send_user "connection to host failed\n"; exit 1 } |
| 71 | } |
| 72 | |
| 73 | |
| 74 | # password |
| 75 | expect { |
| 76 | "password:" { send "$password\r" } |
| 77 | timeout { send_user "connection to host failed\n"; exit 1 } |
| 78 | eof { send_user "connection to host failed\n"; exit 1 } |
| 79 | } |
| 80 | |
| 81 | expect { |
| 82 | "$prompt" { send "env -T system:blade\[$blade\]\r" } |
| 83 | "username:" { send_user "connection to host failed\n"; exit 1 } |
| 84 | } |
| 85 | |
| 86 | expect { |
| 87 | "OK" { send "power -$state\r" } |
| 88 | "empty" { send "exit\r"; send_user "\nno blade in this slot\n"; exit 0 } |
| 89 | "$prompt" { send_user "error\n"; exit 1 } |
| 90 | } |
| 91 | |
| 92 | expect { |
| 93 | "OK" { send "exit\r" } |
| 94 | "$prompt" { send_user "error\n"; exit 1 } |
| 95 | } |
| 96 | |
| 97 | send_user "\n" |
| 98 | exit 0 |
| 99 | # vim:set tabstop=4 shiftwidth=4 softtabstop=4: |