Skip to content

Instantly share code, notes, and snippets.

@bdbogjoe
Last active May 8, 2025 12:15
Show Gist options
  • Select an option

  • Save bdbogjoe/e1737efd466dc78b47cec5f76130fda9 to your computer and use it in GitHub Desktop.

Select an option

Save bdbogjoe/e1737efd466dc78b47cec5f76130fda9 to your computer and use it in GitHub Desktop.
openfortivpn saml
#!/bin/bash
# Script: openfortivpn-saml
# Usage: ./openfortivpn-saml [VPN_DOMAIN]
# Example: ./openfortivpn-saml vpn.domain.com
pid=$(pidof openfortivpn)
for a in "$@"; do
case $a in
--kill)
if [ "$pid" != "" ]
then
echo "killing...."
sudo kill $pid
exit 0
else
echo "Not running"
exit 1
fi
;;
--gateway=*)
VPN_DOMAIN="${a#*=}"
;;
*)
echo "Unknown parameter : $a"
exit 1
;;
esac
done
# Check if domain is added
if [ -z "$VPN_DOMAIN" ]; then
echo "Error: VPN domain not specified"
echo "Usage: $0 --gateway=[VPN_DOMAIN]"
echo "Example: $0 --gateway=vpn.domain.com"
exit 1
fi
LOG_DIR="/var/log/openfortivpn"
LOG_FILE="${LOG_DIR}/vpn_$(date +'%Y%m%d_%H%M%S').log"
# Function to start the VPN
start_vpn() {
# Create log directory if it doesn't exist
sudo mkdir -p "$LOG_DIR"
sudo chown $(whoami) "$LOG_DIR"
echo "Starting VPN connection to ${VPN_DOMAIN}..."
sudo -b nohup openfortivpn "${VPN_DOMAIN}:443" --saml-login > "$LOG_FILE" 2>&1 &
# Wait for SAML URL to appear and open it
echo "Waiting for SAML authentication URL..."
while true; do
if grep -m1 -q "https://${VPN_DOMAIN}:443/remote/saml/start?redirect=1" "$LOG_FILE"; then
grep -m1 -o "https://${VPN_DOMAIN}:443/remote/saml/start?redirect=1" "$LOG_FILE" | xargs xdg-open
break
fi
sleep 1
done
echo -e "\nVPN process is running in background."
echo "You can safely close this terminal. Log file: ${LOG_FILE}"
echo "To stop VPN later, run: sudo pkill -f \"openfortivpn ${VPN_DOMAIN}"
}
if [ "$pid" == "" ]
then
start_vpn
else
echo "Already running : $pid"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment