Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #https://www.opennet.ru/base/sys/sed_edit_interfaces.txt.html
- [ -z "$1" -o -z "$2" ] && echo "iface action" && exit 0
- interfaces="/etc/network/interfaces"
- function ip_prompt() {
- while true; do
- read -p "$1: " new_par
- if [ -z `echo $new_par` ]; then
- new_par="$1"
- break
- 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
- echo "xxx.xxx.xxx.xxx"
- :
- else
- break
- fi
- done
- }
- dhclient $1 -r
- if [[ "$2" == "d" ]]; then
- ip -4 addr flush dev $1
- if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
- ip route del default dev $1 2>&1 1>/dev/null || true
- ip link set dev $1 down
- echo "" > /etc/resolve.conf
- else
- sed -i -r "s/iface $1.*$/iface $1 inet dhcp/" "$interfaces"
- sed -i -r "/iface $1/!b;:x;n;s/^./#\0/;/^$/b;bx" "$interfaces"
- ifdown $1
- #echo "" > /etc/resolve.conf
- fi
- dhclient $1
- fi
- if [[ "$2" == "s" ]]; then
- echo "Static configuration $1"
- sed -i -r "s/iface $1.*$/iface $1 inet static/" "$interfaces"
- sed -i "/iface $1/!b;:x;n;s/^#\+//;/^$/b;bx" "$interfaces"
- old_address=$(sed -n -r "/iface $1/!b;:x;n;/^.*address/s/^.*address([ ])*//p;/iface/b;bx" "$interfaces")
- old_netmask=$(sed -n -r "/iface $1/!b;:x;n;/^.*netmask/s/^.*netmask([ ])*//p;/iface/b;bx" "$interfaces")
- old_gateway=$(sed -n -r "/iface $1/!b;:x;n;/^.*gateway/s/^.*gateway([ ])*//p;/iface/b;bx" "$interfaces")
- echo -n "Enter new address: "
- ip_prompt $old_address
- new_address=$new_par
- clear
- echo "Address - $new_address"
- echo -n "Enter new netmask: "
- ip_prompt $old_netmask
- new_netmask=$new_par
- clear
- echo "Address - $new_address"
- echo "Netmask - $new_netmask"
- echo -n "Enter new gateway: "
- ip_prompt $old_gateway
- new_gateway=$new_par
- clear
- echo "Address - $new_address"
- echo "Netmask - $new_netmask"
- echo "Gateway - $new_gateway"
- echo -n "Enter new DNS1: "
- read dns1
- echo -n "Enter new DNS2: "
- read dns2
- clear
- echo "Address - $new_address"
- echo "Netmask - $new_netmask"
- echo "Gateway - $new_gateway"
- echo "DNS - $dns1 $dns2"
- read -p "Apply changes? [y/n]" apply
- if [ "$apply" ] && [ "$apply" == "y" ]; then
- if [[ -z `cat $interfaces | grep $1` ]]; then
- ip link set dev $1 up
- ip addr add $new_address/$new_netmask dev $1 label $1
- ip route add default via $new_gateway dev $1 onlink
- else
- sed -i -r "/iface $1/!b;:x;n;s/address.*/address $new_address/;t;/iface/b;bx" $interfaces
- sed -i -r "/iface $1/!b;:x;n;s/netmask.*/netmask $new_netmask/;t;/iface/b;bx" $interfaces
- sed -i -r "/iface $1/!b;:x;n;s/gateway.*/gateway $new_gateway/;t;/iface/b;bx" $interfaces
- ifdown $1
- ifup $1
- fi
- if [ "$dns1" ]; then
- echo -e "nameserver $dns1" > /etc/resolv.conf
- if [ "$dns2" ]; then
- echo -e "nameserver $dns2" >> /etc/resolv.conf
- fi
- else echo "" > /etc/resolv.conf
- fi
- fi
- fi
- if [[ "$2" == "m" ]]; then
- ip -4 addr flush dev $1
- echo "" > /etc/resolve.conf
- if [[ -z `cat /etc/network/interfaces | grep $1` ]]; then
- ip route del default dev $1 2>&1 1>/dev/null || true
- else
- sed -i -r "s/iface $1.*$/iface $1 inet manual/" "$interfaces"
- sed -i -r "/iface $1/!b;:x;n;s/^./#\0/;/^$/b;bx" "$interfaces"
- ifdown $1
- fi
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement