【问题标题】:How to simulate a keypress when using ssh and interactive shell使用 ssh 和交互式 shell 时如何模拟按键
【发布时间】:2021-04-11 05:12:15
【问题描述】:

卡在按键上

我正在尝试创建一个从 HP proCurve 交换机进行备份的脚本。为此,我使用包golang.org/x/crypto/ssh

Golang 对我来说并不陌生,我对“Go”有相当多的了解。但是在建立连接后我卡住了。开关要求我按任意键继续,但我不知道如何模拟按键。 (见下图)


当前代码

这是我目前正在使用的代码:

package main

import (
    "bufio"
    "fmt"
    "log"
    "net"
    "os"

    "golang.org/x/crypto/ssh"
)

type password string

func main() {

    host := "192.168.2.43:22"
    user := "admin"
    pass := "admin"

    config := &ssh.ClientConfig{
        User: user,
        Auth: []ssh.AuthMethod{
            ssh.Password(pass),
        },
        Config: ssh.Config{
            KeyExchanges: []string{"diffie-hellman-group-exchange-sha1", "diffie-hellman-group1-sha1"},
        },

        HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
    }
    conn, err := ssh.Dial("tcp", host, config)
    if err != nil {
        panic("Failed to dial: " + err.Error())
    }
    defer conn.Close()

    // Each ClientConn can support multiple interactive sessions,
    // represented by a Session.
    session, err := conn.NewSession()
    if err != nil {
        panic("Failed to create session: " + err.Error())
    }
    defer session.Close()

    // Set IO
    session.Stdout = os.Stdout
    session.Stderr = os.Stderr
    in, _ := session.StdinPipe()

    // Set up terminal modes
    modes := ssh.TerminalModes{
        ssh.ECHO:          0,     // disable echoing
        ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
        ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
    }

    // Request pseudo terminal
    if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
        log.Fatalf("request for pseudo terminal failed: %s", err)
    }

    // Start remote shell
    if err := session.Shell(); err != nil {
        log.Fatalf("failed to start shell: %s", err)
    }

    // Accepting commands
    for {

        reader := bufio.NewReader(os.Stdin)
        str, _ := reader.ReadString('\n')
        fmt.Fprint(in, str)

        // Solutition to keypress to continue

        fmt.Fprint(in, " \n")
        fmt.Fprint(in, "show run \n")

    }

}


我想达到的目标

但是当我手动按下任何键时,脚本将完美地运行所有命令。那么有谁知道我如何在下面的代码中模拟任何键:


    for {

        reader := bufio.NewReader(os.Stdin)
        str, _ := reader.ReadString('\n')
        fmt.Fprint(in, str)

        // Solutition to keypress to continue

        fmt.Fprint(in, " \n")
        fmt.Fprint(in, "show run \n")

    }

【问题讨论】:

    标签: go ssh


    【解决方案1】:

    在写入 ssh 连接之前,您正在从标准输入读取数据。因此,只能通过人工干预跳过此屏幕。

    命令行标准输入的读取应该在您想在主机上运行的任何初始命令之后进行,因此它的组织方式如下:

        // Requires keypress to continue
        fmt.Fprint(in, " \n")
        // List out useful information
        fmt.Fprint(in, "show run \n")
    
        // Forward user commands to the remote shell
        for {
            reader := bufio.NewReader(os.Stdin)
            str, _ := reader.ReadString('\n')
            fmt.Fprint(in, str)
        }
    

    【讨论】:

    • 谢谢!经过更多的解释,现在它是有道理的。它当然有效:)
    猜你喜欢
    • 2011-01-16
    • 2014-11-23
    • 2012-11-23
    • 2011-01-03
    • 2013-03-23
    • 2014-03-30
    • 2013-04-16
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多