A comprehensive PowerShell script for testing VPN connectivity with Nagios integration and detailed diagnostics.
This script provides comprehensive VPN connectivity testing with specific focus on Cisco AnyConnect environments. It performs multiple validation tests to ensure your VPN connection is not only established but actually working correctly, including verification that traffic routes through the VPN tunnel.
- π Multi-layered VPN Detection: Network interfaces, CLI integration, services, and processes
- π VPN Tunnel Validation: Ensures traffic actually routes through VPN (no bypass)
- π₯ Nagios Integration: Ready-to-use monitoring with performance data
- β‘ VPN Server Response Time: Tracks actual server connectivity performance
- ποΈ Flexible Configuration: Test any internal server with custom ports
- π Comprehensive Reporting: 8 different connectivity tests with scoring
- π§ Cross-Platform: Works on Windows PowerShell 5.1+ and PowerShell Core
# Download and run basic VPN test
.\vpn_connection_tester.ps1
# Test specific VPN server with custom ports
.\vpn_connection_tester.ps1 -VpnServer "myserver.local" -Ports 80,443,1433
# Nagios monitoring mode
.\vpn_connection_tester.ps1 -Nagios -VpnServer "db-server" -Ports 1433,5432
=== VPN CONNECTION SUMMARY ===
Network Interface Detection: β PASS
CLI Status Check: β PASS
VPN Services Running: β PASS
VPN Processes Running: β PASS
VPN Routing Detected: β PASS
Internet Connectivity: β PASS
VPN Server Access: β PASS
DNS Resolution: β PASS
Overall Score: 8/8 (100%)
π’ VPN Status: CONNECTED AND WORKING
The script performs 8 comprehensive tests:
Test | Description | What It Validates |
---|---|---|
Network Interface Detection | Scans for active VPN adapters | Physical VPN connection |
CLI Status Check | Cisco AnyConnect CLI integration | Official connection status |
VPN Services | Windows service monitoring | VPN software running |
VPN Processes | Process detection | VPN client active |
Routing Analysis | Network route validation | Traffic routing setup |
Internet Connectivity | External host ping tests | Basic internet access |
VPN Server Access | Internal server connectivity | VPN tunnel functionality |
DNS Resolution | Domain name resolution | DNS through VPN |
Parameter | Type | Default | Description |
---|---|---|---|
-VpnServer |
String | "carvmxdev3" |
Internal server to test VPN access |
-Ports |
Int[] | 80,443,22 |
Ports to test on VPN server |
-TestHost |
String | "8.8.8.8" |
External host for internet connectivity |
-TimeoutSeconds |
Int | 10 |
Network operation timeout |
-Nagios |
Switch | false |
Output in Nagios format |
-SkipVpnServerTest |
Switch | false |
Skip VPN server connectivity test |
-Detailed |
Switch | false |
Show detailed diagnostic information |
# Test web server
.\vpn_connection_tester.ps1 -VpnServer "intranet.company.com" -Ports 80,443
# Test database server
.\vpn_connection_tester.ps1 -VpnServer "sql-server" -Ports 1433,1434
# Test multiple services
.\vpn_connection_tester.ps1 -VpnServer "app-server" -Ports 80,443,1433,3389,22
# Skip VPN server test
.\vpn_connection_tester.ps1 -SkipVpnServerTest
# Detailed diagnostics
.\vpn_connection_tester.ps1 -Detailed -VpnServer "fileserver" -Ports 445,139
The script specifically targets Cisco AnyConnect with:
- CLI Integration: Executes
vpncli.exe state
for official status - Interface Detection: Identifies AnyConnect virtual adapters
- Service Monitoring: Tracks Cisco AnyConnect services
- Process Detection: Monitors AnyConnect processes
Searched CLI Paths:
C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe
C:\Program Files\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe
C:\Program Files (x86)\Cisco\Cisco AnyConnect VPN Client\vpncli.exe
The script ensures traffic actually uses the VPN tunnel:
- Interface Identification: Detects active VPN network adapter
- Private IP Validation: Confirms target server is in private ranges (RFC 1918)
- Route Verification: Validates routing through VPN interface
- DNS Resolution: Tests VPN DNS functionality
- Connection Tracking: Measures actual response times through tunnel
The script provides rich performance metrics:
# Nagios output example
OK - VPN server 'carvmxdev3' accessible (45ms) | response_time=45ms;server=carvmxdev3;vpn_server_response_time=45ms
Metrics Included:
response_time
: VPN server average response time (primary metric)server
: Name of tested VPN servervpn_server_response_time
: Specific server timingtotal_script_time
: Complete test duration
Code | Status | Condition | Tests Passed |
---|---|---|---|
0 |
OK | VPN working properly | 7-8 tests |
1 |
WARNING | Partial connectivity | 5-6 tests |
2 |
CRITICAL | VPN issues/disconnected | 0-4 tests |
3 |
UNKNOWN | Script error | N/A |
define command {
command_name check_vpn_server
command_line powershell.exe -ExecutionPolicy Bypass -File "C:\\scripts\\vpn_connection_tester.ps1" -Nagios -VpnServer $ARG1$ -Ports $ARG2$
}
define service {
use generic-service
host_name vpn-client
service_description VPN Server Access - Database
check_command check_vpn_server!db-server!1433,5432
max_check_attempts 3
normal_check_interval 5
retry_check_interval 2
}
- Windows 10/11 or Windows Server 2016+
- PowerShell 5.1+ (built into Windows)
- Network adapter access (for interface detection)
- Cisco AnyConnect (for CLI integration)
-
Download the script:
# Download directly Invoke-WebRequest -Uri "https://raw.githubusercontent.com/GabrielFarfan/VPN-Connection-Tester/main/vpn_connection_tester.ps1" -OutFile "vpn_connection_tester.ps1" # Or clone the repository git clone https://github.com/GabrielFarfan/VPN-Connection-Tester.git
-
Set execution policy (if needed):
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
-
Test the installation:
.\vpn_connection_tester.ps1 -VpnServer "your-server" -Ports 80,443
For automated monitoring, create a scheduled task:
# Create scheduled task for regular VPN monitoring
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Scripts\vpn_connection_tester.ps1 -Nagios -VpnServer myserver -Ports 80,443"
$Trigger = New-ScheduledTaskTrigger -RepetitionInterval (New-TimeSpan -Minutes 5) -Once -At (Get-Date)
$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
Register-ScheduledTask -TaskName "VPN Monitor" -Action $Action -Trigger $Trigger -Settings $Settings
# Test development server access
.\vpn_connection_tester.ps1 -VpnServer "dev-server" -Ports 80,443,3000,8080
# Monitor database connectivity
.\vpn_connection_tester.ps1 -VpnServer "db-prod" -Ports 1433,5432,3306 -Nagios
# Infrastructure monitoring
.\vpn_connection_tester.ps1 -VpnServer "domain-controller" -Ports 389,636,3268
# Daily connectivity check
.\vpn_connection_tester.ps1 -VpnServer "fileserver" -Ports 445,139 -Detailed
"No VPN interfaces detected"
- Ensure VPN client is installed and connected
- Check if interface names match detection patterns
- Run with
-Detailed
for interface listing
"VPN server not accessible"
- Verify server name/IP is correct
- Check if server is behind VPN (private IP range)
- Confirm firewall allows the tested ports
"DNS resolution failed"
- VPN DNS may not be configured properly
- Try using IP address instead of hostname
- Check VPN client DNS settings
PowerShell execution policy errors
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
For detailed troubleshooting:
.\vpn_connection_tester.ps1 -Detailed -VpnServer "problematic-server" -Ports 80,443
- Script execution time: Typically 5-15 seconds
- Network timeout: Configurable (default 10 seconds)
- VPN server response time: Measured in milliseconds
- Memory usage: Minimal (<10MB)
- CPU usage: Low impact
Contributions are welcome! Please feel free to submit pull requests or open issues.
# Clone the repository
git clone https://github.com/GabrielFarfan/VPN-Connection-Tester.git
cd VPN-Connection-Tester
# Test your changes
.\vpn_connection_tester.ps1 -Detailed
- Test with Cisco AnyConnect connected/disconnected
- Verify Nagios output format
- Test with various server types (web, database, file)
- Check cross-platform compatibility
- Validate performance data accuracy
This project is licensed under the MIT License - see the LICENSE file for details.
- Cisco AnyConnect team for CLI tools
- PowerShell community for networking modules
- Nagios project for monitoring standards
- Contributors and testers
- Issues: GitHub Issues
- Documentation: Wiki
- Discussions: GitHub Discussions
Made with β€οΈ for reliable VPN monitoring