The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
ntdll

go 中 exec.cmd 中对于标准输入输出流重定向的问题

  •  
  •   ntdll · Jul 8, 2022 · 1647 views
    This topic created in 1431 days ago, the information mentioned may be changed or developed.

    学习项目,目标是实现一个简单的 telnet 功能(远程执行命令)。但是第一步就出现了问题。主要代码如下:

    https://gist.github.com/Lua12138/7cf05e84ee30adde582f78f0c4a62103

    通过这样的代码,我发现,如果执行不需要交互的命令,一切都符合预期,但执行需要交互命令行,比如 bash/base64 这样的命令行,就出现了预期外结果。

    如果执行 bash ,那么在客户端(下半部分代码),后续输入任何命令行都会提示: not found/sh: <stdin>[2]: ls,而其他命令,应该如何通过网络发送过去^D 这样的控制符呢。

    wwqgtxx
        1
    wwqgtxx  
       Jul 8, 2022
    你可能需要一个 tty
    https://github.com/creack/pty
    ntdll
        2
    ntdll  
    OP
       Jul 8, 2022
    @wwqgtxx #1 感谢大佬。仔细排查后,是系统原因。由于客户端是在 Windows 下运行,因此 stdin 被转发到对端以后,Linux 解析命令行以\n 分隔,但是 Windows 中以\r\n 作为分隔,因此 Linux 最终得到类似这样的命令参数 ls\r ,所以出现了找不到命令的情况。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3775 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 10:30 · PVG 18:30 · LAX 03:30 · JFK 06:30
    ♥ Do have faith in what you're doing.