Tuesday, October 17, 2017

Set Static IP address In Ubuntu Machine.

Just Copy and paste below mentioned content to the file and assign executable permission to that file. and Execute file.

#!/bin/bash

getinfo()
{
  read -p "Enter IP Address:" staticip
  read -p "Enter netmask:" netmask
  read -p "Enter Default Gateway:" gateway
  read -p "Enter nameserver:" nameserver
}

writeinterfacefile()

cat << EOF > $1 
# This file describes the network interfaces available on your system
# and how to activate them.
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0 
iface eth0 inet static
address $staticip
netmask $netmask
gateway $gateway
nameserver $nameserver 
EOF
#don't use any space before of after 'EOF' in the previous line

  echo ""
  echo "Given information saved in '$1' file."
  echo ""
  echo "Now need to restart system to effect new changes"
  echo ""
  exit 0
}

file="/etc/network/interfaces"
if [ ! -f $file ]; then
  echo ""
  echo "The file '$file' doesn't exist!"
  echo ""
  exit 1
fi

clear
echo "Set up a static IP address"
echo ""

getinfo
echo ""
echo "Given Information is:"
echo "IP address is:  $staticip"
echo "Net Mask is:  $netmask"
echo "Default Gateway:   $gateway"
echo "IP Address is:   $nameserver"
echo ""

while true; do
  read -p "Are these informations correct? [y/n]: " yn 
  case $yn in
    [Yy]* ) writeinterfacefile $file;;
    [Nn]* ) getinfo;;
        * ) echo "Pleas enter y or n!";;
  esac
done

No comments:

Post a Comment