Auto-detect listening ports in GitHub Codespaces and create SSH tunnels to access them at localhost on your Windows machine.
When you SSH into a codespace with gh cs ssh and start a server (e.g., npm start on port 3000), you can't access localhost:3000 from your Windows machine - unlike VS Code which auto-forwards ports.
This script:
- Monitors your codespace for listening ports
- Automatically creates SSH tunnels (
ssh -L) for each detected port - Lets you access services at
localhost:<port>on Windows
# Run directly
.\cswatch-standalone.ps1
# Or with specific codespace
.\cswatch-standalone.ps1 -Codespace "my-codespace-name"
# Press 'q' to quit and close all tunnels cleanlyIf you close the terminal without pressing 'q', SSH tunnels may be left running:
# List and kill orphaned SSH tunnel processes
.\csstop-standalone.ps1Terminal 1: Run the watcher
.\cswatch-standalone.ps1
# Select codespace from picker
# Watches for new ports...Terminal 2: SSH and start your server
gh cs ssh -c my-codespace
npm start # Server starts on port 3000Terminal 1: Shows detection
[Detected port 3000] -> localhost:3000
Browser: Open http://localhost:3000 - it works!
Just download the scripts and run them from any directory:
.\cswatch-standalone.ps1
.\csstop-standalone.ps1# Add to your $PROFILE
Set-Alias cswatch "C:\path\to\cswatch-standalone.ps1"
Set-Alias csstop "C:\path\to\csstop-standalone.ps1"| Parameter | Default | Description |
|---|---|---|
-Codespace |
(picker) | Name of codespace to watch |
-IgnorePorts |
22,53,1089,2222 | Ports to ignore |
-IncludeHighPorts |
false | Include ports > 49151 |
-IncludeLowPorts |
false | Include ports < 1024 |
-PollInterval |
2 | Seconds between checks |
- Generate SSH Config: Uses
gh cs ssh --configto get SSH configuration - Fix Windows Paths: Converts backslashes to forward slashes, quotes paths with spaces
- Poll for Ports: Runs
ss -tlnHin codespace to detect listening ports - Create Tunnels: Uses
ssh -F config -L port:localhost:port -N hostfor each port
- PowerShell 5.1+ or PowerShell Core
- GitHub CLI (
gh) authenticated - OpenSSH client (included in Windows 10+)
Check if the port is actually bound:
netstat -ano | Select-String "YOUR_PORT.*LISTENING"If not listed, the tunnel failed. Try:
- Kill stale SSH processes:
Get-Process ssh | Stop-Process -Force - Restart the watcher
The script should handle this, but if you see errors about "C:Program", the ProxyCommand path isn't quoted properly. Check ~/.ssh/codespaces_portfwd_config.
MIT