Skip to content

Instantly share code, notes, and snippets.

@sarkrui
Last active February 6, 2024 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarkrui/6aaaaeee84607af4e5ae6668d22f47dd to your computer and use it in GitHub Desktop.
Save sarkrui/6aaaaeee84607af4e5ae6668d22f47dd to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script to check connectivity and ensure VPN connection
# Perform a curl command to check connectivity to google.com
if curl -s --connect-timeout 2 google.com > /dev/null; then
echo "ok"
else
# If curl command fails, attempt to connect to VPN using GlobalProtect
globalprotect connect
fi
[Unit]
Description=Check connectivity and ensure VPN connection via GlobalProtect
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/autoconnect
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
[Unit]
Description=Timer to periodically check connectivity and ensure VPN connection
[Timer]
OnBootSec=0min
OnUnitActiveSec=30sec
Unit=autoconnect.service
[Install]
WantedBy=timers.target
#!/bin/bash
# Exit on any error
set -e
# Download the autoconnect script and set executable permissions
curl -o /usr/local/bin/autoconnect "https://gist.githubusercontent.com/sarkrui/6aaaaeee84607af4e5ae6668d22f47dd/raw/autoconnect"
chmod +x /usr/local/bin/autoconnect
# Download the systemd service and timer files
curl -o /etc/systemd/system/autoconnect.service "https://gist.githubusercontent.com/sarkrui/6aaaaeee84607af4e5ae6668d22f47dd/raw/autoconnect.service"
curl -o /etc/systemd/system/autoconnect.timer "https://gist.githubusercontent.com/sarkrui/6aaaaeee84607af4e5ae6668d22f47dd/raw/autoconnect.timer"
# Reload systemd to recognize the new units
systemctl daemon-reload
# Enable and start the service
systemctl enable autoconnect.service
systemctl start autoconnect.service
# Enable and start the timer
systemctl enable autoconnect.timer
systemctl start autoconnect.timer
echo "Installation and setup completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment