Advertisement
constantin-net

netmenu_setup_iface

Sep 17th, 2020 (edited)
1,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.63 KB | None | 0 0
  1. #!/bin/bash
  2. #https://www.opennet.ru/base/sys/sed_edit_interfaces.txt.html
  3.  
  4. [ -z "$1" -o -z "$2" ] && echo "iface action" && exit 0
  5.  
  6. interfaces="/etc/network/interfaces"
  7.  
  8. function ip_prompt() {
  9. while true; do
  10.     read -p "$1: " new_par
  11.     if [ -z `echo $new_par` ]; then
  12.         new_par="$1"
  13.         break
  14.     elif [ -z `echo $new_par | sed -n -r '/((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/p'` ]; then
  15.         echo "xxx.xxx.xxx.xxx"
  16.         :
  17.     else
  18.         break
  19.     fi
  20. done
  21. }
  22.  
  23. dhclient $1 -r
  24.  
  25. if [[ "$2" == "d" ]]; then
  26.     ip -4 addr flush dev $1
  27.     if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
  28.         ip route del default  dev $1 2>&1 1>/dev/null || true
  29.         ip link set dev $1 down
  30.         echo "" > /etc/resolve.conf
  31.     else
  32.         sed -i -r  "s/iface $1.*$/iface $1 inet dhcp/" "$interfaces"
  33.         sed -i -r  "/iface $1/!b;:x;n;s/^./#\0/;/^$/b;bx" "$interfaces"
  34.         ifdown $1
  35.         #echo "" > /etc/resolve.conf
  36.     fi
  37.     dhclient $1
  38. fi
  39.  
  40. if [[ "$2" == "s" ]]; then
  41.     echo "Static configuration $1"
  42.     sed -i -r "s/iface $1.*$/iface $1 inet static/" "$interfaces"
  43.     sed -i "/iface $1/!b;:x;n;s/^#\+//;/^$/b;bx" "$interfaces"
  44.     old_address=$(sed -n -r "/iface $1/!b;:x;n;/^.*address/s/^.*address([ ])*//p;/iface/b;bx" "$interfaces")
  45.     old_netmask=$(sed -n -r "/iface $1/!b;:x;n;/^.*netmask/s/^.*netmask([ ])*//p;/iface/b;bx" "$interfaces")
  46.     old_gateway=$(sed -n -r "/iface $1/!b;:x;n;/^.*gateway/s/^.*gateway([ ])*//p;/iface/b;bx" "$interfaces")
  47.  
  48.     echo -n "Enter new address: "
  49.     ip_prompt $old_address
  50.     new_address=$new_par
  51.     clear
  52.     echo "Address - $new_address"
  53.     echo -n "Enter new netmask: "
  54.     ip_prompt $old_netmask
  55.     new_netmask=$new_par
  56.     clear
  57.     echo "Address - $new_address"
  58.     echo "Netmask - $new_netmask"
  59.     echo -n "Enter new gateway: "
  60.     ip_prompt $old_gateway
  61.     new_gateway=$new_par
  62.     clear
  63.     echo "Address - $new_address"
  64.     echo "Netmask - $new_netmask"
  65.     echo "Gateway - $new_gateway"
  66.  
  67.     echo -n "Enter new DNS1: "
  68.     read dns1
  69.     echo -n "Enter new DNS2: "
  70.     read dns2
  71.  
  72.     clear
  73.     echo "Address - $new_address"
  74.     echo "Netmask - $new_netmask"
  75.     echo "Gateway - $new_gateway"
  76.     echo "DNS - $dns1 $dns2"
  77.  
  78.     read -p "Apply changes? [y/n]" apply
  79.     if [ "$apply" ] && [ "$apply" == "y" ]; then
  80.         if [[ -z `cat $interfaces | grep $1` ]]; then
  81.             ip link set dev $1 up
  82.             ip addr add $new_address/$new_netmask dev $1 label $1
  83.             ip route add default via $new_gateway  dev $1 onlink
  84.         else
  85.             sed -i -r "/iface $1/!b;:x;n;s/address.*/address $new_address/;t;/iface/b;bx" $interfaces
  86.             sed -i -r "/iface $1/!b;:x;n;s/netmask.*/netmask $new_netmask/;t;/iface/b;bx" $interfaces
  87.             sed -i -r "/iface $1/!b;:x;n;s/gateway.*/gateway $new_gateway/;t;/iface/b;bx" $interfaces
  88.             ifdown $1
  89.             ifup $1
  90.         fi
  91.         if [ "$dns1" ]; then
  92.             echo -e "nameserver $dns1" > /etc/resolv.conf
  93.             if [ "$dns2" ]; then
  94.                  echo -e "nameserver $dns2" >> /etc/resolv.conf
  95.             fi
  96.         else echo "" > /etc/resolv.conf
  97.         fi
  98.     fi
  99. fi
  100.  
  101. if [[ "$2" == "m" ]]; then
  102.     ip -4 addr flush dev $1
  103.     echo "" > /etc/resolve.conf
  104.     if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
  105.         ip route del default  dev $1 2>&1 1>/dev/null || true
  106.     else
  107.         sed -i -r  "s/iface $1.*$/iface $1 inet manual/" "$interfaces"
  108.         sed -i -r  "/iface $1/!b;:x;n;s/^./#\0/;/^$/b;bx" "$interfaces"
  109.         ifdown $1
  110.     fi
  111. fi
  112.  
  113. exit 0
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement