Bash ip Generator: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „<syntaxhighlight lang=bash> #!/bin/bash IP=$1 NM=$2 ip_dez() { local oktette=$1 IFS="." read -a okt <<< $oktette local dez=0 for ((x=0;x<=3;x++)) do NR=$(…“)
 
 
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt)
Zeile 26: Zeile 26:
 
   done
 
   done
 
   echo "${ip%.*}"
 
   echo "${ip%.*}"
}
 
 
cidr_netmask() {
 
  local cidr=$1
 
  local netmask=""
 
  local full_octets=$(( cidr / 8 ))
 
  local remainder=$(( cidr % 8 ))
 
 
  for ((i=0; i<4; i++)); do
 
    if (( i < full_octets )); then
 
      netmask+="255."
 
    elif (( i == full_octets )); then
 
      netmask+="$(( 256 - (2 ** (8 - remainder)) ))."
 
    else
 
      netmask+="0."
 
    fi
 
  done
 
 
  echo "${netmask%.*}"
 
 
}
 
}
  
Zeile 75: Zeile 56:
 
NN_D=$((IP_D & NM_D))
 
NN_D=$((IP_D & NM_D))
 
BC_D=$((NN_D + NM_EK))
 
BC_D=$((NN_D + NM_EK))
NN=$(dez_ip $NN_D)
 
BC=$(dez_ip $BC_D)
 
 
gen_range $NN_D $BC_D
 
gen_range $NN_D $BC_D
 
</syntaxhighlight>
 
</syntaxhighlight>

Aktuelle Version vom 12. Juni 2023, 07:12 Uhr

#!/bin/bash
IP=$1
NM=$2

ip_dez()
{
local oktette=$1
IFS="." read -a okt <<< $oktette
local dez=0
for ((x=0;x<=3;x++))
do
    NR=$(((3-$x)*8))
    dez=$((dez + ( ${okt[$x]} << $NR) ))
done
echo $dez
}


dez_ip(){
  local decimal=$1
  local ip=""
  for ((i=3; i>=0; i--)); do
    local octet=$(( decimal >> (i * 8) & 255 ))
    ip="$ip${octet}."
  done
  echo "${ip%.*}"
}

cidr_dez(){
    local cidr=$1
    cidr=$(((1 << 32) - (1 << (32 - cidr))))
    echo $cidr
}

cidr_ek(){
    local cidr=$1
    cidr=$(((1 << (32 - cidr)) - 1 ))
    echo $cidr
}

gen_range(){
   local start=$1
   local end=$2
   for ((ip_d=$start;ip_d<=$end;ip_d++))
   do
    dez_ip $ip_d
    done

}


IP_D=$(ip_dez $IP)
NM_D=$(cidr_dez $NM)
NM_EK=$(cidr_ek $NM)
NN_D=$((IP_D & NM_D))
BC_D=$((NN_D + NM_EK))
gen_range $NN_D $BC_D