SSH 会话 - 客户端的固定端口 [英] SSH session - fixed port on the client side

查看:65
本文介绍了SSH 会话 - 客户端的固定端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在连接的客户端设置固定端口?

Is it possible to set the fixed port on the client side of the connection?

我使用端口 22 连接到 ssh 服务器,并且客户端套接字正在获取随机端口来识别会话.一个例子(来自 netstat -atn 的输出)

I connect to the ssh-server using port 22 and the client socket is getting random port to identify the session. An example (output from netstat -atn)

tcp4       0      0  <server>.22       <client>.54117    ESTABLISHED

在此示例中,客户端获取端口 54117.出于测试目的,我希望为客户端分配一个固定端口,假设为 40185.

In this example, client gets port 54117. For the test purposes, I'd like a fixed port to be assigned for the client, let's say 40185.

所以我喜欢以下输出:

tcp4       0      0  <server>.22       <client>.40185    ESTABLISHED

有可能吗?

推荐答案

您可以以编程方式进行,但 ssh(1) 命令不允许这样做.主要原因是您让内核选择客户端端口,因此您可以从同一台客户端机器上的不同源端口向同一服务器打开多个 ssh(1) 会话.如果在客户端和服务器端固定端口号,则无法区分属于一个连接的数据包和属于另一个连接的数据包(相同的协议、tcp、相同的源地址、相同的目标地址、相同的源端口和相同的目标端口))

You can do it programmaticaly, but the ssh(1) command doesn't allow to do that. The main reason is that you let the kernel select the client port, so you can open more than one ssh(1) session to the same server from different source ports in the same client machine. If you fix the port number in the client and the server, you cannot distinguish the packets belonging to one connection from the ones belonging to the other (same protocol, tcp, same source address, same dest address, same source port and same destination port)

要在客户端以编程方式执行并修复本地端口,只需在执行 connect(2) 系统之前调用 bind(2) 系统调用来修复它调用(就像服务器在 accept(2) 系统调用之前所做的那样)

To do it programmaticaly in a client and fix the local port, just call bind(2) system call to fix it, before doing the connect(2) system call (as the server does just before the accept(2) system call)

注意不能有两个具有相同五个参数的连接(source add源端口tcp 协议目标端口目标地址)

Be careful in that you cannot have two connections with the same five parameters (source add, source port, tcp protocol, dest port, dest addr)

这篇关于SSH 会话 - 客户端的固定端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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