Golang正确处理SSH会话的标准输出

时间:2018-01-16 02:25:45

标签: go ssh

我正在编写一个Go程序来连接思科无线控制器,以便在连接的接入点上运行多个配置命令。我可以通过Go的SSH包golang.com/x/crypto/ssh连接并向控制器发送命令。

我需要 a)只打印我发送的每个命令的输出,而不是控制器的提示或登录提示, b)弄清楚如何发送连续的命令在输出包含--More-- or (q)uit提示的命令之后。

现在我已将os.Stdout分配给SSH session.Stdout,这就是为什么我打印的不仅仅是我发送的命令的输出。如何手动控制session.Stdout的输出?我知道session.Stdoutio.Writer,但我不知道如何控制它写入os.Stdout的内容。这是我的代码及其输出:

package main

import (
    "bufio"
    "fmt"
    "golang.org/x/crypto/ssh"
    "golang.org/x/crypto/ssh/terminal"
    "log"
    "os"
    "time"
    "io"
)

func SendCommand(in io.WriteCloser, cmd string) error {
    if _, err := in.Write([]byte(cmd + "\n")); err != nil {
        return err
    }

    return nil
}

func main() {
    // Prompt for Username
    fmt.Print("Username: ")
    r := bufio.NewReader(os.Stdin)
    username, err := r.ReadString('\n')
    if err != nil {
        log.Fatal(err)
    }

    // Prompt for password
    fmt.Print("Password: ")
    password, err := terminal.ReadPassword(int(os.Stdin.Fd()))
    if err != nil {
        log.Fatal(err)
    }

    // Setup configuration for SSH client
    config := &ssh.ClientConfig{
        Timeout: time.Second * 5,
        User:    username,
        Auth: []ssh.AuthMethod{
            ssh.Password(string(password)),
        },
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    }

    // Connect to the client
    client, err := ssh.Dial("tcp", "host:22", config)
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    // Create a session
    session, err := client.NewSession()
    if err != nil {
        log.Fatal(err)
    }
    defer session.Close()

    // Setup StdinPipe to send commands
    stdin, err := session.StdinPipe()
    if err != nil {
        log.Fatal(err)
    }
    defer stdin.Close()

    // Route session Stdout/Stderr to system Stdout/Stderr
    session.Stdout = os.Stdout
    session.Stderr = os.Stderr

    // Start a shell
    if err := session.Shell(); err != nil {
        log.Fatal(err)
    }

    // Send username
    if _, err := stdin.Write([]byte(username)); err != nil {
        log.Fatal(err)
    }
    // Send password
    SendCommand(stdin, string(password))

    // Run configuration commands
    SendCommand(stdin, "show ap summary")
    SendCommand(stdin, "logout")
    SendCommand(stdin, "N")

    session.Wait()
}

示例运行:

$ go run main.go 
Username: username
Password: 

(Cisco Controller) 
User: username
Password:****************
(Cisco Controller) >show ap summary

Number of APs.................................... 1

Global AP User Name.............................. its-wap
Global AP Dot1x User Name........................ Not Configured

AP Name             Slots  AP Model              Ethernet MAC       Location          Country     IP Address       Clients   DSE Location  
------------------  -----  --------------------  -----------------  ----------------  ----------  ---------------  --------  --------------
csc-ap3500   2     AIR-CAP3502I-A-K9     00:00:00:00:00:00  csc-TESTLAB-ap35  US          10.10.110.10       0       [0 ,0 ,0 ]

(Cisco Controller) >logout
The system has unsaved changes.
Would you like to save them now? (y/N) N

当命令的输出有一个或多个--More-- or (q)uit提示并且我尝试发送连续命令时,我的下一个问题就出现了。例如,以下是运行show sysinfo后跟logout

时代码的输出
$ go run main.go 
Username: username
Password: 

(Cisco Controller) 
User: username
Password:****************
(Cisco Controller) >show sysinfo

Manufacturer's Name.............................. Cisco Systems Inc.
Product Name..................................... Cisco Controller
Product Version.................................. 8.2.161.0
Bootloader Version............................... 1.0.20
Field Recovery Image Version..................... 7.6.101.1
Firmware Version................................. PIC 16.0


Build Type....................................... DATA + WPS

--More-- or (q)uit

Configured Country............................... US  - United States
Operating Environment............................ Commercial (0 to 40 C)
Internal Temp Alarm Limits....................... 0 to 65 C
Internal Temperature............................. +23 C
External Temperature............................. +28 C
Fan Status....................................... 3900 rpm
# the "l" is missing in "logout"
(Cisco Controller) >ogout

Incorrect usage.  Use the '?' or <TAB> key to list commands.

(Cisco Controller) >N

Incorrect usage.  Use the '?' or <TAB> key to list commands.
# the program hangs here
(Cisco Controller) > 

TL; DR 如何手动控制io.Writer的输出以及如何正确处理连续发送命令?

感谢任何帮助。提前谢谢。

0 个答案:

没有答案