dongzhuo5425 2015-03-07 23:41
浏览 125
已采纳

如何使用Go通过SSH发送终端转义序列?

I'm writing a Go program that will connect to a host via SSH using the native x/crypto/ssh library and drop an interative shell.

I'm using RequestPty(), but the (bash) shell on the remote end does not behave as expected with control codes.

When I enter various control characters, they're echoed back by my terminal:

$ ^[[A

The characters still work in the sense that if I press enter after pressing the up arrow, the previous command is run - but the control character output clobbers what should be displayed there. The same goes for tab.

Is there some straightforward way to get this to work? When I've implemented similar systems in the past it hasn't been an issue because I've just shelled out to openssh, and the semantics of process groups sort it all out.

I've studied "The TTY Demystified" and as great as it is it's not clear where to begin.

A couple of things I've thought to investigate:

openssh itself must be doing this work correctly, but it's a real best of a code base to study.

It's not actually clear to me whether this printing is being done by my local terminal emulator or shell or by the code on the remote host.

Where do I begin?


Here is a sample of my code:

conf := ssh.ClientConfig{
    User: myuser,
    Auth: []ssh.AuthMethod{ssh.Password(my_password)}
}
conn, err := ssh.Dial("tcp", myhost, conf)
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
}
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
    return err
}
if err = session.Shell(); err != nil {
    return err
}

return session.Wait()

I've tried this with term values other than xterm: screen-256color and vt100.

For the record - in the real code, instead of just a call to session.Wait(), I have a for/select loop that catches various signals to the process and sends them on to the Session.

  • 写回答

2条回答 默认 最新

  • dongxiong4571 2015-04-29 03:32
    关注

    Disable ECHOCTL terminal mode.

    modes := ssh.TerminalModes{
        ssh.ECHO: 0,
        ssh.ECHOCTL: 0,
        ssh.TTY_OP_ISPEED: 14400,
        ssh.TTY_OP_OSPEED: 14400
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题