Post

IPcalc

Proporcionando una IP y una máscara de red, calcula el Network id, clase IP y el Broadcast con sus respectivas conversiones en binario.

VMnet_lab
Uso NetScan
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash

# Regular Colors
Black='\033[0;30m'        # Black
Red='\033[0;31m'          # Red
Green='\033[0;32m'        # Green
Yellow='\033[0;33m'       # Yellow
Blue='\033[0;34m'         # Blue
Purple='\033[0;35m'       # Purple
Cyan='\033[0;36m'         # Cyan
White='\033[0;37m'        # White
GrayL='\e[37m'            # Gray
EndC="\033[0m\e[0m"       # End colour

# Indicadores
declare -i parameter_counter=0

function helpPanel(){
	echo -e "\n\t${Purple}i) ${GrayL}Información networkID, netmask y brd de una IP/CIDR.${EndC}"
  echo -e "\t\t${Purple}- ${GrayL}Uso: './IpCIDR -i [192.168.0.1/24]'${EndC}"
  echo -e "\t${Purple}h) ${GrayL}Muestra el panel de ayuda.${EndC}\n"
}

function ipcidr(){
  ip="$1"
  # Comprobacion IP/CIDR formato
  ip_addr="${ip%/*}"
  mask="${ip#*/}"
  if ! [[ $ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]{1,2}$ ]]; then
    echo -e "\n${Red}Error:${GrayL} Formato IP incorrecto\n'./IpCIDR.sh -h' para más ayuda.\n"
    exit 1
  fi
  for octet in $(echo $ip_addr | tr "." " "); do
    if (( $octet > 255 )); then
      echo -e "\n${Red}Error:${EndC} El valor $octet es mayor que 255.\n'./IpCIDR.sh -h' para más ayuda.\n"
      exit 1
    fi
  done
  if (( $mask < 0 || $mask > 32 )); then
    echo -e "\n${Red}Error:${EndC} Mascara de red invalida.\n'./IpCIDR.sh -h' para más ayuda.\n"
    exit 1
  fi

  spaces=$(echo "$ip" | tr '.\/' ' ')
  read -r -a oct <<< "$spaces"
  for ((i=0; i<4; i++)); do
    octet_bin=$(echo "obase=2;${oct[i]}" | bc)
    octbin[i]=$(printf "%08d" $octet_bin)
  done
 
  # NetMask
  cidr=${oct[4]}
  mask=$((0xffffffff << (32 - $cidr)))
  mask_oct1=$((mask >> 24 & 0xff))
  mask_oct2=$((mask >> 16 & 0xff))
  mask_oct3=$((mask >> 8 & 0xff))
  mask_oct4=$((mask & 0xff))
  mask_arr=($mask_oct1 $mask_oct2 $mask_oct3 $mask_oct4)
  for ((i=0; i<4; i++)); do
    mask_bin=$(echo "obase=2;${mask_arr[i]}" | bc)
    maskbin[i]=$(printf "%08d" $mask_bin)
  done

  # Network ID
  networkid_oct1=$((oct[0] & mask_arr[0]))
  networkid_oct2=$((oct[1] & mask_arr[1]))
  networkid_oct3=$((oct[2] & mask_arr[2]))
  networkid_oct4=$((oct[3] & mask_arr[3]))
  networkid_arr=($networkid_oct1 $networkid_oct2 $networkid_oct3 $networkid_oct4)
  for ((i=0; i<4; i++)); do
    networkid_bin=$(echo "obase=2;${networkid_arr[i]}" | bc)
    networkid_bin_arr[i]=$(printf "%08d" $networkid_bin)
  done

  # Broadcast
  inverted_mask=$((mask ^ 0xffffffff))
  broadcast_oct1=$((networkid_oct1 | (inverted_mask >> 24 & 0xff)))
  broadcast_oct2=$((networkid_oct2 | (inverted_mask >> 16 & 0xff)))
  broadcast_oct3=$((networkid_oct3 | (inverted_mask >> 8 & 0xff)))
  broadcast_oct4=$((networkid_oct4 | (inverted_mask & 0xff)))
  broadcast_arr=($broadcast_oct1 $broadcast_oct2 $broadcast_oct3 $broadcast_oct4)
  for ((i=0; i<4; i++)); do
    broadcast_bin=$(echo "obase=2;${broadcast_arr[i]}" | bc)
    broadcast_bin_arr[i]=$(printf "%08d" $broadcast_bin)
  done
  # Determinar la clase de la IP
  if [ ${oct[0]} -lt 128 ]; then
    clase="Clase A"
  elif [ ${oct[0]} -lt 192 ]; then
    clase="Clase B"
  elif [ ${oct[0]} -lt 224 ]; then
    clase="Clase C"
  elif [ ${oct[0]} -lt 240 ]; then
    clase="Clase D"
  else
    clase="Clase E"
  fi

  # Output
  echo -e "\n${Yellow}[*]${Blue} Resultados para la IP: ${Purple}$ip${EndC}"
  echo -e "\n\t${Yellow}-${Green} IP: ${GrayL}$ip ${Green}${Green} -> ${GrayL}$clase${Green} ->${EndC} ${octbin[@]}${EndC}"
  echo -e "\t${Yellow}-${Green} Network Mask:${GrayL} ${mask_arr[@]}${Green} -> ${GrayL}${maskbin[@]}${EndC}"
  echo -e "\t${Yellow}-${Green} Network id: ${GrayL}${networkid_arr[@]}${Green} <-> ${GrayL}${networkid_bin_arr[@]}${EndC}"
  echo -e "\t${Yellow}-${Green} Broadcast: ${GrayL}${broadcast_arr[@]}${Green} <->${GrayL} ${broadcast_bin_arr[@]}\n"
}

while getopts "hi:" arg; do
	case $arg in
    i) ip="$OPTARG"; let parameter_counter+=1;;
    h) ;;
	esac
done

if [ $parameter_counter -eq 1 ]; then
	ipcidr $ip
else
  helpPanel
fi
This post is licensed under CC BY 4.0 by the author.