Advertisement
constantin-net

netmenu_new_conf

Sep 18th, 2020 (edited)
1,584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.35 KB | None | 0 0
  1. #!/bin/bash
  2. #https://www.opennet.ru/base/sys/sed_edit_interfaces.txt.html
  3.  
  4. [ -z "$1" ] && echo "iface" && exit 0
  5.  
  6. interfaces="/etc/network/interfaces"
  7.  
  8. old_address=$(sed -n -r "/iface $1/!b;:x;n;/^.*address/s/^.*address([ ])*//p;/iface/b;bx" "$interfaces")
  9. old_netmask=$(sed -n -r "/iface $1/!b;:x;n;/^.*netmask/s/^.*netmask([ ])*//p;/iface/b;bx" "$interfaces")
  10. old_gateway=$(sed -n -r "/iface $1/!b;:x;n;/^.*gateway/s/^.*gateway([ ])*//p;/iface/b;bx" "$interfaces")
  11.  
  12. function ip_prompt() {
  13. while true; do
  14.     read -p "$1: " new_par
  15.     if [ -z `echo $new_par` ]; then
  16.         new_par="$1"
  17.         break
  18.     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
  19.         echo "xxx.xxx.xxx.xxx"
  20.         :
  21.     else
  22.         break
  23.     fi
  24. done
  25. }
  26.  
  27. echo "Static configuration $1"
  28. echo -n "Enter new address: "
  29. ip_prompt $old_address
  30. new_address=$new_par
  31. clear
  32. echo "Address - $new_address"
  33. echo -n "Enter new netmask: "
  34. ip_prompt $old_netmask
  35. new_netmask=$new_par
  36. clear
  37. echo "Address - $new_address"
  38. echo "Netmask - $new_netmask"
  39. echo -n "Enter new gateway: "
  40. ip_prompt $old_gateway
  41. new_gateway=$new_par
  42. clear
  43. echo "Address - $new_address"
  44. echo "Netmask - $new_netmask"
  45. echo "Gateway - $new_gateway"
  46.  
  47. echo -n "Enter new DNS1: "
  48. read dns1
  49. echo -n "Enter new DNS2: "
  50. read dns2
  51.  
  52. clear
  53. echo "Address - $new_address"
  54. echo "Netmask - $new_netmask"
  55. echo "Gateway - $new_gateway"
  56. echo "DNS - $dns1 $dns2"
  57.  
  58. read -p "Apply changes? [y/n]" apply
  59. if [ "$apply" ] && [ "$apply" == "y" ]; then
  60.     if [[ -z `cat $interfaces | grep $1` ]]; then
  61.         dhclient -r $1
  62.         ip link set dev $1 up
  63.         ip addr add $new_address/$new_netmask dev $1 label $1
  64.         ip route add default via $new_gateway  dev $1 onlink
  65.     else
  66.         sed -i -r "/iface $1/!b;:x;n;s/address.*/address $new_address/;t;/iface/b;bx" $interfaces
  67.         sed -i -r "/iface $1/!b;:x;n;s/netmask.*/netmask $new_netmask/;t;/iface/b;bx" $interfaces
  68.         sed -i -r "/iface $1/!b;:x;n;s/gateway.*/gateway $new_gateway/;t;/iface/b;bx" $interfaces
  69.     fi
  70.     #temporary dns
  71.     if [ "$dns1" ]; then
  72.         echo -e "nameserver $dns1" > /etc/resolv.conf
  73.         if [ "$dns2" ]; then
  74.             echo -e "nameserver $dns2" >> /etc/resolv.conf
  75.         fi
  76.         else echo "" > /etc/resolv.conf
  77.     fi
  78. fi
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement