Bash script to list all IPs in prefix

后端 未结 11 1628
[愿得一人]
[愿得一人] 2020-12-23 12:23

I\'m trying to create script that I can input a set of prefixes, which will then list all IP addresses within the prefixes (including network/host/broadcast).

An ex

11条回答
  •  青春惊慌失措
    2020-12-23 13:10

    You can use this script
    (you need to have "bc" installed on your system):

    for ip in $@ ;do
            net=$(echo $ip | cut -d '/' -f 1);
            prefix=$(echo $ip | cut -d '/' -f 2);
            o1=$(echo $net | cut -d '.' -f4);
            o2=$(echo $net | cut -d '.' -f3);
            o3=$(echo $net | cut -d '.' -f2);
            o4=$(echo $net | cut -d '.' -f1);
            len=$(echo "2^(32 - $prefix)"|bc);
            for i in `seq $len`;do
                    echo "$o4.$o3.$o2.$o1";
                    o1=$(echo "$o1+1"|bc);
                    if [ $o1 -eq 256 ]; then
                            o1=0;
                            o2=$(echo "$o2+1"|bc);
                            if [ $o2 -eq 256 ]; then
                                    o2=0;
                                    o3=$(echo "$o3+1"|bc);
                                    if [ $o3 -eq 256 ]; then
                                            o3=0;
                                            o4=$(echo "$o4+1"|bc);
                                    fi
                            fi
                    fi
            done
    done
    

提交回复
热议问题