登录
首页 >  Golang >  Go问答

模拟按键操作在 ssh 和交互式 shell 中的实现

来源:stackoverflow

时间:2024-02-24 16:36:25 143浏览 收藏

Golang不知道大家是否熟悉?今天我将给大家介绍《模拟按键操作在 ssh 和交互式 shell 中的实现》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

问题内容

卡在按键上

我正在尝试创建一个脚本来从 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")

    }


正确答案


在写入 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)
    }

今天关于《模拟按键操作在 ssh 和交互式 shell 中的实现》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>