constantin-net

netmenu_static_onoff

Sep 17th, 2020 (edited)
1,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.77 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. [ -z "$1" -o -z "$2" ] && echo "iface action" && exit 0
  4.  
  5. if [[ "$2" == "up" ]]; then
  6.   sed -i -r "s/iface $1.*$/iface $1 inet static/" /etc/network/interfaces
  7.   sed -i "/iface $1/!b;:x;n;s/^#\+//;/^$/b;bx" /etc/network/interfaces
  8.   echo -n "Enter new address: "
  9.   read new_address
  10.   echo -n "Enter new netmask: "
  11.   read new_netmask
  12.   echo -n "Enter new gateway: "
  13.   read new_gateway
  14.   echo -n "Enter new DNS1: "
  15.   read dns1
  16.   echo -n "Enter new DNS2: "
  17.   read dns2
  18.   if [[ $new_address ]]; then
  19.     sed -i -r "/iface $1/!b;:x;n;s/address.*/address $new_address/;t;/iface/b;bx" /etc/network/interfaces
  20.   fi
  21.   if [[ $new_netmask ]]; then
  22.     sed -i -r "/iface $1/!b;:x;n;s/netmask.*/netmask $new_netmask/;t;/iface/b;bx" /etc/network/interfaces
  23.   fi
  24.   if [[ $new_gateway ]]; then
  25.   sed -i -r "/iface $1/!b;:x;n;s/gateway.*/gateway $new_gateway/;t;/iface/b;bx" /etc/network/interfaces
  26.   fi
  27.   #sed -i "/$2 $3/s/^#\+//" ~/interfaces
  28.   #temporary parm
  29.   if [[ -z `cat /etc/network/interfaces | grep $1` && $new_address && $new_netmask && $new_gateway ]]; then
  30.     ifdown $1
  31.     ip addr add $new_address/$new_netmask dev $1 label $1
  32.     ip route add default via $new_gateway  dev $1 onlink
  33.   else
  34.     ifdown $1
  35.     ifup $1
  36.   fi
  37.   #temporary dns
  38.   if [ "$dns1" ]; then
  39.     echo -e "nameserver $dns1" > /etc/resolv.conf
  40.     if [ "$dns2" ]; then
  41.       echo -e "nameserver $dns2" >> /etc/resolv.conf
  42.     fi
  43.   #else echo "" > /etc/resolv.conf
  44.   fi
  45. fi
  46. if [[ "$2" == "down" ]]; then
  47.   if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
  48.     ip route del default  dev $1 2>&1 1>/dev/null || true
  49.     ip -4 addr flush dev $1
  50.     ip link set dev $1 down
  51.     echo "" > /etc/resolve.conf
  52.   else
  53.     ifdown $1
  54.     #echo "" > /etc/resolve.conf
  55.   fi
  56. fi
  57. exit 0
  58.  
Add Comment
Please, Sign In to add comment