例如
my $starting_ip = '1.2.3.4';
my $ending_ip = '1.3.0.5';
我的输出将是
1.2.3
1.2.4
1.2.5
..
..
1.2.255
1.3.0
我到目前为止尝试过:
use Socket;
my $startIp = $ARGV[0];
my $endIp = $ARGV[1];
my $start_address = unpack 'N', inet_aton( $startIp );
my $finish_address = unpack 'N', inet_aton( $endIp );
my @f = split( '\.', $endIp );
my $last_ip = join( '.', $f[0], $f[1], $f[2] );
for ( my $address = $start_address;
$address <= $finish_address;
$address = $address + 255 ) {
my $new_ip = inet_ntoa( pack 'N', $address );
my @a = split( '\.', $new_ip );
$new_ip = join( '.', $a[0], $a[1], $a[2] );
print "$new_ip\n";
}
print "$last_ip\n";
无需考虑IP地址的最后部分。
答案 0 :(得分:0)
喜欢这样吗?
sub ip_addr { $_[0] = pack "N", 1 + unpack "N", $_[0] }
my $starting_ip = 1.2.4.4;
my $ending_ip = 1.3.0.5;
for ( $addresses = $starting_ip; $addresses le $ending_ip; ip_addr($addresses) ) {
printf "%vd\n", $addresses;
}
虽然还有很多其他方法,但我认为更好的选择是使用Net::IP