英文:
Golang, multiple commands in the same SSH Session
问题
我需要在同一个SSH会话中运行多个命令。在每个命令之间,我需要解析输出。
我发现我可以从标准输出(STDOUT)中打印出第一行,但是当我尝试打印所有内容时,它就会卡住,似乎在等待STDOUT。这对于打印第一行是有效的,但我无法弄清楚如何打印所有来自STDOUT的内容,并释放回程序以运行下一个命令。
我需要运行一个命令,解析输出,然后在同一个会话中运行第二个命令。
以下是我的代码示例:
package main
import (
"bufio"
"fmt"
"log"
"golang.org/x/crypto/ssh"
)
func main() {
// SSH凭证
sshConfig := &ssh.ClientConfig{
User: "rob",
Auth: []ssh.AuthMethod{
ssh.Password("OMGAPASSWORD....EEEEEEE"),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// 连接到SSH服务器
client, err := ssh.Dial("tcp", "*************:22", sshConfig)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// 打开一个新会话
session, err := client.NewSession()
if err != nil {
log.Fatal(err)
}
defer session.Close()
// 设置输入和输出流
sshIn, _ := session.StdinPipe()
sshOut, _ := session.StdoutPipe()
//sshErr, _ := session.StderrPipe()
// 启动SSH会话
if err := session.Start("/bin/bash"); err != nil {
log.Fatalf("Failed to start SSH session: %v", err)
}
// 发送第一个命令
cmd1 := "ls -al"
fmt.Fprintf(sshIn, "%s\n", cmd1)
lines := make(chan string)
go func() {
scanner := bufio.NewScanner(sshOut)
for scanner.Scan() {
lines <- scanner.Text()
}
close(lines)
}()
for line := range lines {
fmt.Println(line)
}
}
希望对你有所帮助!
英文:
I need to run multiple commands in the same SSH Session. Between each command I'm sending. I need to parse the output.
I found I can printout the first line from STDOUT, but when I try and print everything it just hangs, seeming waiting for STDOUT. This works for printing the first line, I can't figure out how to print all from STDOUT and release back to the program to run the next command.
I need to run a command, parse the output, then run a second command within the same session.
Example of my code.
package main
import (
"bufio"
"fmt"
"log"
"golang.org/x/crypto/ssh"
)
func main() {
// SSH credentials
sshConfig := &ssh.ClientConfig{
User: "rob",
Auth: []ssh.AuthMethod{
ssh.Password("OMGAPASSWORD....EEEEEEE"),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// Connect to SSH server
client, err := ssh.Dial("tcp", "*************:22", sshConfig)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Open a new session
session, err := client.NewSession()
if err != nil {
log.Fatal(err)
}
defer session.Close()
// Set up input and output streams
sshIn, _ := session.StdinPipe()
sshOut, _ := session.StdoutPipe()
//sshErr, _ := session.StderrPipe()
// Start the SSH session
if err := session.Start("/bin/bash"); err != nil {
log.Fatalf("Failed to start SSH session: %v", err)
}
// Send the first command
cmd1 := "ls -al"
fmt.Fprintf(sshIn, "%s\n", cmd1)
lines := make(chan string)
go func() {
scanner := bufio.NewScanner(sshOut)
for scanner.Scan() {
lines <- scanner.Text()
}
close(lines)
}()
for line := range lines {
fmt.Println(line)
}
}
答案1
得分: 2
session.Start("/bin/bash")
尝试通过持久化的 shell 运行连续的命令是你所有困难的根源。由于你正在读取和写入 bash,你无法轻松地知道每个命令是否成功。
相反,每个命令都启动一个新的会话。将 stdout 和 stderr 复制到缓冲区,然后你可以对它们进行任何操作。
for _, cmd := range []string{"ls -al", `bc <<< "3/2.0"`} {
session, err := client.NewSession()
if err != nil {
log.Fatal(err)
}
var stdout, stderr bytes.Buffer
// 设置输入和输出流
//sshIn, _ := session.StdinPipe()
sshOut, _ := session.StdoutPipe()
sshErr, _ := session.StderrPipe()
go io.Copy(&stdout, sshOut)
go io.Copy(&stderr, sshErr)
// 启动命令
if err := session.Start(cmd); err != nil {
log.Fatalf("Failed to start SSH session: %v", err)
}
if err := session.Wait(); err != nil {
log.Fatal(fmt.Errorf("error waiting: %w", err))
}
session.Close()
fmt.Printf("Command: \t%s\nStdout: \t%s\nStderr:%s\n", cmd, stdout.String(), stderr.String())
}
这样你就知道每个连续命令何时结束以及它们的输出是什么。
如果你在 ~/.ssh
中有一个 ssh 密钥对,完整示例 应该在本地工作,但在 playground 中不起作用,因为没有 SSH 服务器可以连接(或密钥对)。可以像这样启动本地 ssh 服务器:
docker run --rm -d -e PUBLIC_KEY="$(cat ~/.ssh/id_rsa.pub)" -e USER_NAME=$(whoami) -p 2222:2222 linuxserver/openssh-server
<details>
<summary>英文:</summary>
session.Start("/bin/bash")
Trying to run sequential commands through a persistent shell is the root of all your difficulties. Since bash is what you're reading to and writing from, you can't easily tell when each command is successful.
Instead, start a new session with each command. Copy stdout and stderr to buffers, then you can do whatever you want with them.
for _, cmd := range []string{"ls -al", `bc <<<"3/2.0"`} {
session, err := client.NewSession()
if err != nil {
log.Fatal(err)
}
var stdout, stderr bytes.Buffer
// Set up input and output streams
//sshIn, _ := session.StdinPipe()
sshOut, _ := session.StdoutPipe()
sshErr, _ := session.StderrPipe()
go io.Copy(&stdout, sshOut)
go io.Copy(&stderr, sshErr)
// Start the command
if err := session.Start(cmd); err != nil {
log.Fatalf("Failed to start SSH session: %v", err)
}
if err := session.Wait(); err != nil {
log.Fatal(fmt.Errorf("error waiting: %w", err))
}
session.Close()
fmt.Printf("Command: \t%s\nStdout: \t%s\nStderr:%s\n", cmd, stdout.String(), stderr.String())
}
This way you know when each sequential command ends and whose output is which.
[Full example][1] should work locally if you have an ssh key pair in `~/.ssh`, but doesn't work in playground because there's no SSh server to connect to (or key pair). Start a local ssh server like this:
docker run --rm -d -e PUBLIC_KEY="$(cat ~/.ssh/id_rsa.pub)" -e USER_NAME=$(whoami) -p 2222:2222 linuxserver/openssh-server
[1]: https://go.dev/play/p/B2E4OD6uIie
</details>
# 答案2
**得分**: 1
主要缺少的是在最后的循环块中,当最终的循环被阻塞时,你需要再次从键盘读取,这样你就可以向`stdin`发送更多的命令。
类似这样的代码:
```go
// 你的代码的最后一个块
for {
fmt.Println("$")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
text := scanner.Text()
wr <- []byte(text + "\n")
}
另一个重要的部分缺失是保持从上面的操作系统扫描器接收命令并将其写入stdin
的goroutine。
// 你需要创建一个goroutine来写入stdin
go func() {
for {
select {
case d := <-wr:
_, err := stdin.Write(d)
if err != nil {
fmt.Println(err.Error())
}
}
}
}()
这两个代码块应该允许你连续输入命令。
基本上你需要:
- 一个写入SSH stdin的goroutine
- 一个从SSH stdout读取的goroutine
- 一个从SSH stderr读取的goroutine
- 一个循环来阻塞并从
os.Stdin
读取新的命令
我找到了一个功能性的代码片段的例子,你可以复制并运行它作为起点。
英文:
The main thing missing is that you need to read again from the keyboard when your final loop blocks so you can send more commands to stdin
.
Something like this:
// Last block of your code
for {
fmt.Println("$")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
text := scanner.Text()
wr <- []byte(text + "\n")
}
Another important part missing is the goroutine that will keep receiving commands from the OS scanner above, and will write that to the stdin
.
// You need to create a gorouting to write to stdin
go func() {
for {
select {
case d := <-wr:
_, err := stdin.Write(d)
if err != nil {
fmt.Println(err.Error())
}
}
}
}()
These two blocks should allow you to enter commands one after another.
Basically you'll need:
- goroutine that writes to SSH stdin
- goroutine that reads from SSH stdout
- goroutine that reads from SSH stderr
- A loop to block and read new commands from the
os.Stdin
I've found this functional gist example that you can copy and run to use as a starting point.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论