[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3master/usr/share/se3/sbin/ -> bibliotheque_ip_masque.sh (source)

   1  #!/bin/bash
   2  
   3  # $Id: bibliotheque_ip_masque.sh 7719 2013-03-17 09:20:14Z keyser $
   4  # Bibliothèque de fonctions de calculs IP/MASQUE/...
   5  # Usage: source <chemin>/bibliotheque_ip_masque.sh
   6  #        calcule_reseau $IP $MASQUE
   7  #        calcule_broadcast $IP $MASQUE
   8  
   9  function puissance(){
  10      a=$1
  11      n=$2
  12      resultat=1
  13      cpt=1
  14      while [ $cpt -le $n ]
  15      do
  16          resultat=$(($resultat*$a))
  17          cpt=$((cpt+1))
  18      done
  19      echo $resultat
  20  }
  21  
  22  function binaire(){
  23      reste=$1
  24      diviseur=128
  25      chaine=""
  26      while [ $diviseur -ge 1 ]
  27      do
  28          if [ $reste -ge $diviseur ]; then
  29              reste=$(($reste-$diviseur))
  30              chaine=$chaine"1"
  31          else
  32              chaine=$chaine"0"
  33          fi
  34          diviseur=$(($diviseur/2))
  35      done
  36      echo $chaine
  37  }
  38  
  39  function octet(){
  40      chaine=$1
  41      octet=0
  42      cpt=0
  43      while [ $cpt -le 7 ]
  44      do
  45          b=$chaine:$cpt:1}
  46          octet=$(($octet+$(($(puissance 2 $((7-$cpt)))*$b))))
  47          cpt=$((cpt+1))
  48      done
  49      echo $octet
  50  }
  51  
  52  function et(){
  53      A=$1
  54      B=$2
  55      chaine=""
  56      cpt=0
  57      while [ $cpt -le 7 ]
  58      do
  59          a=$A:$cpt:1}
  60          b=$B:$cpt:1}
  61          if [ "$a" = "1" -a "$b" = "1" ]; then
  62              ajout="1"
  63          else
  64              ajout="0"
  65          fi
  66          chaine=$chaine}$ajout}
  67          cpt=$((cpt+1))
  68      done
  69      echo $chaine
  70  }
  71  
  72  function complement(){
  73      A=$1
  74      chaine=""
  75      cpt=0
  76      while [ $cpt -le 7 ]
  77      do
  78          a=$A:$cpt:1}
  79          if [ "$a" = "1" ]; then
  80              ajout="0"
  81          else
  82              ajout="1"
  83          fi
  84          chaine=$chaine}$ajout}
  85          cpt=$((cpt+1))
  86      done
  87      echo $chaine
  88  }
  89  
  90  function ou(){
  91      A=$1
  92      B=$2
  93      chaine=""
  94      cpt=0
  95      while [ $cpt -le 7 ]
  96      do
  97          a=$A:$cpt:1}
  98          b=$B:$cpt:1}
  99          if [ "$a" = "0" -a "$b" = "0" ]; then
 100              ajout="0"
 101          else
 102              ajout="1"
 103          fi
 104          chaine=$chaine}$ajout}
 105          cpt=$((cpt+1))
 106      done
 107      echo $chaine
 108  }
 109  
 110  #octet $1
 111  #puissance $1 $2
 112  #binaire $1
 113  #et 11001011 10010110
 114  #et 11001011
 115  #   10010110
 116  # = 10000010
 117  #et $1 $2
 118  
 119  #ou 11001011
 120  #   10010110
 121  # = 11011111
 122  #ou $1 $2
 123  
 124  function calcule_reseau(){
 125      IP=$1
 126      NETMASK=$2
 127      binreseau=""
 128      cpt=1
 129      while [ $cpt -le 4 ]
 130      do
 131          octip[$cpt]=$(echo $IP | cut -d"." -f$cpt)
 132          binip[$cpt]=$(binaire $octip[$cpt]})
 133  
 134          octmask[$cpt]=$(echo $NETMASK | cut -d"." -f$cpt)
 135          binmask[$cpt]=$(binaire $octmask[$cpt]})
 136  
 137          et_ajout=$(et $binip[$cpt]} $binmask[$cpt]})
 138          binreseau=$binreseau}$et_ajout}
 139          octreseau[$cpt]=$(octet $et_ajout})
 140  
 141          cpt=$((cpt+1))
 142      done
 143      echo "$octreseau[1]}.$octreseau[2]}.$octreseau[3]}.$octreseau[4]}"
 144  }
 145  
 146  function calcule_broadcast(){
 147      IP=$1
 148      NETMASK=$2
 149      binbroadcast=""
 150      cpt=1
 151      while [ $cpt -le 4 ]
 152      do
 153          octip[$cpt]=$(echo $IP | cut -d"." -f$cpt)
 154          binip[$cpt]=$(binaire $octip[$cpt]})
 155  
 156          octmask[$cpt]=$(echo $NETMASK | cut -d"." -f$cpt)
 157          binmask[$cpt]=$(binaire $octmask[$cpt]})
 158          compbinmask[$cpt]=$(complement $binmask[$cpt]})
 159  
 160          ou_ajout=$(ou $binip[$cpt]} $compbinmask[$cpt]})
 161          binbroadcast=$binbroadcast}$ou_ajout}
 162          octbroadcast[$cpt]=$(octet $ou_ajout})
 163  
 164          cpt=$((cpt+1))
 165      done
 166      echo "$octbroadcast[1]}.$octbroadcast[2]}.$octbroadcast[3]}.$octbroadcast[4]}"
 167  }


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1