2 回答
TA贡献1847条经验 获得超11个赞
在ssh.ECHO: 0与ssh.ECHOCTL: 0设置并没有为我工作。对于遇到此问题的其他人,以下是使用 Go ssh 库获得完全工作的交互式终端所需的粗略代码:
config := return &ssh.ClientConfig{
User: "username",
Auth: []ssh.AuthMethod{ssh.Password("password")},
}
client, err := ssh.Dial("tcp", "12.34.56.78:22", config)
if err != nil { ... }
defer client.Close()
session, err := client.NewSession()
if err != nil { ... }
defer session.Close()
session.Stdout = os.Stdout
session.Stderr = os.Stderr
session.Stdin = os.Stdin
modes := ssh.TerminalModes{
ssh.ECHO: 1, // enable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
fileDescriptor := int(os.Stdin.Fd())
if terminal.IsTerminal(fileDescriptor) {
originalState, err := terminal.MakeRaw(fileDescriptor)
if err != nil { ... }
defer terminal.Restore(fileDescriptor, originalState)
termWidth, termHeight, err := terminal.GetSize(fileDescriptor)
if err != nil { ... }
err = session.RequestPty("xterm-256color", termHeight, termWidth, modes)
if err != nil { ... }
}
err = session.Shell()
if err != nil { ... }
// You should now be connected via SSH with a fully-interactive terminal
// This call blocks until the user exits the session (e.g. via CTRL + D)
session.Wait()
请注意,所有键盘功能(制表符完成、向上箭头)和信号处理 ( CTRL + C, CTRL + D) 在上述设置中都能正常工作。
TA贡献1818条经验 获得超8个赞
禁用ECHOCTL终端模式。
modes := ssh.TerminalModes{
ssh.ECHO: 0,
ssh.ECHOCTL: 0,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400
}
- 2 回答
- 0 关注
- 281 浏览
添加回答
举报