Add expect version of APC controller
[pcucontrol.git] / pcucontrol / models / exp / APC.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 set sequence [lindex $argv 6]
11
12 log_user 0
13 if { "$dryrun" == "True" } {
14     set reset_msg "Test: No error"
15 } else {
16     set reset_msg "Reset: No error"
17 }
18
19 set cmd_list { {"ssh $user@$host"} {"telnet $host"} }
20
21 if { "$method" == "ssh" || "$method" == "telnet" } {
22
23     set env(TERM) "vt100"
24     if { "$method" == "telnet" } {
25         spawn telnet $host
26     } else { 
27         spawn ssh $user@$host
28     }
29
30     expect {
31         "password:" { send "$password\n" ; exp_continue }
32         -re "User Name.*" { send "$user\r"; exp_continue}
33         -re "Password.*" { send "$password\r"; exp_continue}
34         -re "Access to the Control Console will be denied" { 
35                     send_user "Error $method: $expect_out(0,string)\n"; exit } 
36         "Are you sure you want to continue connecting (yes/no)? " { send "yes\n" ; exp_continue }
37         "Permission denied" { send_user "Error $method: username/password: $expect_out(0,string)"; exit }
38         "Could not resolve hostname" { send_user "Error $method: $expect_out(0,string)"; exit }
39         "Connection refused" { send_user "Error $method: Could not connect\n"; exit }
40         "Connection timed out" { send_user "Error $method: Connection timed out\n"; exit }
41         "No route to host" { send_user "Error $method: $expect_out(0,string)"; exit }
42         "> " { send "\r\n"; } 
43         timeout { send_user "Error $method: Timeout\n"; exit; }
44     }
45 }
46
47
48 set steps [split $sequence " "]
49 foreach step $steps {
50     #send_user "Sending step $step\n";
51     expect "> " { send "$step\r\n"; }
52 }
53
54 if { "$dryrun" == "True" } {
55     expect "Enter 'YES' to continue or <ENTER> to cancel" { send "\r\n\r\n"; } 
56 } else {
57     expect "Enter 'YES' to continue or <ENTER> to cancel" { send "YES\r\n"; } 
58 }
59 expect "> " { send "\e\e\e\e4\r\n"; } 
60
61 expect eof { send_user "$reset_msg\n" }