Created
July 16, 2025 18:21
-
-
Save blobaugh/52d9c796b1fa46efd107745a3681a013 to your computer and use it in GitHub Desktop.
Script that runs commands only after the PIA VPN is connected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# VPN Connect, Execute Command, and Disconnect Script | |
# Usage: ./vpn_script.sh "command_to_run" | |
set -e # Exit on any error | |
# Configuration | |
MAX_WAIT_TIME=30 # Maximum time to wait for connection (seconds) | |
CHECK_INTERVAL=2 # How often to check connection status (seconds) | |
# Colors for output | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[1;33m' | |
NC='\033[0m' # No Color | |
# Function to print colored output | |
print_status() { | |
echo -e "${GREEN}[INFO]${NC} $1" | |
} | |
print_warning() { | |
echo -e "${YELLOW}[WARNING]${NC} $1" | |
} | |
print_error() { | |
echo -e "${RED}[ERROR]${NC} $1" | |
} | |
# Function to check if VPN is connected | |
check_vpn_status() { | |
local status=$(piactl get connectionstate 2>/dev/null | grep -i "connected" || echo "") | |
if [[ -n "$status" ]]; then | |
return 0 # Connected | |
else | |
return 1 # Not connected | |
fi | |
} | |
# Function to cleanup on exit | |
cleanup() { | |
print_status "Cleaning up..." | |
piactl disconnect >/dev/null 2>&1 || true | |
print_status "Disconnected from VPN" | |
} | |
# Set trap to ensure cleanup on script exit | |
trap cleanup EXIT | |
# Check if command is provided | |
if [ $# -eq 0 ]; then | |
print_error "Usage: $0 \"command_to_run\"" | |
print_error "Example: $0 \"curl -s https://ipinfo.io/ip\"" | |
exit 1 | |
fi | |
COMMAND="$1" | |
print_status "Starting VPN connection process..." | |
# Check if piactl is available | |
if ! command -v piactl &> /dev/null; then | |
print_error "piactl command not found. Please install Private Internet Access client." | |
exit 1 | |
fi | |
# Disconnect first (in case already connected) | |
print_status "Ensuring clean state..." | |
piactl disconnect >/dev/null 2>&1 || true | |
# Connect to VPN | |
print_status "Connecting to VPN..." | |
if ! piactl connect; then | |
print_error "Failed to initiate VPN connection" | |
exit 1 | |
fi | |
# Wait for connection to establish | |
print_status "Waiting for VPN connection to establish..." | |
elapsed=0 | |
while [ $elapsed -lt $MAX_WAIT_TIME ]; do | |
if check_vpn_status; then | |
print_status "VPN connection established successfully!" | |
break | |
fi | |
sleep $CHECK_INTERVAL | |
elapsed=$((elapsed + CHECK_INTERVAL)) | |
printf "." | |
done | |
echo "" # New line after dots | |
# Check if connection was successful | |
if ! check_vpn_status; then | |
print_error "VPN connection failed or timed out after ${MAX_WAIT_TIME} seconds" | |
exit 1 | |
fi | |
# Display connection info | |
print_status "VPN Status:" | |
piactl get connectionstate | |
# Execute the user command | |
print_status "Executing command: $COMMAND" | |
echo "----------------------------------------" | |
echo "" | |
echo "" | |
# Execute the command and capture exit code | |
set +e # Don't exit this script on command failure | |
eval "$COMMAND" | |
command_exit_code=$? | |
set -e | |
echo "" | |
echo "" | |
echo "----------------------------------------" | |
if [ $command_exit_code -eq 0 ]; then | |
print_status "Command executed successfully" | |
else | |
print_warning "Command exited with code: $command_exit_code" | |
fi | |
print_status "Script completed. VPN will be disconnected by cleanup function." | |
# Exit with the same code as the executed command | |
exit $command_exit_code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment