Skip to content

Instantly share code, notes, and snippets.

@blacklabssecurity
Last active September 2, 2018 23:39
Show Gist options
  • Save blacklabssecurity/ae9a117529389f70df6618f2706604a3 to your computer and use it in GitHub Desktop.
Save blacklabssecurity/ae9a117529389f70df6618f2706604a3 to your computer and use it in GitHub Desktop.
Used to change a Debian Linux system between static and dynamic addressing
#!/bin/bash
dhcp_func () {
echo "Let's configure your IPv4 DHCP configuration."
sed -i 's|static|dhcp|g' /etc/network/interfaces
if /etc/network/interfaces | grep "address"; then
sed -i 's|address.*|#address|g' /etc/network/interfaces
fi
if /etc/network/interfaces | grep "netmask"; then
sed -i 's|netmask.*|#netmask|g' /etc/network/interfaces
fi
if /etc/network/interfaces | grep "broadcast"; then
sed -i 's|broadcast.*|#broadcast|g' /etc/network/interfaces
fi
if /etc/network/interfaces | grep "gateway"; then
sed -i 's|gateway.*|#gateway|g' /etc/network/interfaces
fi
if /etc/network/interfaces | grep ".*nameserver"; then
sed -i 's|.*nameserver.*|#.*nameserver'
fi
if ask "Would you like to customize the DNS nameserver: " Y; then
echo "Enter the DNS nameserver to use (Ex: 8.8.8.8 for Goolge DNS): "
read dns
echo "nameserver $dns" > /etc/resolv.conf
fi
echo "Restarting the network service..." && /etc/init.d/networking restart >/dev/null
}
static_func () {
echo "Let's configure your IPv4 static configuration."
sed -i 's|dhcp|static|g' /etc/network/interfaces
echo "Enter the IP Address to set followed by [ENTER] : "
read ipadd
echo " address $ipadd" >> /etc/network/interfaces
echo "Enter the Subnet Mask to set followed by [ENTER] : "
read mask
echo " netmask $mask" >> /etc/network/interfaces
echo "Enter the Gateway Address to set followed by [ENTER] : "
read gway
echo " address $gway" >> /etc/network/interfaces
echo "Enter the Broadcast Address to set followed by [ENTER] : "
read bcast
echo " address $bcast" >> /etc/network/interfaces
echo "Enter the DNS nameserver to use (Ex: 8.8.8.8 for Goolge DNS): "
read dns
echo "nameserver $dns" > /etc/resolv.conf
echo "Restarting the network service..." && /etc/init.d/networking restart >/dev/null
}
echo " This can be either a static or dynmaic configuration..."
echo
echo " Let's test your Internet access now..."
sleep 1
echo
ping -c3 8.8.8.8 >/tmp/pingOutput
if grep "bytes from" /tmp/pingOutput >/dev/null && grep "eth.* inet static" /etc/network/interfaces >/dev/null; then
echo " You are connected to the Internet through a static configuration"
elif
grep "bytes from" /tmp/pingOutput >/dev/null || grep "eth.* inet dhcp" /etc/network/interfaces >/dev/null; then
echo " You are connected to the Internet through a DHCP configuration"
else
echo " You are not currently connected to the Internet"
fi
echo
if ask "Would you like to adjust your IPv4 Address configuration? " Y; then
while true; do
echo " 1. Configure a DHCP Address"
echo " 2. Configure a Static Address"
echo " 3. Leave as currently configured"
echo
echo
echo -n "Choice: "
read choice
# case use will create approach to ip configuration
case $choice in
1) dhcp_func && break;;
2) static_func && break ;;
3) break;;
*) echo " That is not a valid choice"
esac
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment