英文:
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 := &ssh.ClientConfig{
User: USERNAME,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(pem),
},
}
// Connect to the machine
client, err := ssh.Dial("tcp", HOSTNAME + ":" + PORT, config)
if err != nil {
panic("Failed to dial: " + err.Error())
}
// Create a session
session, err := client.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session.Close()
// Start running commands!
var output bytes.Buffer
session.Stdout = &output
// 1) Login to swarm registry
fmt.Println("Logging into swarm registry...")
if err := session.Run("docker login ..."); err != nil {
panic("Failed to login to swarm registry: " + err.Error())
}
fmt.Println(output.String())
// 2) List all of the docker processes
fmt.Println("List swarm processes...")
if err := session.Run("docker ps"); err != nil { // <-------- FAILS HERE
panic("Failed to list swarm processes: " + 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{
"docker login",
"docker ps",
}
command := strings.Join(commands, "; ")
And then running it the same as before:
if err := session.Run(command); err != nil {
panic("Failed to run command: " + command + "\nBecause: " + err.Error())
}
fmt.Println(output.String())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论