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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?