Advertisement
test01ies

Cambia la IP a una dir IP fija estática Lliurex - change ip to a certain fix static IP linux lliurex

May 21st, 2024 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.48 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. # Function to configure the network interface with the static IP
  4. configure_ip() {
  5.     local aula=$1
  6.     local third_octet=$2
  7.     local fourth_octet=$3
  8.     local ip="10.2.${third_octet}.${fourth_octet}"
  9.     local netmask="255.255.255.0"
  10.     local gateway="10.2.${third_octet}.1"
  11.  
  12.     echo "Setting up static IP for aula ${aula}..."
  13.     echo "IP Address: ${ip}"
  14.     echo "Netmask: ${netmask}"
  15.     echo "Gateway: ${gateway}"
  16.  
  17.     # Replace "eth0" with your network interface name
  18.     interface="eth0"
  19.  
  20.     sudo ip addr flush dev ${interface}
  21.     sudo ip addr add ${ip}/24 dev ${interface}
  22.     sudo ip route add default via ${gateway} dev ${interface}
  23.  
  24.     echo "Static IP configuration completed for ${interface}"
  25. }
  26.  
  27. # Get user input
  28. read -p "Enter the aula: " aula
  29. read -p "Enter the third octet (decimal number): " third_octet
  30. read -p "Enter the PC number (last octet): " fourth_octet
  31.  
  32. # Validate the input
  33. if ! [[ ${third_octet} =~ ^[0-9]+$ ]] || ! [[ ${fourth_octet} =~ ^[0-9]+$ ]]; then
  34.     echo "Error: Both the third octet and the PC number must be decimal numbers."
  35.     exit 1
  36. fi
  37.  
  38. # Ensure the octets are within valid range
  39. if [ ${third_octet} -lt 0 ] || [ ${third_octet} -gt 255 ] || [ ${fourth_octet} -lt 1 ] || [ ${fourth_octet} -gt 254 ]; then
  40.     echo "Error: The third octet must be between 0 and 255, and the PC number must be between 1 and 254."
  41.     exit 1
  42. fi
  43.  
  44. # Configure the IP address
  45. configure_ip ${aula} ${third_octet} ${fourth_octet}
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement