home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / pvfs2 / orangefs-2.8.3-20110323.tar.gz / orangefs-2.8.3-20110323.tar / orangefs / examples / heartbeat / hardware-specific / apc-switched-pdu-ssh-control.exp < prev    next >
Text File  |  2008-04-24  |  4KB  |  218 lines

  1. #!/usr/bin/expect -f
  2.  
  3. # expect script to login to a baytech management module and control power
  4. # to one of its outlets
  5.  
  6. # gather command line arguments into variables
  7. set host [lrange $argv 0 0]   
  8. set user [lrange $argv 1 1] 
  9. set password [lrange $argv 2 2] 
  10. set outlet [lrange $argv 3 3]
  11. set command [lrange $argv 4 4]
  12.  
  13. # complain if we don't get exactly 5 arguments
  14. if {$argc!=5} {
  15.     send_user "Usage: apc-switched-pdu-ssh-control.exp <host> <user> <password> <outlet> <cmd>\n"
  16.     send_user "   NOTE: <cmd> may be \"on\" \"off\" or \"reboot\"\n"
  17.     exit 1
  18. }
  19.  
  20. set apc_command ""
  21.  
  22. if { [string compare $command "on"] == 0 } {
  23.     set apc_command "1"
  24. }
  25. if { [string compare $command "off"] == 0 } {
  26.     set apc_command "2"
  27. }
  28. if { [string compare $command "reboot"] == 0 } {
  29.     set apc_command "3"
  30. }
  31.  
  32. if { [string compare $apc_command ""] == 0 } {
  33.     send_error "Error: <cmd> must be one of on|off|reboot.\n"
  34.     exit 1
  35. }
  36.  
  37. # use a 30 second timeout
  38. set timeout 30 
  39.  
  40. # this disables showing interaction on stdout.  It should be commented
  41. # if you are trying to debug this script and want to see what it is doing
  42. log_user 0
  43.  
  44. # delete old log file and start a new one
  45. #system rm -f /tmp/expect.log
  46. #log_file -a /tmp/expect.log
  47.  
  48. # open ssh connection.  Turn off strict host checking so ssh doesn't ask us 
  49. # if it is ok to connect to this hostname
  50. spawn ssh "-oStrictHostKeyChecking no" $user@$host 
  51.  
  52. # Look for passwod prompt
  53. expect {
  54.     "*?assword:*" {}
  55.     default {
  56.         # password prompt never showed up
  57.         send_user "failed to ssh to host $host\n"
  58.         exit 1
  59.     }
  60. }
  61.  
  62. # Send password aka $password 
  63. send -- "$password\r"
  64.  
  65. # look for top level prompt
  66. expect {
  67.     "> *" {}
  68.     default {
  69.         # our user name and password did not work
  70.         send_user "Error: host $host failed to accept username and password\n"
  71.         exit 1
  72.     }
  73. }
  74.  
  75. send -- "1\r"
  76.  
  77. expect {
  78.     "> *" {}
  79.     default {
  80.         # our user name and password did not work
  81.         send_user "Error: unable to control outlet\n"
  82.         exit 1
  83.     }
  84. }
  85.  
  86. send -- "2\r"
  87.  
  88. expect {
  89.     "> *" {}
  90.     default {
  91.         # our user name and password did not work
  92.         send_user "Error: unable to control outlet\n"
  93.         exit 1
  94.     }
  95. }
  96.  
  97. send -- "1\r"
  98.  
  99. while {1} {
  100.         expect {
  101.                 "*to continue*" {send -- "\r"}
  102.                 "> " {break }
  103.                 default {}
  104.         }
  105. }
  106.  
  107. send -- "$outlet\r"
  108.  
  109. expect {
  110.     "> *" {}
  111.     default {
  112.         # our user name and password did not work
  113.         send_user "Error: unable to control outlet\n"
  114.         exit 1
  115.     }
  116. }
  117.  
  118. send -- "1\r"
  119.  
  120. expect {
  121.     "> *" {}
  122.     default {
  123.         # our user name and password did not work
  124.         send_user "Error: unable to control outlet\n"
  125.         exit 1
  126.     }
  127. }
  128.  
  129.  
  130. send -- "$apc_command\r"
  131.  
  132. expect {
  133.     "*to continue*" {}
  134.     default {
  135.         # our user name and password did not work
  136.         send_user "Error: unable to control outlet\n"
  137.         exit 1
  138.     }
  139. }
  140.  
  141. send -- "YES\r"
  142.  
  143. while {1} {
  144.         expect {
  145.                 "*to continue*" {send -- "\r"}
  146.                 "> " {break }
  147.                 default {}
  148.         }
  149. }
  150.  
  151. send -- \003
  152.  
  153. expect {
  154.     "> *" {}
  155.     default {
  156.         # our user name and password did not work
  157.         send_user "Error: unable to control outlet\n"
  158.         exit 1
  159.     }
  160. }
  161.  
  162. send -- \003
  163.  
  164. expect {
  165.     "> *" {}
  166.     default {
  167.         # our user name and password did not work
  168.         send_user "Error: unable to control outlet\n"
  169.         exit 1
  170.     }
  171. }
  172.  
  173. send -- \003
  174.  
  175. while {1} {
  176.         expect {
  177.                 "*to continue*" {send -- "\r"}
  178.                 "> " {break }
  179.                 default {}
  180.         }
  181. }
  182.  
  183. send -- \003
  184.  
  185. expect {
  186.     "> *" {}
  187.     default {
  188.         # our user name and password did not work
  189.         send_user "Error: unable to control outlet\n"
  190.         exit 1
  191.     }
  192. }
  193.  
  194. send -- \003
  195.  
  196. expect {
  197.     "> *" {}
  198.     default {
  199.         # our user name and password did not work
  200.         send_user "Error: unable to control outlet\n"
  201.         exit 1
  202.     }
  203. }
  204.  
  205. # send logout command
  206. send -- "4\r"
  207.  
  208. expect {
  209.         eof {}
  210.         default {
  211.                 send_user "Error: could not log out cleanly\n"
  212.                 close
  213.                 wait
  214.                 exit 1
  215.         }
  216. }
  217.  
  218.