mysql - How to close an ssh tunnel in python -
i writing python application accesses remote mysql database using ssh tunneling. setup tunnel
os.system("ssh -fng -l 3306:127.0.0.1:3306 username@host")
and works perfectly. problem is:
what python code close ssh tunnel?
thanks
os.system('exit') doesn't work
the process creates tunnel still runs in background
>>> command = 'ssh -fng vagrant@localhost -p2222 -l 8000:localhost:8000' >>> os.system(command) >>> os.system('exit') ps -a | grep ssh 7144 ?? 0:00.04 ssh -fng vagrant@localhost -p2222 -l 8000:localhost:8000
this shows process still running , tunnel still working, , os.system
doesn't return process id can use terminate process (it returns exit code)
use subprocess return handle process
import subprocess proc = subprocess.popen(command, shell=true) proc.terminate() # terminates process
Comments
Post a Comment