golang ssh write backspace to stdin

huangapple go评论84阅读模式
英文:

golang ssh write backspace to stdin

问题

我正在编写一个SSH客户端来解析交换机的配置。这不是一个交互式客户端。
在某些情况下,我需要删除之前输入的命令的一部分。如果我使用PuTTY,我可以通过按退格键或Ctrl-X键盘快捷键来实现。

如何向服务器发送Ctrl-X或退格命令?
我已经尝试发送\b和0x08,但在这种情况下它不起作用。

以下是示例代码(不包含错误处理等):

连接和写入:

c,_ := ssh.Dial( <dial ,_parameters> )
session, _= c.NewSession()

modes := ssh.TerminalModes{
    ssh.ECHO:          0,     // 禁用回显
    ssh.TTY_OP_ISPEED: 38400, // 输入速度 = 14.4kbaud
    ssh.TTY_OP_OSPEED: 38400, // 输出速度 = 14.4kbaud
}

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

stdin, _ = session.StdinPipe()

stdin.Write([]byte( <command> ))

读取:

var buf = make([]byte, 1024)
for {
    n, e := c.stdout.Read(buf)
    if e != nil {
        fmt.Println(e.Error())
        break
    }

    if n != 0 {
        fmt.Print(string(buf[:n]))
    }

    ...
}

编辑:
问题是在显示输出后,命令仍然保持不变,因为我没有向stdin写入"\n"。

例如,如果我发送"display bla ?"并看到以下内容:

<some_switch>display bla ?
bla
bla-bla
...
bla-bla-bla

<some_switch>display bla

如果我想看到其他内容,我需要删除"display bla"。问题是如何删除?

P.S. 对不起,我的英语不好。

英文:

I am writing an ssh client to parse the configuration of a switch. This is not an interactive client.
In some cases, I need to erase part of the previous typed command. If I was using PuTTY, I would do it by pressing the backspace button or the Ctrl-X keyboard shortcuts.

How to send a Ctrl-X or Backspace command to the server?
I've already tried sending \b and 0x08 and in this case it doesn't work as expected.

Below exemplary code without error handling etc...

Dial and write:

 c,_ := ssh.Dial( <dial ,_parameters> )
 session, _= c.NewSession()
 
 modes := ssh.TerminalModes{
		ssh.ECHO:          0,     // disable echoing
		ssh.TTY_OP_ISPEED: 38400, // input speed = 14.4kbaud
		ssh.TTY_OP_OSPEED: 38400, // output speed = 14.4kbaud
	}

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

stdin, _ = session.StdinPipe()

stdin.Write([]byte( <command> ))

Read:

var buf = make([]byte, 1024)
for {
    n, e := c.stdout.Read(buf)
    if e != nil {
        fmt.Println(e.Error())
        break
    }

    if n != 0 {
        fmt.Print(string(buf[:n]))
    }

    ...
}

EDIT:
Problem is after displaying output, command still stay the same because i didn't write "\n" to the stdin.

For instance if i'll send "display bla ?" and i see this

<some_switch>display bla ?
bla
bla-bla
...
bla-bla-bla

<some_switch>display bla

And if i want to see something else i need erase "display bla". The question is HOW?

P.S. Sorry for my english.

答案1

得分: 1

非打印的ASCII符号必须与命令分开发送。如果没有输出,就不需要读取stdout。

//写入Ctrl-X
stdin.Write([]byte{0x18})

//然后发送命令
stdin.Write([]byte())

//写入退格键
stdin.Write([]byte{0x8})

英文:

Non-printing ASCII symbols must be sending separately from the command. No need read stdout if there is no output.

//To write Ctrl-X
stdin.Write([]byte{0x18})

//Then send the command
stdin.Write([]byte(<command>))

//To write Backspace
stdin.Write([]byte{0x8})

huangapple
  • 本文由 发表于 2022年6月23日 18:09:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/72728419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定