Shell script to set IP address in SUSE OS

Version 1

    #!/bin/bash

     

    acceptInput ()

    {

    echo -n "Enter IP address \[`ifconfig | egrep "inet addr" | grep Bcast | awk '{ print $2 }' | awk -F ":" '{ print $2 }'`]: "

    read ip

    if \[ "$ip" == "" ]

    then

    ip=`ifconfig | egrep "inet addr" | grep Bcast | awk '{ print $2 }' | awk -F ":" '{ print $2 }'`

    fi

     

    echo -n "Enter netmask \[`ifconfig | egrep "inet addr" | grep Bcast | awk '{ print $4 }' | awk -F ":" '{ print $2 }'`]: "

    read netmask

    if \[ "$netmask" == "" ]

    then

    netmask=`ifconfig | egrep "inet addr" | grep Bcast | awk '{ print $4 }' | awk -F ":" '{ print $2 }'`

    fi

     

    echo -n "Enter gateway \[`netstat -rn | awk '{ print $2 }' | tac | head -1`]: "

    read gateway

    if \[ "$gateway" == "" ]

    then

    gateway=`netstat -rn | awk '{ print $2 }' | tac | head -1`

    fi

     

    echo -n "Enter Domain name \[`cat /etc/resolv.conf | grep "search " | awk '{ print $2 }'`]: "

    read domainname

    if \[ "$domainname" == "" ]

    then

    domainname=`cat /etc/resolv.conf | grep "search " | awk '{ print $2 }'`

    fi

     

    echo -n "Enter Domain IP \[`cat /etc/resolv.conf | grep "nameserver " | awk '{ print $2 }'`]: "

    read domainip

    if \[ "$domainip" == "" ]

    then

    domainip=`cat /etc/resolv.conf | grep "nameserver " | awk '{ print $2 }'`

    fi

     

    echo -n "Enter the Fully qualified hostname \[`cat /etc/hosts | grep 127.0.0.1 | awk '{ print $2 }'`]: "

    read hostname

    if \[ "$hostname" == "" ]

    then

    hostname=`cat /etc/hosts | grep 127.0.0.1 | awk '{ print $2 }'`

    fi

     

    echo -n "Enter the hostname alias \[`cat /etc/hosts | grep 127.0.0.1 | awk '{ print $3 }'`]: "

    read hostnameAlias

    if \[ "$hostnameAlias" == "" ]

    then

    hostnameAlias=`cat /etc/hosts | grep 127.0.0.1 | awk '{ print $3 }'`

    fi

     

    echo ""

    echo "You have entered following details"

    echo IP = $ip

    echo NETMASK = $netmask

    echo GATEWAY = $gateway

    echo DOMAINNAME = $domainname

    echo DOMAINIP = $domainip

    echo HOSTNAME = $hostname

    echo HOSTNAME ALIAS = $hostnameAlias

    echo ""

    echo -n "Is that right? \[Y/N] : "

    read ans

    }

     

    setIpHostname ()

    {

    \# set hosts file

    echo "127.0.0.1 $hostname $hostnameAlias localhost" > /etc/hosts

     

    \# set resolve.conf

    echo "search $domainname" > /etc/resolv.conf

    echo "nameserver $domainip" >> /etc/resolv.conf

     

    \# set network file

    echo "NETWORKING=yes" > /etc/sysconfig/network

    echo "NETWORKING_IPV6=yes" >> /etc/sysconfig/network

    echo "HOSTNAME=$hostname" >> /etc/sysconfig/network

    echo "DOMAINNAME=$domainname" >> /etc/sysconfig/network

     

    \# set ifcfg-eth0

    echo "DEVICE=eth0" > /etc/sysconfig/network-scripts/ifcfg-eth0

    echo "BOOTPROTO=static" >> /etc/sysconfig/network-scripts/ifcfg-eth0

    echo "NETMASK=$netmask" >> /etc/sysconfig/network-scripts/ifcfg-eth0

    echo "DHCP_HOSTNAME=$hostname" >> /etc/sysconfig/network-scripts/ifcfg-eth0

    echo "IPADDR=$ip" >> /etc/sysconfig/network-scripts/ifcfg-eth0

    echo "GATEWAY=$gateway" >> /etc/sysconfig/network-scripts/ifcfg-eth0

    echo "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-eth0

    echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0

    echo "USERCTL=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0

    echo "IPV6INIT=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0

    echo "PEERDNS=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0

     

    \# set in Studio's vami

    #cat /opt/vmware/share/vami/vami_set_hostname | grep "HOSTNAME=\"localhost.localdom\"" | sed -e 's/localhost.localdom/$hostname/g'

    }

     

    setHotnameInVami ()

    {

    lineCount=`sed -n '$=' /opt/vmware/share/vami/vami_set_hostname` > /tmp/sam.txt

     

    for (( i = 1; i <= $lineCount; i++ ))

    do

    if \[ $i -eq 41 ]

    then

    echo " HOSTNAME=$hostname" >> /tmp/sam.txt

    else

    sed -n $i'p' /opt/vmware/share/vami/vami_set_hostname >> /tmp/sam.txt

    fi

    done

    mv /tmp/sam.txt /opt/vmware/share/vami/vami_set_hostname

    }

     

    main ()

    {

    clear

    echo ""

    echo "This script is used to set static hostname and IP address, press enter if you want to set default value"

    echo ""

     

    acceptInput

     

    while \[ "$ans" == "N" -o "$ans" == "n" ]

    do

    clear

    echo ""

    echo "This script is used to set static hostname and IP address"

    echo ""

    acceptInput

    done

     

    if \[ "$ans" == "Y" -o "$ans" == "y" ]

    then

    setIpHostname

    \# This VA so set it for every boot

    cp /etc/hosts /root/

    cp /etc/resolv.conf /root/

    cp /etc/sysconfig/network /root/

    cp /etc/sysconfig/network-scripts/ifcfg-eth0 /root/

     

    {

    echo "## Following line is added by ISV"

    echo "cp /root/hosts /etc/hosts"

    echo "cp /root/resolv.conf /etc/resolv.conf"

    echo "cp /root/network /etc/sysconfig/network"

    echo "cp /root/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0"

    } >> /etc/rc.local

     

    \# set in vami

     

    setHotnameInVami

     

    echo "OK DONE ..."

    fi

    }

     

    \# Start of the program

    main