如何用os.system实现跨平台SSH后台连接?Windows替代/dev/null方案
Windows Alternative for Background Serveo SSH Tunnel with os.system()
Got it, let's fix this for Windows while keeping your original workflow with os.system(). The main differences are replacing Linux's /dev/null and adjusting how we run the command in the background—here's how to do it:
The Modified Command for Windows
Instead of relying on /dev/null and & (which don't work the same in Windows CMD), we'll use Windows-native tools:
import os os.system('start /B ssh -o StrictHostKeyChecking=no -o ServerAliveInterval=60 -R test:80:127.0.0.1:80 serveo.net > data.txt 2> NUL')
Breakdown of Changes:
start /B: This is Windows CMD's way to run a command in the background. It prevents a new command prompt window from popping up and ensures your Python program can continue executing without waiting for the SSH tunnel to finish.2> NUL:NULis Windows' equivalent of Linux's/dev/null—it discards all error output, just like your original command did with2> /dev/null.- Standard Output: We keep
> data.txtto log the Serveo output exactly as you did in Linux.
Key Notes
- Make sure
sshis available in your Windows PATH: You can install Windows' native OpenSSH via Settings > Apps > Optional features, or use the SSH included with Git Bash. If it's not in PATH, specify the full path tossh.exe(e.g.,C:\\Program Files\\OpenSSH\\ssh.exe). - Test first without backgrounding: To debug, run the SSH command directly in CMD (without
start /B) to confirm it connects to Serveo properly before adding the background and redirection. - No extra dependencies: This uses only built-in Windows CMD features, so you don't need to install anything beyond SSH.
内容的提问来源于stack exchange,提问作者Ilan Kalendarov
相关产品推荐
相关产品推荐

