Skip to content

Instantly share code, notes, and snippets.

@briantissue
Created May 12, 2017 14:43
Show Gist options
  • Save briantissue/ed39e176c45fa71ad1885fe41155798f to your computer and use it in GitHub Desktop.
Save briantissue/ed39e176c45fa71ad1885fe41155798f to your computer and use it in GitHub Desktop.
Linux Container Static IP address Assignment
#!/bin/bash
# Script by Brian Tissue
# Used to assign static IP addresses to LXC containers
LXCPATH='/var/lib/lxc'
if [ "$(whoami)" != "root" ]; then
echo "Not running as root. I told you that you needed to be root, come on man..."
echo "Please use su -s then run this script again as root."
exit 0
else
echo "Running as root. Good"
fi
echo "View Existing Configured Containers? [Y,n]"
read input
if [[ $input == "Y" || $input == "y" ]]; then
ls -latrh "$LXCPATH"
else
echo "Ok, I guess you don't want to check yourself...."
fi
echo "View this hosts current IP address scheme? [Y,n]"
read input
if [[ $input == "Y" || $input == "y" ]]; then
ifconfig
echo "This Host's Current Default Gateway:"
ip route ls | grep -i default
else
echo "Ok, I guess you don't want to check yourself...."
fi
echo "-----------------------------------------------------/"
echo
echo "Okay, let's begin to configure the static IP addresses."
echo
echo "-----------------------------------------------------/"
read -p "Enter the container name: " cnname
read -p "Enter Static IP address: " cnipaddress
read -p "Enter Static Subnet Mask: " cnsubnetmask
read -p "Enter Default Gateway: " cndefaultgateway
read -p "Enter DNS Name Server: " cnnameserver
echo "-----------------------------------------------------/"
echo
echo "Let's look the current configuration cntrl+c if you need to quit"
echo
echo "-----------------------------------------------------/"
cat "$LXCPATH"/$cnname/rootfs/etc/network/interfaces
echo "Creating a backup of the original interface configuration for $cnname"
sleep 5
if [ -f "$LXCPATH"/$cnname/rootfs/etc/network/interfaces/*.bak ]
then
echo "Removing current bakup file of interfaces"
rm -rf "$LXCPATH"/$cnname/rootfs/etc/network/interfaces/*.bak
else
echo "Creating Backup Now"
cp -r "$LXCPATH"/$cnname/rootfs/etc/network/interfaces "$LXCPATH"/$cnname/rootfs/etc/network/interfaces.bak
fi
echo "-----------------------------------------------------/"
echo
echo "Backup file is located at: $cnname/rootfs/etc/network"
echo
echo "-----------------------------------------------------/"
sleep 5
> "$LXCPATH"/$cnname/rootfs/etc/network/interfaces
echo "auto lo" >> "$LXCPATH"/$cnname/rootfs/etc/network/interfaces
echo "iface lo inet loopback" >> "$LXCPATH"/$cnname/rootfs/et c/network/interfaces
echo "auto eth0" >> "$LXCPATH"/$cnname/rootfs/etc/network/interfaces
echo "iface eth0 inet static" >> "$LXCPATH"/$cnname/rootfs/etc/network/interfaces
echo "address $cnipaddress" >> "$LXCPATH"/$cnname/rootfs/etc/network/interfaces
echo "netmask $cnsubnetmask" >> "$LXCPATH"/$cnname/rootfs/etc/network/interfaces
echo "gateway $cndefaultgateway" >> "$LXCPATH"/$cnname/rootfs/etc/network/interfaces
echo "dns-nameserver $cnnameserver" >> "$LXCPATH"/$cnname/rootfs/etc/network/interfaces
echo "View $cnname new IP address scheme? [Y,n]"
read input
if [[ $input == "Y" || $input == "y" ]]; then
cat "$LXCPATH"/$cnname/rootfs/etc/network/interfaces
else
echo "Ok, I guess you don't want to check yourself...."
fi
echo "Power on $cnname? [Y,n]"
read input
if [[ $input == "Y" || $input == "y" ]]; then
lxc-start -n $cnname
sleep 2
lxc-ls -f
else
echo "Exiting Script..."
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment