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" ] && echo "iface" && exit 0
- interfaces="/etc/network/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")
- 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
- }
- echo "Static configuration $1"
- 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
- dhclient -r $1
- 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
- fi
- #temporary dns
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement