Skip to content

Instantly share code, notes, and snippets.

@bobrik
Created September 4, 2017 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobrik/734aa556048c93f0b6f7b47de3ed7bd9 to your computer and use it in GitHub Desktop.
Save bobrik/734aa556048c93f0b6f7b47de3ed7bd9 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
# Original source: https://lkml.org/lkml/2016/12/7/114
# Changesd to ping the whole 8.8.x.x subnet to produce lots of entries.
my $subnet = '8.8';
use Socket;
use Symbol;
use NetAddr::IP::Lite;
sub ICMP_ECHO () { 8 }
sub ICMP_SUBCODE () { 0 }
sub ICMP_STRUCT () { 'C2S3A56' }
sub ICMP_FLAGS () { 0 }
sub ICMP_PORT () { 0 }
$sequence=0;
for $j (1..255) {
for $i (2..254) {
$protocol = (getprotobyname('icmp'))[2] or
die('Cannot get ICMP protocol number by name - ', $!);
$socket = Symbol::gensym;
socket($socket, PF_INET, SOCK_RAW, $protocol) or
die('Cannot create IMCP socket - ', $!);
$sequence = ($sequence+1) & 0xFFFF;
my $checksum = 0;
my $msg = pack(
ICMP_STRUCT,
ICMP_ECHO,
ICMP_SUBCODE,
$checksum,
$$ & 0xFFFF,
$sequence,
'0' x 56
);
my $short = int(length($msg) / 2);
$checksum += $_ for unpack "S$short", $msg;
$checksum += ord(substr($msg, -1)) if length($msg) % 2;
$checksum = ($checksum >> 16) + ($checksum & 0xFFFF);
$checksum = ~(($checksum >> 16) + $checksum) & 0xFFFF;
$msg = pack(
ICMP_STRUCT,
ICMP_ECHO,
ICMP_SUBCODE,
$checksum,
$$ & 0xFFFF,
$sequence,
'0' x 56
);
my($address)=$single?$single:"$subnet.$j.$i";
my $netaddr = inet_aton($address);
my $sockaddr = pack_sockaddr_in(ICMP_PORT, $netaddr);
send($socket, $msg, ICMP_FLAGS, $sockaddr) or
die("ERROR ($address) sending ICMP packet - $!");
}
}
print "OK\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment