Magic script for post install interface configuration

Generally on a network we can’t always trust dhcp or rtadvd to be there for servers.

So here is a magic script that will generate an ifcfg based on these parameters when the server first runs. It helps if you register off the mac to a dhcp entry too.

DEV=$(ip route | grep ^default | sed 's/^.* dev //;s/ .*$//'|head -1)
if [ -n "$DEV" ]
then
     IP_AND_PREFIX_LEN=$(ip -f inet addr show dev $DEV | grep 'inet '| head -1 | sed 's/^ *inet *//;s/ .*$//')
     IP=$(echo ${IP_AND_PREFIX_LEN} | cut -f1 -d'/')
     MASK=$(ipcalc -m ${IP_AND_PREFIX_LEN} | sed 's/^.*=//')
     GW=$(ip route | grep default | head -1 | sed 's/^.*via //;s/ .*$//')
     IP6_PREFIX=$(ip -f inet6 addr show dev $DEV | grep 'inet6 '| head -1 | sed 's/^ *inet6 *//;s/ .*$//')
     IP6=$(echo ${IP6_PREFIX} | cut -f1 -d'/')
     MASK6=$(echo ${IP6_PREFIX} | cut -f2 -d'/')
     GW6=$(ip -6 route | grep default | head -1 | sed 's/^.*via //;s/ .*$//')
     MAC=$(ip link show dev ${DEV} | grep 'link/ether '| head -1 | sed 's/^ *link\/ether *//;s/ .*$//')

cat > /etc/sysconfig/network-scripts/ifcfg-${DEV} << DEVEOF
# Generated by magic
DEVICE=${DEV}
ONBOOT=yes
NETBOOT=no
BOOTPROTO=static
TYPE=Ethernet
NAME=${DEV}
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6_FAILURE_FATAL=yes
IPV6INIT=yes
PEERDNS=no
PEERROUTES=no
IPV6_AUTOCONF=no
HWADDR=${MAC}
IPADDR=${IP}
GATEWAY=${GW}
NETMASK=${MASK}
IPV6ADDR=${IP6}
#PREFIX=${MASK6}
IPV6_DEFAULTGW=${GW6}
DNS1=PUT YOUR DNS SERVER IP HERE
DNS2=PUT YOUR DNS SERVER IP HERE
#NM_CONTROLLED=no

DEVEOF

    #Done
fi