Advertisement
leonteale

ipexploder.sh

Sep 2nd, 2014
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.23 KB | None | 0 0
  1. #!/bin/bash
  2. shopt -s extglob
  3.  
  4. oct2bin=("000" "001" "010" "011" "100" "101" "110" "111")
  5.  
  6. function squishtogether {
  7.         result=""
  8.         while (($#)) ; do
  9.                 result="${result}${1}"
  10.                 shift
  11.         done
  12.         echo $result
  13. }
  14.  
  15. function splitapart {
  16.         string=$1
  17.         result=""
  18.         while ((${#string})) ; do
  19.                 rest=${string#?}
  20.                 chr=${string%$rest}
  21.                 if [[ -z $result ]] ; then
  22.                         result=$chr
  23.                 else
  24.                         result="${result} ${chr}"
  25.                 fi
  26.                 string=$rest
  27.         done
  28.         echo $result
  29. }
  30.  
  31. function addzeros {
  32. # will also remove zeros if needed
  33.         ZS=$2
  34.         NUM=$1
  35.         NUM=${NUM##+(0)}
  36.         NUM=$(printf "%0${ZS}d\n" $NUM)
  37.         echo $NUM
  38. }
  39.  
  40. function bin2dec {
  41.         string="2#${1}"
  42.         ((idec=$string))
  43.         echo $idec
  44. }
  45.  
  46. function dec2bin {
  47.         result=""
  48.         for i in $(splitapart $(printf "%3o" $1)) ; do
  49.                 result="${result}${oct2bin[i]}"
  50.         done
  51.         echo $result
  52. }
  53.  
  54. function ipbin2ip {
  55.    cont=1
  56.    for l in $(splitapart $1); do
  57.       VIPTMP[$cont]=$l
  58.       ((cont=cont+1))
  59.    done
  60.    ONE=`bin2dec $(squishtogether ${VIPTMP[@]:1:8})`
  61.    TWO=`bin2dec $(squishtogether ${VIPTMP[@]:9:8})`
  62.    THREE=`bin2dec $(squishtogether ${VIPTMP[@]:17:8})`
  63.    FOUR=`bin2dec $(squishtogether ${VIPTMP[@]:25:8})`
  64.    echo ${ONE}.${TWO}.${THREE}.${FOUR}
  65. }
  66. IPCIDR=$1
  67. IP=${IPCIDR%/*}
  68. BITS=${IPCIDR#*/}
  69.  
  70. FST=${IP%%.*}
  71. IP=${IP#*.}
  72. FSTBIN=`addzeros $(dec2bin "$FST") 8`
  73. SND=${IP%%.*}
  74. IP=${IP#*.}
  75. SNDBIN=`addzeros $(dec2bin "$SND") 8`
  76. TRD=${IP%%.*}
  77. FOH=${IP#*.}
  78. TRDBIN=`addzeros $(dec2bin "$TRD") 8`
  79. FOHBIN=`addzeros $(dec2bin "$FOH") 8`
  80.  
  81. VIP=0.0.0.0
  82. IPBIN="$FSTBIN$SNDBIN$TRDBIN$FOHBIN"
  83.  
  84. i=1
  85. for l in $(splitapart $IPBIN); do
  86.    VIPBIN[$i]="$l"
  87.    ((i=i+1))
  88. done
  89.  
  90. BITSHOST=$(expr 32 - $BITS)
  91. i=$BITSHOST
  92. while (( $i > 0 )); do
  93.    MAXHOSTBIN=1$MAXHOSTBIN
  94.    ((i=i-1))
  95. done
  96. MAXHOST=$(bin2dec $MAXHOSTBIN)
  97.  
  98. n=$(squishtogether ${VIPBIN[@]:1:$BITS})
  99. c=$MAXHOST
  100. while (( $c > 0 )); do
  101.    h=$c
  102.    EUREKA=${n}$(addzeros `dec2bin $h` $BITSHOST)
  103.    ipbin2ip "$EUREKA"
  104.    c=$(expr $c - 1)
  105. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement