efa456275585540b9809d7aea1299a8f0cb9ce61
[pcucontrol.git] / pcucontrol / models / exp / DRAC.exp
1 #!/usr/bin/expect
2
3 set timeout 20
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 2>&1
28         } else {
29             spawn ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no $user@$host 
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             -re "Received disconnect.*" { send_user "DRAC ssh: $expect_out(0,string)"; exit}
38             -re "\\$|/.*>|\[$user\]#" { send "\r"; } 
39             eof { set cont 1 }
40         }
41         if { $cont == 0 } { break; }
42
43     }
44
45     if { "$dryrun" == "True" } {
46         expect {
47             -re "\\$|/.*>" { send "racadm getsysinfo\r"; } 
48             -re "\[$user\]#" { send "getsysinfo\r" }
49         }
50     } else {
51         expect {
52             -re "\\$|/.*>" { send "racadm serveraction powercycle\r"; } 
53             -re "\[$user\]#" { send "serveraction powercycle\r" }
54         }
55     }
56     expect -re "\\$|/.*>|\[$user\]#" { send "exit\r"; } 
57
58     expect eof { send_user "$reset_msg\n" }
59
60 } else { 
61     if { [string match "*racadm*" $method] } {
62         if { "$dryrun" == "True" } {
63             set rac_cmd "getsysinfo"
64         } else {
65             set rac_cmd "serveraction powercycle"
66         }
67         send_user "/opt/dell/srvadmin/bin/$method -r $host -u $user -p '$password' $rac_cmd\n";
68         set cont 0
69         for { set retry 0 } { $retry < 2 } { incr retry 1 } {
70
71             # NOTE: For unknown reason, placing the pasword 
72             #       on the cmdline causes racadm to fail.
73             # NOTE: For unknown reason, some DRAC modules work only with
74             #       stderr redirected to stdout, others only work without it.
75             if { $retry == 0 } {
76                 set x [ spawn /opt/dell/srvadmin/bin/$method -i -r $host $rac_cmd 2>&1 ]
77             } else {
78                 set x [ spawn /opt/dell/srvadmin/bin/$method -i -r $host $rac_cmd  ]
79             }
80
81             expect {
82                 -re "ERROR:.*" { send_user "DRAC: $expect_out(0,string)"; exit }
83                 -re "UserName:|username:" { send "$user\r" ; exp_continue };
84                 -re "Password:|password:" { send "$password\r" };
85             }
86
87             expect {
88                 -re "Authentication failed.*" { send_user "DRAC: $expect_out(0,string)"; exit }
89                 -re "This.*not support remote RACADM" { send_user "DRAC: $expect_out(0,string)" ; exit }
90                 -re "ERROR: The syntax of the command specified is not correct." { set cont 1 }
91                 -re "RAC Information:" { sleep .1; }
92                 #-re "ERROR:.*" { send_user "DRAC: $expect_out(0,string)"; exit }
93             }
94             if { $cont == 0 } { break; }
95         }
96         # If one of these are present, then the status information has been returned.
97         # otherwise an expect error will be thrown, signaling the caller.
98         expect {
99             -re "Power Status.*" { sleep .1; } 
100             -re "RAC Firmware.*" { sleep .1; }
101         } 
102         expect eof { send_user "$reset_msg\n" }
103     }
104 }