Post by gideon007 on Feb 17, 2010 5:07:33 GMT 7
First of all you need of course OS X, one or more Thecus devices, the modules, sysuser, sshd, shutdown(reboot). You also need to install PuTTY for this script, since that allows passing on the password in the terminal. You can get PuTTY by installing MacPorts and then running "sudo port install putty".
Now for the script.
My script assumes two Thecus devices since I own two N5200. But it is easily adjustable to work for just one N5200 or more than two.
I left comments in the script, so it is a bit clearer what I'm doing in the script. Just copy/paste this into the AppleScriptEditor. You do need to enter the IP Address of your device and the passwords for the reboot and shutdown user. My script assumes that you gave these users the same password on both N5200s
Dialog box when both N5200 are running
Dialog box, when no N5200 is running
Now for the script.
My script assumes two Thecus devices since I own two N5200. But it is easily adjustable to work for just one N5200 or more than two.
I left comments in the script, so it is a bit clearer what I'm doing in the script. Just copy/paste this into the AppleScriptEditor. You do need to enter the IP Address of your device and the passwords for the reboot and shutdown user. My script assumes that you gave these users the same password on both N5200s
set ip_t1 to "0.0.0.0" -- enter IP of Thecus #1
set ip_t2 to "0.0.0.0" -- enter IP of Thecus #2
set reboot_pw to "PASSWORD" -- enter password of User reboot
set shutdown_pw to "PASSWORD" -- enter password of User shutdown
set list1 to {"Reboot Thecus #1", "Shutdown Thecus #1"}
set list2 to {"Reboot Thecus #2", "Shutdown Thecus #2"}
set listboth to {"Reboot both", "Shutdown both", "Reboot Thecus #1", "Shutdown Thecus #1", "Reboot Thecus #2", "Shutdown Thecus #2"}
set ping_result_1 to (do shell script "ping -c 1 -t 1 " & ip_t1 & "; echo -n") -- ping Thecus #1
set ping_result_2 to (do shell script "ping -c 1 -t 1 " & ip_t2 & "; echo -n") -- ping Thecus #2
set reboot_t1 to "plink -l reboot -pw " & reboot_pw & " " & ip_t1 -- command to reboot Thecus #1
set reboot_t2 to "plink -l reboot -pw " & reboot_pw & " " & ip_t2 -- command to reboot Thecus #2
set shutdown_t1 to "plink -l shutdown -pw " & shutdown_pw & " " & ip_t1 -- command to shutdown Thecus #1
set shutdown_t2 to "plink -l shutdown -pw " & shutdown_pw & " " & ip_t2 -- command to shutdown Thecus #2
(*
Now we ping both machines to see whether they are turned on or off.
We set on/off for both machines, Thecus #1 is t1 and Thecus #2 is t2
*)
if ping_result_1 contains " 0.0% packet loss" then
set t1 to "on"
else if ping_result_1 contains "100.0% packet loss" then
set t1 to "off"
end if
if ping_result_2 contains " 0.0% packet loss" then
set t2 to "on"
else if ping_result_2 contains "100.0% packet loss" then
set t2 to "off"
end if
(*
let's see which device is turned on
*)
if t1 = "on" and t2 = "on" then -- both are turned on
set choice to choose from list listboth with title "Thecus" with prompt "Reboot or Shutdown"
else if t1 = "on" and t2 = "off" then -- only Thecus #1 is turned on
set choice to choose from list list1 with title "Thecus" with prompt "Reboot or Shutdown"
else if t1 = "off" and t2 = "on" then -- only Thecus #2 is turned on
set choice to choose from list list2 with title "Thecus" with prompt "Reboot or Shutdown"
else if t1 = "off" and t2 = "off" then -- both are turned off
display dialog "No Thecus turned on" buttons ["Cancel"] with title "Thecus" with icon stop
end if
(*
Now we get to choose whether we want to reboot or shutdown the devices,
depending on which devices are turned on
*)
if choice is {"Reboot both"} then
tell application "Terminal"
activate
do script reboot_t1
delay 15
do script reboot_t2
end tell
else if choice is {"Shutdown both"} then
tell application "Terminal"
activate
do script shutdown_t1
delay 15
do script shutdown_t2
end tell
else if choice is {"Reboot Thecus #1"} then
tell application "Terminal"
activate
do script reboot_t1
end tell
else if choice is {"Shutdown Thecus #1"} then
tell application "Terminal"
activate
do script shutdown_t1
end tell
else if choice is {"Reboot Thecus #2"} then
tell application "Terminal"
activate
do script reboot_t2
end tell
else if choice is {"Shutdown Thecus #2"} then
tell application "Terminal"
activate
do script shutdown_t2
end tell
end if
Dialog box when both N5200 are running
Dialog box, when no N5200 is running