ssh:检查隧道是否存在
我编写了一个 小型 bash 脚本,它需要 ssh 隧道从远程服务器提取数据,因此它提示用户:
echo "Please open an ssh tunnel using 'ssh -L 6000:localhost:5432 example.com'"
我想检查用户是否打开了此隧道,如果不存在隧道,则退出并显示错误消息。有没有办法查询 ssh 隧道,即检查本地端口 6000 是否真的通过隧道连接到该服务器?
I have written a small bash script which needs an ssh tunnel to draw data from a remote server, so it prompts the user:
echo "Please open an ssh tunnel using 'ssh -L 6000:localhost:5432 example.com'"
I would like to check whether the user had opened this tunnel, and exit with an error message if no tunnel exist. Is there any way to query the ssh
tunnel, i.e. check if the local port 6000 is really tunneled to that server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Netcat 是你的朋友:
Netcat is your friend:
这是我的测试。希望它有用。
This is my test. Hope it is useful.
Autossh 是最好的选择 - 检查进程是否在所有情况下都工作(例如僵尸进程、网络相关问题)
示例:
Autossh is best option - checking process is not working in all cases (e.g. zombie process, network related problems)
example:
这实际上更像是一个服务器故障类型的问题,但您可以使用 netstat。
类似于:
这会告诉您是否有 ssh 进程正在侦听指定端口。它还会告诉你进程的 PID。
如果您确实想仔细检查 ssh 进程是否使用正确的选项启动,则可以通过 PID 查找该进程,例如
This is really more of a serverfault-type question, but you can use netstat.
something like:
This will tell you if there's an ssh process listening on the specified port. it will also tell you the PID of the process.
If you really want to double-check that the ssh process was started with the right options, you can then look up the process by PID in something like
使用 autossh。这是用于监控 ssh 连接的工具。
Use autossh. It's the tool that's meant for monitoring the ssh connection.
我们可以使用 ps 命令进行检查
将显示所有正在运行的 shh 服务,我们可以找到列出的隧道服务
We can check using ps command
Will show all shh service running and we can find the tunnel service listed
这些是测试 SSH 隧道或排除故障的更详细步骤。您可以在脚本中使用其中一些。我添加这个答案是因为我必须在两个应用程序停止工作后对其之间的链接进行故障排除。仅 grep 查找 ssh 进程是不够的,因为它仍然存在。而且我无法使用 nc -z ,因为该选项在我的 netcat 咒语中不可用。
让我们从头开始吧。假设有一台机器,称为本地,IP 地址为 10.0.0.1,另一台称为远程,IP 地址为 10.0.3.12。我将把这些主机名添加到下面的命令中,这样它们的执行位置就很明显了。
目标是创建一个隧道,将 TCP 流量从远程计算机上端口 123 上的环回地址转发到本地计算机上端口 456。这可以在本地计算机上使用以下命令来完成
:正在运行,我们可以这样做:
如果您在输出中看到该命令,我们就可以继续。否则,请检查远程设备中是否安装了 SSH 密钥。请注意,排除远程 IP 之前的用户名会使 ssh 使用当前用户名。
接下来,我们要检查远程设备上的隧道是否打开:
我们应该得到与此类似的输出:
如果能够实际看到从远程设备到主机的一些数据,那就太好了。这就是 netcat 发挥作用的地方。在 CentOS 上,可以使用
yum install nc
安装它。首先,在本地计算机上打开侦听端口:
然后在远程上建立连接:
如果打开本地计算机的第二个终端,则可以看到该连接。像这样的东西:
更好的是,继续在远程输入一些内容:
您应该看到它被镜像到本地终端上:
隧道正在工作!但是,如果您有一个名为 appname 的应用程序,该应用程序应该侦听本地计算机上的端口 456,该怎么办?终止两侧的 nc,然后运行您的应用程序。您可以使用 this:
顺便说一下,在远程运行相同的命令应该显示 sshd 正在侦听端口 127.0.0.1:123。
These are more detailed steps to test or troubleshoot an SSH tunnel. You can use some of them in a script. I'm adding this answer because I had to troubleshoot the link between two applications after they stopped working. Just grepping for the ssh process wasn't enough, as it was still there. And I couldn't use
nc -z
because that option wasn't available on my incantation of netcat.Let's start from the beginning. Assume there is a machine, which will be called local with IP address 10.0.0.1 and another, called remote, at 10.0.3.12. I will prepend these hostnames, to the commands below, so it's obvious where they're being executed.
The goal is to create a tunnel that will forward TCP traffic from the loopback address on the remote machine on port 123 to the local machine on port 456. This can be done with the following command, on the local machine:
To check that the process is running, we can do:
If you see the command in the output, we can proceed. Otherwise, check that the SSH key is installed in the remote. Note that excluding the username before the remote IP, makes ssh use the current username.
Next, we want to check that the tunnel is open on the remote:
We should get an output similar to this:
Would be nice to actually see some data going through from the remote to the host. This is where netcat comes in. On CentOS it can be installed with
yum install nc
.First, open a listening port on the local machine:
Then make a connection on the remote:
If you open a second terminal to the local machine, you can see the connection. Something like this:
Better still, go ahead and type something on the remote:
You should see this being mirrored on the local terminal:
The tunnel is working! But what if you have an application, called appname, which is supposed to be listening on port 456 on the local machine? Terminate nc on both sides then run your application. You can check that it's listening on the correct port with this:
By the way, running the same command on the remote should show sshd listening on port 127.0.0.1:123.
stunnel 是在主机之间建立半永久连接的好工具。
http://www.stunnel.org/
stunnel is a good tool to make semi-permanent connections between hosts.
http://www.stunnel.org/
如果您在后台使用 ssh,请使用以下命令:
If you are using ssh in background, use this: