Skip to content

Instantly share code, notes, and snippets.

@maestrofx
Created December 11, 2015 08:35
Show Gist options
  • Save maestrofx/2838a6d3918cf651fbc5 to your computer and use it in GitHub Desktop.
Save maestrofx/2838a6d3918cf651fbc5 to your computer and use it in GitHub Desktop.
trusty static ip
#!/bin/bash
# This is a bash script to set a static IP.
# Tested in Ubuntu 14.04, added more
readInfo() {
read -p "Type static IP (eg - 10.10.10.2) : " staticip
read -p "Type Default Gateway (eg - 10.10.10.1) : " gateway
read -p "Type Netmask (eg - 255.255.255.0) : " netmask
read -p "Type DNS (eg - 8.8.8.8) : " dns
}
writeToIntf() {
cat << EOF > $1
# This file describes the network interfaces available on your system.
# The loopback network interface
##auto lo
##iface lo inet loopback
# The primary network interface
auto eth0
##iface eth0 inet dhcp
#Your static network configuration
iface eth0 inet static
address $staticip
netmask $netmask
gateway $gateway
dns-nameservers $dns
EOF
#don't use any space before of after 'EOF' in the previous line
echo ""
echo "Your configuration was saved in '$1'"
echo ""
echo "Restarting interfaces..."
restartNW
exit 0
}
restartNW() {
stopNW=`sudo ifdown eth0`
startNW=`sudo ifup eth0`
}
#Add your path here
file="/etc/network/interfaces"
if [ ! -f $file ]; then
echo ""
echo "The file '$file' does not exist!"
echo ""
exit 1
fi
clear
IP=`hostname -I`
DG=`route | grep "default" | awk '{print $2}'`
echo "Your current IP: $IP"
echo "Your current DG: $DG"
echo ""
echo "Set up a static IP address"
echo ""
readInfo
echo ""
echo "Your settings are"
echo "Static IP : $staticip"
echo "Default Gateway : $gateway"
echo "Subnetmask : $netmask"
echo "DNS : $dns"
echo ""
while true;
do
read -p "Is this configuration correct? [y/n]: " yn
case $yn in
[Yy]* ) writeToIntf $file;;
[Nn]* ) readInfo;;
* ) echo "Enter y or n!";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment