ronniec has asked for the wisdom of the Perl Monks concerning the following question:

Hello again cellmates. I work from home for part of the week and in an office for the remainder of the time. I've inherited a script that is run from a PC in an attempt to waken (900) PC's on our Network. It works fine from my home PC - well okay it works! - but from the office it just seems to drop through and print the Usage details like a UNIX Here document. Even if I supply the -v option all I get is the usage details. I think it's something to do with the Getopt::Std or the Vars modules as it doesn't seem to be picking up the options? Using ppm I can't find any of these modules when I do a search but I would have thought that the the script would have failed with an appropriate message if they couldn't be located?
#!perl -c ###################################################################### +### # $Id: wakeonlan,v 1.4 2000/08/01 03:38:59 jpo Exp $ + # # + # # Example here: perl wakeonlan_rob.pl 00:04:75:F4:8F:B6 + # ###################################################################### +### use strict; use Socket; use Getopt::Std; use vars qw($VERSION $opt_v $opt_h $opt_i $opt_p $opt_f); $VERSION = '0.40'; my $DEFAULT_IP = '255.255.255.255'; my $DEFAULT_PORT = getservbyname('discard', 'udp'); # # Process the command line # getopts("hvp:i:f:"); if ($opt_h) { usage(); exit(0); } if ($opt_v) { print "wakeonlan version $VERSION\n"; exit(0); } if (!$opt_f and !@ARGV) { usage(); exit(0); } if ($opt_i) { $DEFAULT_IP = $opt_i; } # override default if ($opt_p) { $DEFAULT_PORT = $opt_p; } # override default if ($opt_f) { process_file($opt_f); } # The rest of the command line are a list of hardware addresses foreach (@ARGV) { wake($_, $opt_i, $opt_p); } # # wake # # The 'magic packet' consists of 6 times 0xFF followed by 16 times # the hardware address of the NIC. This sequence can be encapsulated # in any kind of packet, in this case UDP to the discard port (9). # + sub wake { my $hwaddr = shift; my $ipaddr = shift || $DEFAULT_IP; my $port = shift || $DEFAULT_PORT; my ($raddr, $them, $proto); my ($hwaddr_re, $pkt); # Validate hardware address (ethernet address) $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6); if ($hwaddr !~ m/^$hwaddr_re$/) { warn "Invalid hardware address: $hwaddr\n"; return undef; } # Generate magic sequence foreach (split /:/, $hwaddr) { $pkt .= chr(hex($_)); } $pkt = chr(0xFF) x 6 . $pkt x 16; # Alocate socket and send packet $raddr = gethostbyname($ipaddr); $them = pack_sockaddr_in($port, $raddr); $proto = getprotobyname('udp'); socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!"; setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $! +"; print "Sending magic packet to $ipaddr:$port with $hwaddr\n"; send(S, $pkt, 0, $them) or die "send : $!"; close S; } # # process_file # sub process_file { my $filename = shift; my ($hwaddr, $ipaddr, $port); open (F, "<$filename") or die "open : $!"; while(<F>) { next if /^\s*#/; # ignore comments next if /^\s*$/; # ignore empty lines chomp; ($hwaddr, $ipaddr, $port) = split; wake($hwaddr, $ipaddr, $port); } close F; } # # Usage # sub usage { print <<__USAGE__; Usage wakeonlan [-h] [-v] [-i IP_address] [-p port] [-f file] [[hardware +_address] ...] Options -h this information -v dislpays the script version -i ip_address set the destination IP address default: 255.255.255.255 (the limited broadcast address) -p port set the destination port default: 9 (discard port) -f file uses file as a source of hardware addresses See also wakelan(1) __USAGE__ } __END__
Any ideas? ( The -c at the shebang was just to show that there were no compilation errors - none of the required modules (Getopt:Std etc) were missing). The exact same zip file was used in the build of Perl on each PC (Version 5.8.7 Build 813) so they should be identical?!

Edited by planetscape - added readmore tags

( keep:1 edit:24 reap:0 )

Replies are listed 'Best First'.
Re: How to make Wake Win32 work?
by markh (Scribe) on Oct 24, 2006 at 16:01 UTC
    I am looking at your code, and I see that something seems odd with your shebang line:
    #!perl -c

    According to the perl docs, putting -c tells it to check the syntax only, and not to actually run the code. I'm not sure if that line is used when you are using perl under Win32, but that definitely is something I'd check.
      the shebang line is actually a *nix characteristic (since perl is native to *nix), and in a native windows perl implementation (activeperl for instance), i think the shebang is ignored altogether, as the file is usually executed by perl through a Windows Explorer file association.

      __________
      Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
      - Terry Pratchett

        in a native windows perl implementation (activeperl for instance), i think the shebang is ignored altogether,

        Not so.

        C:\test>perl #! perl -c $x = 1; ^Z - syntax OK

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        ...usually executed by perl through a Windows Explorer file association.

        I don't know the truth value of that statement, but "usually" is pretty broad-brush and unless I'm being even denser than usual, I don't see anything in OP's statement of the problem to support or rebut the notion that the script is being invoked via an association.

        I also have a notion (but not the time right now to check my recall) that "the shebang is ignored altogether" is also subject to some qualifications or exceptions. <- yep, I may be wrong, but will try to recheck this later. In the meantime, let us hope that wiser ones will confirm or rebut?