dsjgk330337 2019-07-29 21:39
浏览 518

如何在Go crypto / ssh会话中处理EOF / Ctrl + D.Wait()

When I use session.Shell() to start a new shell on a remote server and then session.Wait() to run the session until the remote end exits the session does not gracefully handle using Ctrl+D to end the session.

I can make this work using os/exec to launch a local child process to run whatever copy of the ssh client is available locally, but I would prefer to do this with native Go.

Examle code snippet:

conn, err := ssh.Dial("tcp", "some-server.fqdn", sshConfig)
if err != nil {
  return err
}
defer conn.Close()

session, err := conn.NewSession()
if err != nil {
  return err
}
defer session.Close()

session.Stdout = os.Stdout
session.Stderr = os.Stderr
session.Stdin = os.Stdin

modes := ssh.TerminalModes{
  ssh.ECHO:          0,
  ssh.TTY_OP_ISPEED: 14400,
  ssh.TTY_OP_OSPEED: 14400,
}

err = session.RequestPty("xterm", 80, 40, modes)
if err != nil {
  return err
}

err = session.Shell()
if err != nil {
  return err
}
session.Wait()

Running exit on the remote server gracefully hangs up the remote end and session.Wait() returns as expected, but sending an EOF with Ctrl+D causes the remote end to hang up but the call to session.Wait() is stuck blocking. I have to use Ctrl+C to SIGINT the Go program. I would like to get both to gracefully exit the session.Wait() call as that is expected behavior for most interactive ssh sessions.

  • 写回答

1条回答 默认 最新

  • douxian3828 2019-07-30 03:41
    关注

    I was able to reproduce this (with a bunch of additional framework code) but am not sure why it happens. It is possible to terminate the session by adding a session.Close call if you encounter EOF on stdin:

            session.Stdout = os.Stdout
            session.Stderr = os.Stderr
            // session.Stdin = os.Stdin
            ip, err := session.StdinPipe()
            if err != nil {
                    return err
            }
            go func() {
                    io.Copy(ip, os.Stdin)
                    fmt.Println("stdin ended")
                    time.Sleep(1 * time.Second)
                    fmt.Println("issuing ip.Close() now")
                    ip.Close()
                    time.Sleep(1 * time.Second)
                    fmt.Println("issuing session.Close() now")
                    err = session.Close()
                    if err != nil {
                            fmt.Printf("close: %v
    ", err)
                    }
            }()
    

    You'll see that the session shuts down (not very nicely) after the session.Close(). Calling ip.Close() should have shut down the stdin channel, and it seems like this should happen when just using os.Stdin directly too, but for some reason it does not work. Debugging shows an ssh-channel-close message going to the other end (for both cases), but the other end doesn't close the return-data ssh channels, so your end continues to wait for more output from them.

    Worth noting: you have not put the local tty into raw-ish character-at-a-time mode. A regular ssh session does, so ^D does not actually close the connection, it just sends a literal control-D to the pty on the other side. It's the other end that turns that control-D into a (soft) EOF signal on the pty.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题