Add expect version of APC controller
[pcucontrol.git] / pcucontrol / models / exp / DRAC.exp
1 #!/usr/bin/expect
2
3 set timeout 60
4 set method [lindex $argv 0]
5 set host [lindex $argv 1]
6 set user [lindex $argv 2]
7 set password [lindex $argv 3]
8 set dryrun [lindex $argv 4]
9 set model [lindex $argv 5]
10
11 log_user 0
12 if { "$dryrun" == "True" } {
13     set reset_msg "Test: No error"
14 } else {
15     set reset_msg "Reset: No error"
16 }
17
18
19 if { "$method" == "ssh" } {
20     #send_user "spawn ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no $user@$host\n"
21     set cont 0
22     for { set retry 0 } { $retry < 2 } { incr retry 1 } {
23
24         # NOTE: For unknown reason, some DRAC modules work only with
25         #       stderr redirected to stdout, others only work without it.
26         if { $retry == 0 } {
27             spawn ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no $user@$host 
28         } else {
29             spawn ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no $user@$host 2>&1
30         }
31
32         expect {
33             "Are you sure you want to continue connecting (yes/no)? " { send "yes\n" ; exp_continue }
34             "password: " { send "$password\n" ; exp_continue }
35             "Permission denied" { send_user "DRAC ssh: username/password: $expect_out(0,string)"; exit }
36             "Could not resolve hostname" { send_user "DRAC ssh: $expect_out(0,string)"; exit }
37             "Connection timed out" { send_user "DRAC ssh: $expect_out(0,string)"; exit }
38             -re "Received disconnect.*" { send_user "DRAC ssh: $expect_out(0,string)"; exit}
39             -re "\\$|/.*>|.*$user.*#" { send "\r"; } 
40             timeout { send_user "DRAC ssh: timeout\n" ; exit; }
41             eof { if { $cont == 1 } { send_user "DRAC ssh failed twice\n"; exit ; } ; set cont 1 }
42         }
43         if { $cont == 0 } { break; }
44
45     }
46
47     if { "$dryrun" == "True" } {
48         expect {
49             -re "\\$|/.*>" { send "racadm getsysinfo\r"; } 
50             -re ".*$user.*#" { send "getsysinfo\r" }
51         }
52     } else {
53         expect {
54             -re "\\$|/.*>" { send "racadm serveraction powercycle\r"; } 
55             -re ".*$user.*#" { send "serveraction powercycle\r" }
56         }
57     }
58     expect -re "\\$|/.*>|.*$user.*#" { send "exit\r"; } 
59
60     expect eof { send_user "$reset_msg\n" }
61
62 } else { 
63     if { [string match "*racadm*" $method] } {
64         if { "$dryrun" == "True" } {
65             set rac_cmd_a "getsysinfo"
66             set rac_cmd_b ""
67         } else {
68             set rac_cmd_a "serveraction"
69             set rac_cmd_b "powercycle"
70         }
71         #send_user "/opt/dell/srvadmin/bin/$method -r $host -u $user -p '$password' $rac_cmd\n";
72         set cont 0
73         for { set retry 0 } { $retry < 2 } { incr retry 1 } {
74
75             # NOTE: For unknown reason, placing the pasword 
76             #       on the cmdline causes racadm to fail.
77             # NOTE: For unknown reason, some DRAC modules work only with
78             #       stderr redirected to stdout, others only work without it.
79             # NOTE: For unknown reason, the racadm cmd does not recognize "serveraction powercycle" 
80             #       but does recognize "serveraction" "powercycle"
81             if { $retry == 0 } {
82                if { "$rac_cmd_a" == "getsysinfo" } {
83                    set x [ spawn /opt/dell/srvadmin/rac5/bin/$method -i -r $host $rac_cmd_a ]
84                } else {
85                    set x [ spawn /opt/dell/srvadmin/rac5/bin/$method -i -r $host $rac_cmd_a $rac_cmd_b  ]
86                }
87             } else {
88                if { "$rac_cmd_a" == "getsysinfo" } {
89                    set x [ spawn /opt/dell/srvadmin/rac5/bin/$method -i -r $host $rac_cmd_a 2>&1 ]
90                } else {
91                    set x [ spawn /opt/dell/srvadmin/rac5/bin/$method -i -r $host $rac_cmd_a $rac_cmd_b 2>&1 ]
92                }
93             }
94
95             expect {
96                 -re "ERROR:.*" { send_user "DRAC: $expect_out(0,string)"; exit }
97                 -re "UserName:|username:" { send "$user\r" ; exp_continue };
98                 -re "Password:|password:" { send "$password\r" };
99                 timeout { send_user "DRAC: timeout\n" ; exit }
100                 eof { send_user "DRAC: early EOF\n"; exit ; }
101             }
102
103             expect {
104                 -re "Authentication failed.*" { send_user "DRAC: $expect_out(0,string)"; exit }
105                 -re "This.*not support remote RACADM" { send_user "DRAC: $expect_out(0,string)" ; exit }
106                 -re "ERROR: The syntax of the command specified is not correct." { set cont 1 }
107                 -re "ERROR: the command that was entered is not valid" { set cont 1 }
108                 -re "INVALID ARGUMENT" { send_user "DRAC: received 'INVALID ARGUMENT'\n"; exit }
109                 -re "RAC Information:" { sleep .1; }
110                 timeout { send_user "DRAC: timeout\n" ; exit }
111                 eof { send_user "$reset_msg\n"; exit ; }
112                 #-re "ERROR:.*" { send_user "DRAC: $expect_out(0,string)"; exit }
113             }
114             if { $cont == 0 } { break; }
115         }
116         # If one of these are present, then the status information has been returned.
117         # otherwise an expect error will be thrown, signaling the caller.
118         expect {
119             -re "Power Status.*" { sleep .1; } 
120             -re "RAC Firmware.*" { sleep .1; }
121             eof { send_user "DRAC: early EOF\n"; exit ; }
122         } 
123         expect eof { send_user "$reset_msg\n" }
124     }
125 }