在一个SSH会话中运行多个命令

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

Running multiple commands in one SSH session

问题

我需要在单个ssh会话中运行多个命令:

// 定义客户端配置
config := &ssh.ClientConfig{
User: USERNAME,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(pem),
},
}

// 连接到机器
client, err := ssh.Dial("tcp", HOSTNAME + ":" + PORT, config)
if err != nil {
panic("连接失败:" + err.Error())
}

// 创建一个会话
session, err := client.NewSession()
if err != nil {
panic("创建会话失败:" + err.Error())
}
defer session.Close()

// 开始运行命令!
var output bytes.Buffer
session.Stdout = &output

// 1) 登录到swarm注册表
fmt.Println("登录到swarm注册表...")
if err := session.Run("docker login ..."); err != nil {
panic("登录到swarm注册表失败:" + err.Error())
}
fmt.Println(output.String())

// 2) 列出所有docker进程
fmt.Println("列出swarm进程...")
if err := session.Run("docker ps"); err != nil { // <-------- 在这里失败
panic("列出swarm进程失败:" + err.Error())
}
fmt.Println(output.String())

我阅读了源文件(session.go)中的Session.Run命令,它说:

一个会话只接受一次对Run、Start、Shell、Output或CombinedOutput的调用。

根据我的用例,我需要发出第一个命令来登录会话,然后在登录后发出后续命令。

是否有其他方法可以在同一个ssh会话中运行多个命令?

英文:

I need to run multiple commands in a single ssh session:

// Define the client configuration
config := &amp;ssh.ClientConfig{
    User: USERNAME,
    Auth: []ssh.AuthMethod{
        ssh.PublicKeys(pem),
    },
}

// Connect to the machine
 client, err := ssh.Dial(&quot;tcp&quot;, HOSTNAME + &quot;:&quot; + PORT, config)
 if err != nil {
    panic(&quot;Failed to dial: &quot; + err.Error())
}

// Create a session
session, err := client.NewSession()
if err != nil {
    panic(&quot;Failed to create session: &quot; + err.Error())
}
defer session.Close()

// Start running commands!
var output bytes.Buffer
session.Stdout = &amp;output

// 1)  Login to swarm registry
fmt.Println(&quot;Logging into swarm registry...&quot;)
if err := session.Run(&quot;docker login ...&quot;); err != nil {
    panic(&quot;Failed to login to swarm registry: &quot; + err.Error())
}
fmt.Println(output.String())

// 2)  List all of the docker processes
fmt.Println(&quot;List swarm processes...&quot;)
if err := session.Run(&quot;docker ps&quot;); err != nil {    // &lt;-------- FAILS HERE
    panic(&quot;Failed to list swarm processes: &quot; + err.Error())
}
fmt.Println(output.String())

I read through the source file (session.go) and for the Session.Run command and it says:

> A Session only accepts one call to Run, Start, Shell, Output, or CombinedOutput.

For my use case I need to issue the first command to log the session in, and then issue subsequent commands once I am logged in.

Is there an alternate way to run multiple commands using the same ssh session?

答案1

得分: 3

感谢 @JimB,现在我改为这样做:

// 创建一个以分号分隔的单个命令
commands := []string{
    "docker login",
    "docker ps",
}
command := strings.Join(commands, "; ")

然后像以前一样运行它:

if err := session.Run(command); err != nil {
    panic("运行命令失败:" + command + "\n原因:" + err.Error())
}
fmt.Println(output.String())
英文:

Thanks to @JimB I am now doing this instead:

// Create a single command that is semicolon seperated
commands := []string{
	&quot;docker login&quot;,
	&quot;docker ps&quot;,
}
command := strings.Join(commands, &quot;; &quot;)

And then running it the same as before:

if err := session.Run(command); err != nil {
    panic(&quot;Failed to run command: &quot; + command + &quot;\nBecause: &quot; + err.Error())
}
fmt.Println(output.String())

huangapple
  • 本文由 发表于 2016年11月19日 02:52:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/40684160.html
匿名

发表评论

匿名网友

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

确定