1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #/bin/bash
# Ce paramètre peut être adapté à la configuration courante
defIF=eth0
## MIEUX VAUT NE RIEN MODIFIER SOUS CETTE LIGNE ##
PATH=$PATH:/sbin
checkIP() {
[[ "$2" =~ ^([0-9]{1,3} ){3}[0-9]{1,3}$ ]] && declare $1=\"$2\" || { echo "erreur dans le format des adresses requises"; exit 1; }
}
printAddr() {
local dotted comment="$1" array=($2)
case ${#array[@]} in 1) dotted="%s";; 2) dotted="%s.%s";; 3) dotted="%s.%s.%s";; 4) dotted="%s.%s.%s.%s";; esac
printf "\t\t%s \e[01;37m${dotted}\e[0m\n" "$comment" "${array[@]}"
[ -n "$3" ] && $FUNCNAME "${@:3}"
}
complmtA1() {
for ((x=0;x<${#1};x++)); do array[n]+="$((! ${1:x:1}))"; done
[ -n "$2" ] && { ((n++)); $FUNCNAME "${@: 2}"; } || printf '%s\n' "${array[@]}"
}
getInfo() {
read -a binValues <<<$(printf '%08d\n' $(bc <<< "obase=2; ${1// /;}; ${2// /;}"))
addr="${binValues[@]: -4}"
CIDR="${addr//[0 ]}"
invSubnetMask=( $(complmtA1 $addr) )
for i in "${binValues[@]::4}"
do networkAddress+=( $((2#$i & 2#${binValues[y+4]})) )
node_host+=( $((2#$i & 2#${invSubnetMask[y]})) )
broadcast+=( $((2#$i | 2#${invSubnetMask[y++]})) )
done
printf '\n\e[01;33m%s\t\e[01;36m%s\n\e[0;33m%s\t\e[0;36m%s\e[0m\n\ndans le Réseau: \e[01;37m%s.%s.%s.%s\e[0m/\e[0;37m%s\e[0m\n' \
"Adresse_IP" "Masque_Réseau" "${1// /.}" "${2// /.}" "${networkAddress[@]}" "${#CIDR}"
printAddr "le Numéro de Noeud de la Machine est" "${node_host[*]//0}" "l'Adresse de Diffusion est" "${broadcast[*]}"
read -sn1 -p$'\n\n\e[01;30mappuyez sur une touche"\e[0m'
printf $"\e[u\e[J"
}
printf $"\e[s"
while getopts a:n: options
do
case $options in
a) checkIP ipAddress "${OPTARG//./ }";;
n) checkIP subnetMask "${OPTARG//./ }";;
esac
done
(( OPTIND > 1 )) && getInfo "$ipAddress" "$subnetMask" || {
echo "usage: $0 [ <-a address> <-n netmask> ]"
addrS=($(sed -n '/inet adr/s/[^:]*:\([^ ]*\).*:\(.*\)/\1 \2/p' <(ifconfig $defIF)))
getInfo "${addrS[0]//./ }" "${addrS[1]//./ }" |
Partager