Cidre2addr

Aus Si:Wiki von Siegrist SystemLösungen - Informatik und Rezepte
Wechseln zu: Navigation, Suche
#!/usr/bin/perl
 
 
my @ret = ();
my $in = <>;
chomp($in);
 
if ( $in =~ /^(\d+\.\d+\.\d+\.\d+)\/(\d+)$/ ) {
	my ($addr, $mask) = ($1, $2);
	my $iaddr = dot2int($addr);
	for (my $i = 0; $i < 2**(32-$mask); $i++) {
		print int2dot($iaddr+$i), "\n";
	}
} else {
	print "Usage: $0 n.n.n.n/m\n";
}
 
 
sub dot2int {
	my @bytes = split(/\./,shift);
 
	return undef unless @bytes == 4 && ! grep {!(/\d+$/ && $_<256)} @bytes;
 
	return unpack("N",pack("C4",@bytes));
}
 
sub int2dot {
	return join('.',unpack('C4', pack("N", $_[0])));
}