Wake On Lan implementation

Wake On Lan implementation

Postby Scorpei » Sun Dec 11, 2011 2:46 pm

I recently updated my nas to 10-1 (SPD8020), however in the process I lost my WOL script :(. I'm currently on the fence about keeping this NAS in operation anyway to be fair (have an Athlon X3 8650 system doing nothing, so possibly putting that into good use with the 500GB disk from the 8020), but loosing this script is really too bad. I've always used the NAS to SSH into my home network and wake my home system in order to work there. Sadly I did not backup the script however (I have a dd from the partitions but I have no clue if my script was on there). I believe it was a modifcation of this script:

http://www.hackernotcracker.com/2006-04 ... cript.html
or this one
Code: Select all
<?
function WakeOnLan($ip, $mac, $port)
{
$packet = "";
for($i = 0; $i < 6; $i++) $packet .= chr(0xFF);
for($i = 0; $i < 6; $i++) $packet .= chr((int)substr($mac, $i, $i +
2));
$nic = fsockopen("udp://" . $ip, $port));
if($nic==false){
return false;
fclose($nic);
}
fwrite($nic, $packet);
fclose($nic);
return true;
}

?>

But it's been over 2 and half years since I made/modified it all for use so I am unsure. I do think however that an implementation of WOL on the NAS would be a nice addition for many people to keep it functional as gatekeeper/off-site backup. Generating the script wasn't all that much work (if I was able to do it ;)) and adding it to the interface should also not be too hard (aside from possibly always requiring root). Just a suggestion :).

P.S. Sorry I lost my script, would have loved to share!!! It ran on 4.2 fvdw
Scorpei
 
Posts: 2
Joined: Sun Dec 11, 2011 12:02 pm

Re: Wake On Lan implementation

Postby fvdw » Sun Dec 11, 2011 10:20 pm

If you upgraded in one step from 4-2 to 10-1 then your script is still on the spd8020. :ugeek:

This is because the upgrade process (for the sp8020) is that the new firmware is written to another partition and the boot flag is changed to that partition and at next boot it boots from the partition were you have written the new firmware leaving the old one untouched.
(On the Lacies' another method is used)

In more detail. The disc has a partition sda1 and sda2 one of them is used for booting and has a boot flag set.
Assume the nas has booted form sda1. Now during a firmware upgrade the new firmware is written to sda2. And the boot flag is set from sda1 to sda2. At next boot it now boots from sda2. So the previous version is still there on sda1.
To view the content of sda1 just access the nas via SSH using putty. Make a a mount point for sda1 , and mount sda1 on that.
Code: Select all
mkdir /mnt/sda1
mount /dev/sda1 /mnt/sda1

If you now change to directory /mnt/sda1 you will be able to see the content of sda1 partition and copy whatever you want.

Of course if the nas was running on sda2 before upgrade then the new firmware is written to sda1 and boot flag is set to that. In that case you need to mount /dev/sda2

To find out which partition is used for booting just run fdisk and use the p command, you will get output like this
Code: Select all
root@SPD8020:/ # fdisk /dev/sda

The number of cylinders for this disk is set to 121601.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks  Id System
/dev/sda1               1          32      257008+ 83 Linux
/dev/sda2   *          33          64      257040  83 Linux
/dev/sda3              65          96      257040  82 Linux swap
/dev/sda4              97      121601   975988912+  5 Extended
/dev/sda5              97         144      385528+ 83 Linux
/dev/sda6             145      121601   975603321  83 Linux

Command (m for help):

Notice the * at sda2, this means my sps8020 has booted from sda2.

Of course if you have done several upgrades to come from the fvdw version were you script was still there to the one you are running now then it will be lost. But if you didn't do that then you can still save your script and share it with us :mrgreen: :D
fvdw
Site Admin - expert
 
Posts: 13471
Joined: Tue Apr 12, 2011 2:30 pm
Location: Netherlands

Re: Wake On Lan implementation

Postby Scorpei » Mon Dec 12, 2011 9:33 am

Of course, why didn't I think of that :P! Mounted and leached.

In my /usr/bin directory I had a small script for easy acces, stating:
Code: Select all
php -f /root/wol

As I only needed to wake one machine I have hardcoded it's information in the script, which should of course not be implemented in real life. A better solution would be to make the script available in /usr/bin

The script (/root/wol) is as follows:
Code: Select all
#!/usr/bin/php -q
<?php
# Wake on LAN - (c) HotKey@spr.at, upgraded by Murzik
# Modified by Allan Barizo http://www.hackernotcracker.com
# Ever so slightly modified by Simon van de Berg http://www.Scorpei.com
flush();
function WakeOnLan($addr, $mac,$socket_number) {
  $addr_byte = explode(':', $mac);
  $hw_addr = '';
  for ($a=0; $a <6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
  $msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
  for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;
  // send it to the broadcast address using UDP
  // SQL_BROADCAST option isn't help!!
  $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  if ($s == false) {
    echo "Error creating socket!\n";
    echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s));
    return FALSE;
    }
  else {
    // setting a broadcast option to socket:
    $opt_ret = socket_set_option($s, 1, 6, TRUE);
    if($opt_ret <0) {
      echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
      return FALSE;
      }
    if(socket_sendto($s, $msg, strlen($msg), 0, $addr, $socket_number)) {
      print "Magic Packet sent successfully!\n$mac\n\n";
      socket_close($s);
      return TRUE;
      }
    else {
      print "Magic packet failed!\n$mac\n\n";
      return FALSE;
      }
   
    }
  }
// Port number where the computer is listening. Usually, any number between 1-50000 will do. Normally people choose 7 or 9.
$socket_number = "7";
// MAC Address of the listening computer's network device
$mac_addy = "xx:xx:xx:xx:xx:xx";
// You can keep adding mac adresses here. In order to wake in batch or multiple NICs. You need to also add a line with the WakeOnLan command with this new $mac_addy*
# $mac_addy2 = "xx:xx:xx:xx:xx:xx";
# $mac_addy3 = "xx:xx:xx:xx:xx:xx";
# $mac_addy4 = "xx:xx:xx:xx:xx:xx";

// IP address of the listening computer. Input the domain name if you are using a hostname (like when under Dynamic DNS/IP)
$ip_addy = "192.168.2.255"; #gethostbyname("myhomeserver.dynamicdns.org");
 
WakeOnLan($ip_addy, $mac_addy,$socket_number);
# WakeOnLan($ip_addy, $mac_addy2,$socket_number);
# WakeOnLan($ip_addy, $mac_addy3,$socket_number);
# WakeOnLan($ip_addy, $mac_addy4,$socket_number);
 
?>

From what I gather from my own script I always sent my packets to both socket 7 and 9. This script like this works perfectly fine via command line and should be easy enough to modify for use in the webclient if one would want this. Hope it will be useful for some people. For me it means the difference in keeping this NAS in operation (together with the added SSL ftp / ssh).
Scorpei
 
Posts: 2
Joined: Sun Dec 11, 2011 12:02 pm


Return to Development

Who is online

Users browsing this forum: No registered users and 8 guests