bash脚本设置一个临时的SSH隧道 [英] Bash script to setup a temporary SSH tunnel

查看:191
本文介绍了bash脚本设置一个临时的SSH隧道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Cygwin,我要一个bash脚本:

On CYGWIN, I want a BASH script to:


  1. 创建一个SSH隧道到远程服务器。

  2. 请当地一些工作使用的隧道。

  3. 然后关闭隧道。

关闭部分有我不知所措。

The "shutdown part" has me perplexed.

目前,我有一个跛脚的解决方案。在一个外壳我运行下面创建一个隧道。

Currently, I have a lame solution. In one shell I run the following to create a tunnel.

# Create the tunnel - this works! It runs forever, until shell is quit.
ssh -nNT -L 50000:localhost:3306 jm@sampledomain.com

然后,在另一个shell窗口,我做我的工作。

Then, in another shell window, I do my work

# Do some MYSQL stuff over local port 50000 (which goes to remote port 3306)

最后,当我完成。我关闭第一个shell窗口杀隧道。

Finally, when I am done. I close the first shell window to kill the tunnel.

我想做到这一切在一个脚本中,如:
    #创建隧道
    # 做工作
    #隧道杀

I'd like to do this all in one script like: # Create tunnel # do work # Kill tunnel

我如何跟踪隧道的过程,所以我知道杀了哪一个?

How do I keep track of the tunnel process, so I know which one to kill?

推荐答案

您可以用干净的SSH控制插座做到这一点。交谈一个已经运行的SSH进程,并得到它的PID,杀吧等使用控制接口(-M硕士和-S套接字)如下:

You can do this cleanly with an ssh 'control socket'. To talk to an already-running SSH process and get it's pid, kill it etc. Use the 'control socket' (-M for master and -S for socket) as follows:

$ ssh -M -S my-ctrl-socket -fnNT -L 50000:localhost:3306 jm@sampledomain.com
$ ssh -S my-ctrl-socket -O check jm@sampledomain.com
Master running (pid=3517) 
$ ssh -S my-ctrl-socket -O exit jm@sampledomain.com
Exit request sent. 

请注意我的Ctrl插槽将是创建一个实际的文件。

Note that my-ctrl-socket will be an actual file that is created.

我得到这个信息从在OpenSSH的邮件列表非常RTFM答复。

I got this info from a very RTFM reply on the OpenSSH mailing list.

这篇关于bash脚本设置一个临时的SSH隧道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆