Debian Codegraph @

drivers/reboot-apc

raw
1 #!/usr/bin/expect --
2 #
3 # Reboot a machine connected to an APC power strip
4 #
5 # Copyright 2007 Google Inc., Martin J. Bligh <[email protected]>
6 set P "reboot-apc"
7
8 #
9 # OPTIONS: options parser.
10 #
11 proc shift {_list} {
12 upvar $_list list
13 set res [lindex $list 0]
14 set list [lreplace $list 0 0]
15 return $res
16 }
17
18 proc arg {_list arg} {
19 upvar $_list list
20 if {[llength $list] < 1} {
21 puts stderr "$arg: required argument missing"
22 exit 1
23 }
24 return [shift list]
25 }
26
27 set timeout 10
28 set user {apc}
29 set pass {apc}
30 set host [lindex $argv 0]
31 set outlet [lindex $argv 1]
32 shift argv
33 shift argv
34
35 while {[llength $argv] > 0} {
36 puts "length [llength $argv]"
37 switch -- [shift argv] {
38 -p { set pass [arg argv p]}
39 -u { set user [arg argv u]}
40 }
41 }
42
43 if {[llength $argv] > 0} {
44 puts stderr "Usage: $P <host> <outlet> [-u <user>] [-p <pass>]"
45 exit 1
46 }
47
48 if {[string compare $host ""] == 0 ||
49 [string compare $outlet ""] == 0} \
50 {
51 puts stderr "host and outlet required"
52 exit 1
53 }
54
55 spawn telnet $host
56 expect "User Name :"
57 send $user
58 send "\r"
59 expect "Password :"
60 send $pass
61 send "\r"
62 expect "1- Device Manager"
63 expect "> "
64 send "1\r"
65 # We get a different prompt if we're just an outlet controller
66 # decide which response we need to enter
67 set timeout 2
68 expect {
69 "3- Outlet Control/Configuration" {
70 send "3\r"
71 exp_continue
72 }
73 "2- Outlet Control" {
74 send "2\r"
75 exp_continue
76 }
77 }
78 send "\r"
79 expect "> "
80 send $outlet
81 send "\r"
82 # Here too, if we're just an outlet controller we don't get the option
83 # to modify configuration
84 expect {
85 "1- Control Outlet" {
86 send "1\r"
87 }
88 }
89 expect "3- Immediate Reboot"
90 expect "> "
91 send "3\r"
92 expect "Immediate Reboot"
93 expect "Enter 'YES' to continue or <ENTER> to cancel :"
94 send "YES\r"
95 expect "Press <ENTER> to continue..."