Skip to content

Instantly share code, notes, and snippets.

@larrasket
Created February 24, 2023 12:58
Show Gist options
  • Save larrasket/619df25883923979c2581629d3cb93bf to your computer and use it in GitHub Desktop.
Save larrasket/619df25883923979c2581629d3cb93bf to your computer and use it in GitHub Desktop.
Toggle internet connection but keep network
#!/bin/bash
# Check if the internet connection is currently active
ping -q -c 1 -W 1 google.com > /dev/null
if [ $? -eq 0 ]
then
# If the internet connection is active, turn it off by removing the default gateway
echo "Turning off internet connection"
sudo ip route del default
else
# If the internet connection is not active, turn it on by adding the default gateway
echo "Turning on internet connection"
sudo ip route add default via <gateway_ip> # replace <gateway_ip> with your default gateway IP address
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment