Linux TCP 连接,仅限 bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7765004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:41:45  来源:igfitidea点击:

TCP connection, bash only

linuxbashsocketstcp

提问by gregseth

I found this line in a script. While I globally understand what it does--opening a bidirectional TCP connection--, I need some explanations on the syntax. Here's the line:

我在脚本中找到了这一行。虽然我全局理解它的作用——打开一个双向 TCP 连接——,但我需要一些关于语法的解释。这是行:

exec 5<>"/dev/tcp/${SERVER}/${PORT}"

And my questions:

还有我的问题:

  1. <and >are usually used to redirect IOs. What does it mean there? Is it usable in another context? How?
  2. Why does it work, while /dev/tcpdoesn't exists?
  3. Why 5? Can it be another number? What are the values allowed?
  4. Why is execnecessary? (given nothing is actually executed)
  1. <并且>通常用于重定向 IO。在那里是什么意思?它可以在其他上下文中使用吗?如何?
  2. 为什么它有效,而/dev/tcp它不存在?
  3. 为什么是 5?可以是另一个号码吗?允许的值是多少?
  4. 为什么是exec必要的?(假设没有实际执行

Thanks.

谢谢。

采纳答案by trojanfoe

< and > are usually used to redirect IOs. What does it mean there? Is it usable in another context? How?

< 和 > 通常用于重定向 IO。在那里是什么意思?它可以在其他上下文中使用吗?如何?

It's the same - input and output is redirected to fd 5.

它是一样的 - 输入和输出被重定向到 fd 5。

Why does it work, while /dev/tcp doesn't exists?

为什么它有效,而 /dev/tcp 不存在?

It's a special file: If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open a TCP connection to the corresponding socket.

这是一个特殊文件:如果 host 是有效的主机名或 Internet 地址,并且 port 是整数端口号或服务名,bash 会尝试打开到相应套接字的 TCP 连接。

Why 5? Can it be another number? What are the values allowed?

为什么是 5?可以是另一个号码吗?允许的值是多少?

Yes, it can be any value, but you need to ensure you don't use an fd already in use.

是的,它可以是任何值,但您需要确保不使用已经在使用的 fd。

Why is exec necessary? (given nothing is actually executed)

为什么 exec 是必要的?(假设没有实际执行)

exec means the redirection happens in the current shell, not within a subshell.

exec 表示重定向发生在当前 shell 中,而不是在子 shell 中。

回答by ktf

I can only answer for the exec part:

我只能回答exec部分:

exec without a command given may be used to change I/O redirections. <> in this case means open for read+write. 5 is the channel number (or file descriptor). This makes sense if other commands send their output / read their input from channel 5.

没有给出命令的 exec 可用于更改 I/O 重定向。<> 在这种情况下意味着打开读写。5 是通道号(或文件描述符)。如果其他命令从通道 5 发送它们的输出/读取它们的输入,这是有道理的。

For "/dev/tcp/${SERVER}/${PORT}" I don't know if it's a feature of a specific Linux version or if it's a bash feature (I assume the latter).

对于“/dev/tcp/${SERVER}/${PORT}”,我不知道它是特定 Linux 版本的功能还是 bash 功能(我假设是后者)。

-- EDIT: from the bash manual page: --

-- 编辑:来自 bash 手册页:-

 Bash handles several filenames specially when they are  used
 in redirections, as described in the following table:

      /dev/fd/fd
           If fd is a valid integer, file  descriptor  fd  is
           duplicated.
      /dev/stdin
           File descriptor 0 is duplicated.
      /dev/stdout
           File descriptor 1 is duplicated.
      /dev/stderr
           File descriptor 2 is duplicated.
      /dev/tcp/host/port
           If host is a valid hostname or  Internet  address,
           and  port  is  an  integer  port number or service
           name, bash attempts to open a  TCP  connection  to
           the corresponding socket.
      /dev/udp/host/port
           If host is a valid hostname or  Internet  address,
           and  port  is  an  integer  port number or service
           name, bash attempts to open a  UDP  connection  to
           the corresponding socket.