#!/bin/bash
deviceInfo=$(lshw -C network)
partialName=${deviceInfo#*logical name: }
networkDevice=${partialName% version*}
deviceName=`echo "${networkDevice/$'\n'}" | sed 's/ *$//g'`

ADDRESS=$(ifconfig $deviceName | grep "inet " | awk '{print $2}')
NETMASK=$(ifconfig $deviceName | grep "inet " | awk '{print $4}')
ROUTER=$(ip r | grep -n default | head -n 1 | awk '{print $3}')

infoDNS=$(resolvectl status $deviceName | grep "DNS Servers: ")
DNS=${infoDNS#*DNS Servers: }

echo "iface lo inet loopback" >> /etc/network/interfaces
echo "auto lo" >> /etc/network/interfaces
echo "allow-hotplug $deviceName" >> /etc/network/interfaces
echo "auto $deviceName" >> /etc/network/interfaces
echo "iface $deviceName inet static" >> /etc/network/interfaces
echo "    address $ADDRESS" >> /etc/network/interfaces
echo "    netmask $NETMASK" >> /etc/network/interfaces
echo "    gateway $ROUTER" >> /etc/network/interfaces
echo "    dns-nameservers $DNS" >> /etc/network/interfaces

sed -i "s/#DNS=/DNS=$DNS/"  /etc/systemd/resolved.conf


ifdown --force $deviceName lo && ifup -a		
systemctl unmask networking
systemctl enable networking
systemctl restart networking

systemctl restart systemd-resolved