[Nottingham] Wake on lan - run backup - power down

Ashley ashley.arb at gmail.com
Sat Oct 7 21:17:11 BST 2006


Hi,

>From my file server I want to start the backup server by WOL, run the backup
and then power it down.

I found the script below on the web and it works.  However it comes with the
warning "Be careful, you could corrupt valid entries if those NICs are
already active".  Does anyone have a better magic packet creating script
which is fail safe?

The next problem is powering down.  When I run halt (slackware) the power is
left on and the last line of output says Power Down.  I need it to actually
power down.  The machine I am testing on is a Pentium II with an ATX power
supply, so I assume it is capable of switching itself off.  I tried halt -p
but that made not difference.  Any suggestions?  I also tried apm -S and got
the message "No apm support in the kernel", do I really need apm support in
the kernel?

#!/usr/bin/perl -w
#
# Reads from stdin or a file lines of the form
#
# 12.34.56.78 aa:bb:cc:dd:ee;ff or
# foo.bar.com aa:bb:cc:dd:ee:ff
#
# which are hostname and MAC addresses of NICs to send a wakeup packet.
#
# This program may have to be run with superuser privilege because it
# may need to inject an ARP entry into the cache.
# Be careful, you could corrupt valid entries if those NICs are
# already active.
#
# Perl version by ken.yap at acm.org after DOS/Windows C version posted by
# Steve_Marfisi at 3com.com on the Netboot mailing list
# Released under GNU Public License, 2000-01-05
#
use Socket;

while (<>) {
    ($ip, $mac) = split;
    next if !defined($ip) or !defined($mac) or $ip eq '' or $mac eq '';
    &send_wakeup_packet($ip, $mac);
}

sub send_wakeup_packet {
    ($ip, $mac) = @_;

    if (!defined($iaddr = inet_aton($ip))) {
        print "Cannot resolve $ip\n";
        return;
    }
    if ($mac !~
/[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}:[\da-f]{2}/i)  {
        print "Malformed MAC address $mac\n";
        return;
    }
    # Inject entry into ARP table, in case it's not there already
    system("arp -s $ip $mac") == 0
        or print "Warning: arp command failed, you need to be root\n";
    # Remove colons
    $mac =~ tr/://d;
    # Magic packet is 6 bytes of FF followed by the MAC address 16 times
    $magic = ("\xff" x 6) . (pack('H12', $mac) x 16);
    # Create socket
    socket(S, PF_INET, SOCK_DGRAM, getprotobyname('udp'))
        or die "socket $!\n";
    # Send the wakeup packet
    print "Sending wakeup packet to $ip at MAC address $mac\n";
    defined(send(S, $magic, 0, sockaddr_in(0x2fff, $iaddr)))
        or print "send: $!\n";
    close(S);
}

Perhaps we can have an evening devoted to power saving and IT environmental
issues?

Thanks,
Ashley
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.lug.org.uk/pipermail/nottingham/attachments/20061007/6784a6ad/attachment.html


More information about the Nottingham mailing list