Skip to content

Instantly share code, notes, and snippets.

@blacknon
Last active January 30, 2023 01:39
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 blacknon/75df3bf797b051457dfdb6c9e272fca7 to your computer and use it in GitHub Desktop.
Save blacknon/75df3bf797b051457dfdb6c9e272fca7 to your computer and use it in GitHub Desktop.
Get IP address list from cidr perl oneliner
echo 192.168.100.9/28|perl -aF[./] -lne'for($i=0;$i<4;$i++){$x.=sprintf("%08b",$F[$i])}$y=$F[4];$k=substr($x,0,$y);$m=2**(32-$y);for($j=0;$j<$m;$j++){$a=$k.sprintf("%0".(32-$y)."b",$j);for($n=0;$n<4;$n++){$b.=".".sprintf(oct("0b".substr($a,$n*8,8)))};$b=~s/^\.//g;print $b;$b=""}'
# もうちょっと短くして改行したもの
echo 192.168.100.9/28|perl -aF[./] -E'
$x.=sprintf("%08b",$F[$_])for 0..3;
$y=$F[4];$z=32-$y;
$m=2**$z;
for($j=0;$j<$m;$j++){
    $a=substr($x,0,$y).sprintf("%0".$z."b",$j);
    $b[$j].=".".sprintf(oct("0b".substr($a,$_*8,8)))for 0..3;
    $b[$j]=~s/^\.//g;
    say $b[$j];}
print $b'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment