Remote Operation of a Powerswitch

Remote Operation of a Powerswitch

linux-logo


1. BACKGROUND
We need a powerswitch, because we want to be able to remotely cycle the power of a PC, in case that we cannot login from remote, thus allowing us to restart the machine from remote.
You may use a powerswitch also for any other purpose: switch on the light or heating device in your holiday cottage remotely...; you just need to connect the powerswitch to the Internet (or your local LAN).
Such devices are sometimes also called 'STONITH', when used in clusters. STONITH stands for 'Shoot The Other Node In The Head' (a rather brutal term...)
We bought the 'Power Switch Maverick Guard' from 'Black Box Network Services' for about CHF 500.- (about 350 or US-$ 440) and got the switch, a power cable, a RS-232 cable, a (Windoze) installation CD, and a small printed manual. For this price we would have expected to also get a cable for the outlet! So we had to build a cable by our own :-(   . The following describes, how we proceeded on a SuSE Linux 9.0 installation. There is no reason, why this procedure should not work on any other Linux distribution, such as Debian.

2. BASIC SETUP
Because we did not want to change the network parameters of the PC, we connected the powerswitch with the serial calbe to the PC and did the first setup using VMware and starting a HyperTerminal (9600 bauds). There we connected to the default IP-address of the powerswitch: 192.168.100.100 and entered several times 'Enter' until the prompt ">" appeared. The command /np brings you to the Network Parameters Menu, where you can enter the number of the parameter, you want to change. We changed the IP-address only (to 192.168.0.20).
Then enter /rs to restart and leave the configuration menu. Now the serial cable can be disconnected and further setup can be done from any browser on a PC in the same network.

3. FINE TUNING OF THE SETUP
Starting a browser in the network and entering http://admin-name:admin-password@192.168.0.20 is now sufficient for the rest of the setup:
- login as the default administrator name (admin) and the default password (admin).
- From the Menu 'General' select the port number, if you want to have another port than 80 (eg. 81)
- From the menu select 'Security' and:   - Set admin-name and admin-password as you like
  - Set a username and userpassword as you like (this user can only switch on and off)
  - Set the the IP-address ranges, from which the powerswitch can be reached (and select Permit/Deny and Activated)
- Click on 'Apply Changes'

4. SWITCHING WITH A BROWSER
This is easy: select the IP-address of the powerswitch (in our example http://192.168.0.20, login and click on 'Power' to switch on and off.

5. SWITCHING THROUGH A UNIX-COMMAND There is no documentation on how this can be done... So I had to find out by myself, what commands are sent to the powerswitch when clicking on the 'Power' icon. Using 'ethereal' is a good tool to find such things out:
- Start ethereal as root and start capturing packets
- Switch the powerswitch via web-interface on and off
- Stop capturing
- Click on the first packet you see, which is sent to the powerswitch
- From the menu choose 'Tools' and then 'Follow TCP-stream'
- You will see, that your browser sends a command P1=0 if you turn off the power (and P1=1 if you turn it on).

With this and using curl (see man curl or with the konqueror man:curl) and after some testing we find out, that the following command allows to switch a device ON:

curl -u user:userpassword --data-binary P1=1 http://192.168.0.20:81/cmd.html

(In the above example, use 80 instead of 81, or whatever port you have selected in paragraph 3).
For switching the device OFF, set P1=0 in the command above. Please note, that it is necessary to add the string /cmd.html in the web-address!!
And: Should you have a special character in your password, such as a $, the password must be masked and entered as \$.

Alternatively and if you have PHP installed on your system, you may create a PHP-script switch.php on your machine with the following content:

<?php
//
// Switch device at powerswitch ON: (P1=1) ; OFF: P1=0
//
$url = "http://192.168.0.20:81/cmd.html";
$ch =  curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "user:userpassword");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "P1=1");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
and then enter the command:   php -f switch.php   ; to switch the device off, use P1=0 in the php-script.
A useful tutorial to PHP can be found on http://www.zend.com/zend/tut/tutorial-thome3.php


Last Update: 21Dec2003 uk --- Created: 21Dec2003 uk