From tool, 1 Month ago, written in Bash.
  1. #!/bin/bash
  2.  
  3. declare -A masks
  4. masks=(
  5.     ["255.0.0.0"]="/8"
  6.     ["255.128.0.0"]="/9" ["255.192.0.0"]="/10" ["255.224.0.0"]="/11" ["255.240.0.0"]="/12" ["255.248.0.0"]="/13" ["255.252.0.0"]="/14" ["255.254.0.0"]="/15" ["255.255.0.0"]="/16" ["255.255.128.0"]="/17" ["255.255.192.0"]="/18" ["255.255.224.0"]="/19" ["255.255.240.0"]="/20" ["255.255.248.0"]="/21" ["255.255.252.0"]="/22" ["255.255.254.0"]="/23" ["255.255.255.0"]="/24" ["255.255.255.128"]="/25" ["255.255.255.192"]="/26" ["255.255.255.224"]="/27" ["255.255.255.240"]="/28" ["255.255.255.248"]="/29" ["255.255.255.252"]="/30"
  7. )
  8.  
  9. read_hostname() {
  10.  
  11.     echo -n "Please enter a hostname for the VM (e.g.: newvm.example.com): "
  12.     read vmhostname
  13.  
  14.     char="."
  15.     hnumperiods=$(echo "${vmhostname}" | awk -F"${char}" '{print NF-1}')
  16.  
  17.     if [[ -z "$vmhostname" || "$hnumperiods" -le 1 ]]; then
  18.         echo -e "nPlease enter a fully qualified hostname, like newvm.example.com or newvm.example.localn"
  19.         read_hostname
  20.     fi
  21.  
  22. }
  23.  
  24. read_ip() {
  25.  
  26.     vmip=''
  27.     echo -n "Please enter IP address to assign. Press Enter to use DHCP (e.g.: 172.16.8.254): "
  28.     read vmip
  29.  
  30.     if [[ -z "$vmip" ]]; then
  31.         return
  32.     fi
  33.  
  34.     validate_ip "$vmip" "read_ip"
  35. }
  36.  
  37. read_mask() {
  38.  
  39.     echo -n "Please enter the subnet mask. Press Enter for the default mask 255.255.192.0 (e.g.: 255.255.192.0): "
  40.     read vmsubnetmask
  41.  
  42.     if [[ -z "$vmsubnetmask" ]]; then
  43.         vmsubnetmask="255.255.192.0"
  44.     fi
  45.  
  46.     validate_ip "$vmsubnetmask" "read_mask"
  47.  
  48. }
  49.  
  50. read_gw() {
  51.     echo -n "Please enter the default gateway. Press Enter for the default default gateway 172.16.8.1 (e.g.: 172.16.8.1): "
  52.     read vmgw
  53.  
  54.     if [[ -z "$vmgw" ]]; then
  55.         vmgw="172.16.8.1"
  56.     fi
  57.  
  58.     validate_ip "$vmgw" "read_gw"
  59.  
  60. }
  61.  
  62. read_dns() {
  63.     echo -n "Please enter DNS servers separated by commas. Press Enter for the default DNS servers. (e.g.: 172.16.8.59,172.16.8.60): "
  64.     read vmdns
  65.  
  66.     if [[ -z "$vmdns" ]]; then
  67.         vmdns="172.16.8.59,172.16.8.60"
  68.     fi
  69. }
  70.  
  71. validate_ip() {
  72.     workingip=$1
  73.     caller=$2
  74.     char="."
  75.     numperiods=$(echo "${workingip}" | awk -F"${char}" '{print NF-1}')
  76.  
  77.     if [[ "$numperiods" -gt 3 ]]; then
  78.         ((numperiods = numperiods + 1))
  79.         echo -e "An IP address or mask contains exactly four octets. You entered $numperiods. Please enter a valid IP."
  80.         $caller
  81.     fi
  82.  
  83.     if [[ "${#workingip}" -lt 7 ]]; then
  84.         echo -e "This IP or mask is invalid because it is too short or contains too few octets. Please enter a valid IP."
  85.         $caller
  86.     fi
  87.  
  88.     if [[ "${#workingip}" -gt 15 ]]; then
  89.         echo -e "This IP or mask is invalid because it is too long or contains too many octets. Please enter a valid IP."
  90.         $caller
  91.     fi
  92.  
  93.     re='^([1-9].*).([0-9].*).([0-9].*).([0-9]+)$'
  94.     if ! [[ "$workingip" =~ $re ]]; then
  95.         echo -e "Invalid IP address or mask. Please enter a valid IP."
  96.         $caller
  97.     fi
  98.  
  99.     if [[ "$workingip" =~ $re ]]; then
  100.         if [[ "${BASH_REMATCH[2]}" -ge 256 || ${BASH_REMATCH[3]} -ge 256 || ${BASH_REMATCH[4]} -ge 256 || ${BASH_REMATCH[5]} -ge 256 ]]; then
  101.             echo -e "Invalid IP address or mask. Octets cannot be greater than 255. Please enter a valid IP."
  102.             $caller
  103.         fi
  104.     fi
  105.  
  106.     if [[ "$caller" == "read_ip" ]]; then
  107.  
  108.         if [[ "$workingip" == "172.16.8.1" || "$workingip" == "172.16.8.2" || "$workingip" == "172.16.8.3" ]]; then
  109.             echo -e "You cannot use the ip $workingip. It is assigned to a downstream switch or router."
  110.             $caller
  111.         fi
  112.     fi
  113.  
  114.     if [[ "$caller" == "read_mask" ]]; then
  115.         for key in ${!masks[@]}; do
  116.             if [[ "${key}" == "$vmsubnetmask" ]]; then
  117.                 cidr="${masks[${key}]}"
  118.             fi
  119.         done
  120.         if [[ -z "$cidr" ]]; then
  121.             echo -e "$workingip is an invalid subnetmask. Please enter a valid subnet mask."
  122.             $caller
  123.         fi
  124.     fi
  125.  
  126.     return 0
  127. }
  128.  
  129. read_hostname
  130. read_ip
  131. if [[ -z "$vmip" ]]; then
  132.     hostname "$vmhostname"
  133.     echo "$vmhostname" >/etc/hostname
  134.     echo "network  --bootproto=dhcp --hostname=$vmhostname" >/tmp/network.ks
  135. else
  136.  
  137.     read_mask
  138.     for key in ${!masks[@]}; do
  139.         if [[ "${key}" == "$vmsubnetmask" ]]; then
  140.             cidr="${masks[${key}]}"
  141.         fi
  142.     done
  143.     read_gw
  144.     read_dns
  145.     hostname "$vmhostname"
  146.     echo "$vmhostname" >/etc/hostname
  147.     echo "network  --bootproto=static --device=ens192 --gateway=$vmgw --ip=$vmip --nameserver=$vmdns --netmask=$vmsubnetmask --ipv6=auto --activate --hostname=$vmhostname" >/tmp/network.ks
  148. fi
  149.  
captcha