Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.