英文:
Run multiple commands on Cisco using golang crypto/ssh
问题
我有以下拼凑的代码,可以让我发送读写到Cisco设备的ssh控制台,但我目前不确定如何在同一个会话中发送多个命令。
我包含了整个程序,以便作为其他人在将来尝试相同事情的起点,我找不到一个特别适用于Golang<->Cisco的工作示例。
package main
import (
"fmt"
"io"
"io/ioutil"
"net"
"os"
//"strings"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
type SSHCommand struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
type SSHClient struct {
Config *ssh.ClientConfig
Host string
Port int
}
func (client *SSHClient) RunCommand(cmd *SSHCommand) error {
var (
session *ssh.Session
err error
)
if session, err = client.newSession(); err != nil {
return err
}
defer session.Close()
if err = client.prepareCommand(session, cmd); err != nil {
return err
}
err = session.Run("en\r")
return err
}
func (client *SSHClient) prepareCommand(session *ssh.Session, cmd *SSHCommand) error {
if cmd.Stdin != nil {
stdin, err := session.StdinPipe()
if err != nil {
return fmt.Errorf("Unable to setup stdin for session: %v", err)
}
go io.Copy(stdin, cmd.Stdin)
}
if cmd.Stdout != nil {
stdout, err := session.StdoutPipe()
if err != nil {
return fmt.Errorf("Unable to setup stdout for session: %v", err)
}
go io.Copy(cmd.Stdout, stdout)
}
if cmd.Stderr != nil {
stderr, err := session.StderrPipe()
if err != nil {
return fmt.Errorf("Unable to setup stderr for session: %v", err)
}
go io.Copy(cmd.Stderr, stderr)
}
return nil
}
func (client *SSHClient) newSession() (*ssh.Session, error) {
connection, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", client.Host, client.Port), client.Config)
if err != nil {
return nil, fmt.Errorf("Failed to dial: %s", err)
}
session, err := connection.NewSession()
if err != nil {
return nil, fmt.Errorf("Failed to create session: %s", err)
}
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
}
if err := session.RequestPty("xterm", 0, 200, modes); err != nil {
session.Close()
return nil, fmt.Errorf("request for pseudo terminal failed: %s", err)
}
return session, nil
}
func SSHAgent() ssh.AuthMethod {
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
}
return nil
}
func main() {
sshConfig := &ssh.ClientConfig{
User: "admin",
Auth: []ssh.AuthMethod{
ssh.Password("password"),
},
}
sshConfig.Ciphers = []string{"aes128-cbc"}
client := &SSHClient{
Config: sshConfig,
Host: "10.10.10.10",
Port: 22,
}
cmd := &SSHCommand{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
fmt.Printf("Running commands\n")
if err := client.RunCommand(cmd); err != nil {
fmt.Fprintf(os.Stderr, "command run error: %s\n", err)
os.Exit(1)
}
}
如你所见,我在session.Run()
中硬编码了"en"命令。如果我简单地在此之后添加一个命令:
err = session.Run("en\r")
err = session.Run("password\r")
它会被忽略。阅读https://godoc.org/golang.org/x/crypto/ssh#Session.Shell 告诉我这是设计如此。所以我的问题是:如何设置一个让我输入命令、读取输出、解析输出并根据输出输入更多命令的东西?
一旦我理解了这个问题,我计划解析路由表并根据需要发出命令。例如,如果路由X丢失,则添加它。
英文:
I have the following code pieced together that will let me send read and write to the ssh console of a Cisco box but I am currently not sure how to send multiple commands in the same session.
I am including the entire program in order to server as a starting point for others attempting the same thing in the future, I could not find a working example specifically for Golang<->Cisco.
package main
import (
"fmt"
"io"
"io/ioutil"
"net"
"os"
//"strings"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
type SSHCommand struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
type SSHClient struct {
Config *ssh.ClientConfig
Host string
Port int
}
func (client *SSHClient) RunCommand(cmd *SSHCommand) error {
var (
session *ssh.Session
err error
)
if session, err = client.newSession(); err != nil {
return err
}
defer session.Close()
if err = client.prepareCommand(session, cmd); err != nil {
return err
}
err = session.Run("en\r")
return err
}
func (client *SSHClient) prepareCommand(session *ssh.Session, cmd *SSHCommand) error {
if cmd.Stdin != nil {
stdin, err := session.StdinPipe()
if err != nil {
return fmt.Errorf("Unable to setup stdin for session: %v", err)
}
go io.Copy(stdin, cmd.Stdin)
}
if cmd.Stdout != nil {
stdout, err := session.StdoutPipe()
if err != nil {
return fmt.Errorf("Unable to setup stdout for session: %v", err)
}
go io.Copy(cmd.Stdout, stdout)
}
if cmd.Stderr != nil {
stderr, err := session.StderrPipe()
if err != nil {
return fmt.Errorf("Unable to setup stderr for session: %v", err)
}
go io.Copy(cmd.Stderr, stderr)
}
return nil
}
func (client *SSHClient) newSession() (*ssh.Session, error) {
connection, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", client.Host, client.Port), client.Config)
if err != nil {
return nil, fmt.Errorf("Failed to dial: %s", err)
}
session, err := connection.NewSession()
if err != nil {
return nil, fmt.Errorf("Failed to create session: %s", err)
}
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
}
if err := session.RequestPty("xterm", 0, 200, modes); err != nil {
session.Close()
return nil, fmt.Errorf("request for pseudo terminal failed: %s", err)
}
return session, nil
}
func SSHAgent() ssh.AuthMethod {
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
}
return nil
}
func main() {
sshConfig := &ssh.ClientConfig{
User: "admin",
Auth: []ssh.AuthMethod{
ssh.Password("password"),
},
}
sshConfig.Ciphers = []string{"aes128-cbc"}
client := &SSHClient{
Config: sshConfig,
Host: "10.10.10.10",
Port: 22,
}
cmd := &SSHCommand{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
fmt.Printf("Running commands\n")
if err := client.RunCommand(cmd); err != nil {
fmt.Fprintf(os.Stderr, "command run error: %s\n", err)
os.Exit(1)
}
}
As you can see I have hard coded the "en" command in session.Run().
If I add a command after that by simply doing:
err = session.Run("en\r")
err = session.Run("password\r")
It gets ignored. Reading https://godoc.org/golang.org/x/crypto/ssh#Session.Shell tells me that this is by design. So my question is : How to set up something that lets me enter commands, read output, parse it, and enter more commands based on that output?
Once I wrap my head around this, I plan on parsing routing tables and issuing commands in response. IE, if route X is missing, add it.
答案1
得分: 1
这可能不是典型的Go语言写法,我还在学习中。我一段时间前写了这个代码来从交换机中获取备份,但我认为它可以实现你所要求的功能。以下是代码的链接:
https://github.com/plod/goSwitchBackup/blob/master/main.go
func writeBuff(command string, sshIn io.WriteCloser) (int, error) {
returnCode, err := sshIn.Write([]byte(command + "\r"))
return returnCode, err
}
这段代码的作用是向sshIn
写入命令字符串,并返回写入的字节数和可能出现的错误。
英文:
This might not be idiomatic go, I am still learning. I wrote this a while ago to get backups off switches but I think it does what you are asking how to do:
https://github.com/plod/goSwitchBackup/blob/master/main.go
func writeBuff(command string, sshIn io.WriteCloser) (int, error) {
returnCode, err := sshIn.Write([]byte(command + "\r"))
return returnCode, err
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论