Paramiko SSH Tunnel Shutdown Issue

前端 未结 4 881
萌比男神i
萌比男神i 2021-02-10 23:36

I\'m working on a python script to query a few remote databases over an established ssh tunnel every so often. I\'m fairly familiar with the paramiko library, so that was my cho

4条回答
  •  孤城傲影
    2021-02-11 00:02

    You may want to add some synchronization between the spawned thread and the caller so that you don't try to use the tunnel before it is ready. Something like:

        from threading import Event   
        def startTunnel(self):
            class SubHandler(Handler):
                chain_host = '127.0.0.1'
                chain_port = 5432
                ssh_transport = self.c.get_transport()
            mysignal = Event()
            mysignal.clear()
            def ThreadTunnel():
                global t
                t = ForwardServer(('', 3333), SubHandler)
                mysignal.set() 
                t.serve_forever()
            Thread(target=ThreadTunnel).start()
            mysignal.wait()
    

提交回复
热议问题