1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- use Getopt::Std;
- use Socket;
-
- getopts('b:df:p:q');
-
- $brc = $opt_b || '255.255.255.255';
- $port = $opt_p || 60000;
- die "Malformed broadcast address: $brc!\n" if ($brc !~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)/);
-
- if (defined($opt_f)) {
- unless (open(F, $opt_f)) {
- print "open: $opt_f: $!\n";
- } else {
- print "Using file $opt_f...\n" if ($opt_d);
- while (<F>) {
- next if /^\s*
- my ($mac, $ip) = split;
- next if !defined($mac) or $mac eq '';
- &send_broadcast_packet($mac,$ip);
- }
- close(F);
- }
- }
- while (@ARGV) {
- send_broadcast_packet(shift(@ARGV));
- }
-
- sub send_broadcast_packet {
- my ($mac,$ip) = @_;
- if ($mac =~ /-/) {
- ($bc,$mac) = split(/-/,$mac);
- } else {
- $bc = $brc;
- }
- 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;
- }
- my $nbc = inet_aton($bc);
-
- $mac =~ tr/://d;
-
- $magic = ("\xff" x 6) . (pack('H12', $mac) x 16);
-
- socket(S, PF_INET, SOCK_DGRAM, getprotobyname('udp')) or die "socket: $!\n";
-
- setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt: $!\n";
-
- printf("$0: Sending wakeup packet to %04X:%08X-%s %s\n",$port,unpack('N',$nbc),uc($mac),$ip) if ($opt_d);
- defined(send(S, $magic, 0, sockaddr_in($port, $nbc)))
- or print "send: $!\n";
- close(S);
- }
|