mirror of https://gitlab.com/hp3icc/emq-TE1.git
168 lines
6.0 KiB
Bash
168 lines
6.0 KiB
Bash
#!/bin/bash
|
|
if [ "$(cat /proc/cpuinfo | grep 'Raspberry')" != "" ]; then
|
|
# menu-ip
|
|
sudo cat > /bin/menu-ip <<- "EOF"
|
|
#!/bin/bash
|
|
|
|
# Función para convertir una máscara de subred en notación CIDR
|
|
mask_to_cidr() {
|
|
local IFS
|
|
IFS=.
|
|
read -r i1 i2 i3 i4 <<< "$1"
|
|
local cidr=0
|
|
local octets=("$i1" "$i2" "$i3" "$i4")
|
|
|
|
for octet in "${octets[@]}"; do
|
|
while [ "$octet" -ne 0 ]; do
|
|
cidr=$((cidr + octet % 2))
|
|
octet=$((octet / 2))
|
|
done
|
|
done
|
|
|
|
echo "$cidr"
|
|
}
|
|
|
|
while :
|
|
do
|
|
choix=$(whiptail --title "IP Tools" --menu "Selecciona una opción:" 15 60 6 \
|
|
1 "Ver IP Local" \
|
|
2 "Ver IP Pública" \
|
|
3 "Set IP Static /DHCP" \
|
|
4 "Salir" 3>&1 1>&2 2>&3)
|
|
|
|
exitstatus=$?
|
|
|
|
if [ $exitstatus = 0 ]; then
|
|
echo "Opción seleccionada:" $choix
|
|
else
|
|
echo "Has elegido cancelar. Saliendo..."
|
|
break
|
|
fi
|
|
|
|
case $choix in
|
|
1)
|
|
# Ver IP Local (muestra la IP local de los dispositivos)
|
|
device_list=$(ip -o link show | awk -F: '$2 !~ /lo/{print $2}')
|
|
ip_list=""
|
|
for device in $device_list; do
|
|
local_ip=$(ip -4 -o addr show $device | awk '{print $4}')
|
|
ip_list="$ip_list$device = $local_ip\n"
|
|
done
|
|
whiptail --title "IP Local" --msgbox "Tu IP local es:\n\n$ip_list" 15 60
|
|
;;
|
|
2)
|
|
# Ver IP Pública (muestra la IP pública)
|
|
public_ip=$(curl -s https://ifconfig.me)
|
|
whiptail --title "IP Pública" --msgbox "Tu IP pública es:\n\n$public_ip" 10 40
|
|
;;
|
|
3)
|
|
# Configurar Static / DHCP (muestra la lista de dispositivos y permite seleccionar uno)
|
|
device_list=$(ip -o link show | awk -F: '$2 !~ /lo/{print $2}')
|
|
selected_device=""
|
|
|
|
while [ -z "$selected_device" ]; do
|
|
options=()
|
|
for device in $device_list; do
|
|
options+=("$device" "")
|
|
done
|
|
|
|
selected_device=$(whiptail --title "Static / DHCP" --menu "Selecciona un dispositivo de red:" 15 60 4 "${options[@]}" 3>&1 1>&2 2>&3)
|
|
|
|
if [ $? -ne 0 ]; then
|
|
echo "Has elegido cancelar. Saliendo..."
|
|
break
|
|
fi
|
|
|
|
if [ "$selected_device" == "eth0" ]; then
|
|
device_name="Wired connection 1"
|
|
elif [ "$selected_device" == "wlan0" ]; then
|
|
device_name=$(nmcli -t -f active,ssid dev wifi | egrep '^yes' | cut -d: -f2)
|
|
fi
|
|
|
|
choice=$(whiptail --title "Static / DHCP" --menu "Selecciona una opción para $selected_device:" 15 60 4 \
|
|
1 "Configurar DHCP" \
|
|
2 "Configurar Static" \
|
|
3 "Volver" 3>&1 1>&2 2>&3)
|
|
|
|
case $choice in
|
|
1)
|
|
# Configurar DHCP para el dispositivo seleccionado
|
|
sudo nmcli con mod "$device_name" ipv4.addresses "" ipv4.gateway ""
|
|
sudo nmcli con mod "$device_name" ipv4.method auto
|
|
sudo nmcli con up "$device_name"
|
|
sudo systemctl stop NetworkManager &&
|
|
sudo systemctl start NetworkManager
|
|
whiptail --title "Static / DHCP" --msgbox "Se ha configurado DHCP para $device_name." 10 40
|
|
;;
|
|
2)
|
|
# Configurar IP estática para el dispositivo seleccionado
|
|
ip=""
|
|
mask=""
|
|
gateway=""
|
|
dns1=""
|
|
dns2=""
|
|
whiptail --title "Static / DHCP" --inputbox "Ingresa la IP estática para $device_name:" 10 60 2>/tmp/ip.txt
|
|
if [ $? -ne 0 ]; then
|
|
echo "Has elegido cancelar. Saliendo..."
|
|
break
|
|
fi
|
|
ip=$(cat /tmp/ip.txt)
|
|
whiptail --title "Static / DHCP" --inputbox "Ingresa la máscara de subred en formato de octetos para $device_name (ejemplo: 255.255.255.0):" 10 60 2>/tmp/mask.txt
|
|
if [ $? -ne 0 ]; then
|
|
echo "Has elegido cancelar. Saliendo..."
|
|
break
|
|
fi
|
|
mask=$(cat /tmp/mask.txt)
|
|
cidr=$(mask_to_cidr "$mask")
|
|
whiptail --title "Static / DHCP" --inputbox "Ingresa la puerta de enlace (gateway) para $device_name:" 10 60 2>/tmp/gateway.txt
|
|
if [ $? -ne 0 ]; then
|
|
echo "Has elegido cancelar. Saliendo..."
|
|
break
|
|
fi
|
|
gateway=$(cat /tmp/gateway.txt)
|
|
whiptail --title "Static / DHCP" --inputbox "Ingresa la dirección DNS primaria (dns1) para $device_name:" 10 60 2>/tmp/dns1.txt
|
|
if [ $? -ne 0 ]; then
|
|
echo "Has elegido cancelar. Saliendo..."
|
|
break
|
|
fi
|
|
dns1=$(cat /tmp/dns1.txt)
|
|
whiptail --title "Static / DHCP" --inputbox "Ingresa la dirección DNS secundaria (dns2) para $device_name:" 10 60 2>/tmp/dns2.txt
|
|
if [ $? -ne 0 ]; then
|
|
echo "Has elegido cancelar. Saliendo..."
|
|
break
|
|
fi
|
|
dns2=$(cat /tmp/dns2.txt)
|
|
|
|
# Configurar IP estática utilizando nmcli
|
|
sudo nmcli con mod "$device_name" ipv4.method manual ipv4.addresses "$ip/$cidr" ipv4.gateway "$gateway" ipv4.dns "$dns1 $dns2"
|
|
sudo nmcli con up "$device_name"
|
|
sudo systemctl stop NetworkManager
|
|
sudo systemctl start NetworkManager
|
|
whiptail --title "Static / DHCP" --msgbox "Se ha configurado una IP estática para $device_name." 10 40
|
|
;;
|
|
3)
|
|
# Volver al menú anterior
|
|
selected_device=""
|
|
;;
|
|
*)
|
|
;;
|
|
esac
|
|
done
|
|
;;
|
|
4)
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
|
|
exit 0
|
|
else
|
|
whiptail --title "IP Tools" --msgbox "Esta opcion solo esta disponible para Raspberry / This option is only available for Raspberry" 0 50
|
|
exit 0
|
|
|
|
EOF
|
|
|
|
chmod +x /bin/menu-ip
|
|
ln -sf /bin/menu-ip /bin/MENU-IP
|
|
chmod +x /bin/MENU*
|
|
fi |