netrefresh
#!/usr/bin/env bash
# This script is used to refresh the network connection on a Linux system
__netrefresh() {
refreshed=0
if [ -z "$1" ]
then
echo "Usage: netrefresh <interface>"
return 1
fi
interface="$1"
# ======================================================================= #
# dhclient #
# ======================================================================= #
if command -v dhclient &> /dev/null
then
echo "Found dhclient. Refreshing network..."
sudo dhclient -r
sudo dhclient
refreshed=1
fi
# ======================================================================= #
# networkctl #
# ======================================================================= #
if command -v networkctl &> /dev/null
then
echo "Found networkctl. Refreshing network..."
sudo networkctl renew "$interface"
refreshed=1
fi
# ======================================================================= #
# systemctl #
# ======================================================================= #
if command -v systemctl &> /dev/null
then
echo "Found systemctl. Refreshing network..."
sudo systemctl restart NetworkManager
refreshed=1
fi
# ======================================================================= #
# ifconfig #
# ======================================================================= #
if command -v ifconfig &> /dev/null
then
echo "Found ifconfig. Refreshing network..."
sudo ifconfig "$interface" down
sudo ifconfig "$interface" up
refreshed=1
fi
# ======================================================================= #
# ifdown #
# ======================================================================= #
if command -v ifdown &> /dev/null || command -v ifup &> /dev/null
then
echo "Found ifdown and ifup. Refreshing network..."
sudo ifdown "$interface" && sudo ifup "$interface"
refreshed=1
fi
# ======================================================================= #
# Unsuccessful refresh #
# ======================================================================= #
if [ "$refreshed" -eq 0 ]
then
echo "Could not refresh network. Please check the network configuration."
fi
}
__netrefresh "$@"