How can I generate a range of IP addresses in Perl?

前端 未结 5 1795
旧巷少年郎
旧巷少年郎 2020-12-18 01:38

I need to generate a list of IP-addresses (IPv4) in Perl. I have start and end addresses, for example 1.1.1.1 and 1.10.20.30. How can I print all the addresses inbetween?

5条回答
  •  星月不相逢
    2020-12-18 02:15

    It's all in how you code it. This is the fastest way I know.

    my $start = 0x010101; # 1.1.1
    my $end   = 0x0a141e; # 10.20.30
    
    for my $ip ( $start..$end ) { 
        my @ip = ( $ip >> 16 & 0xff
                 , $ip >>  8 & 0xff
                 , $ip       & 0xff
                 );
        print join( '.', 1, @ip ), "\n";
    }
    

提交回复
热议问题