从输出中排除SSH命令执行状态。

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

Exclude SSH Command Execution Status from Output

问题

我正在尝试使用Go语言开发一个SSH客户端。我编写了下面的代码来获取用户的命令,执行它们在远程服务器上,并打印响应。

下面的代码有一个小问题。在屏幕上打印输出时,会在响应的末尾打印命令执行状态(nil/错误状态)。我该如何从输出中排除这部分内容?

SSH.go

  1. package main
  2. import "fmt"
  3. import "io"
  4. import "bufio"
  5. import "os"
  6. import "net"
  7. import "golang.org/x/crypto/ssh"
  8. func main(){
  9. sshConfig := &ssh.ClientConfig{
  10. User: "[USERNAME]",
  11. Auth: []ssh.AuthMethod{
  12. ssh.Password("[PASSWORD]"),
  13. },
  14. HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
  15. return nil
  16. },
  17. }
  18. connection,err := ssh.Dial("tcp", "[IP]:[PORT]", sshConfig)
  19. if err != nil {
  20. fmt.Println("Failed to connect: %s", err)
  21. }
  22. reader := bufio.NewReader(os.Stdin)
  23. for{
  24. input, _ := reader.ReadString('\n')
  25. session,err := connection.NewSession()
  26. if err != nil {
  27. fmt.Println("Failed to create session: %s", err)
  28. }
  29. stdout,err := session.StdoutPipe()
  30. if err != nil {
  31. fmt.Println("Failed to get stdout: %s", err)
  32. }
  33. go io.Copy(os.Stdout, stdout)
  34. output := session.Run(input);
  35. fmt.Println(output)
  36. }
  37. }

当前结果

  1. hello
  2. Process exited with status 127
  3. df -hP /tmp
  4. Filesystem Size Used Avail Use% Mounted on
  5. /dev/sda6 198G 13G 176G 7% /
  6. <nil>

期望结果

  1. hello
  2. df -hP /tmp
  3. Filesystem Size Used Avail Use% Mounted on
  4. /dev/sda6 198G 13G 176G 7% /
英文:

I am trying to develop a SSH client using Go language. I've written the below code to get commands from user, execute them on the remote server and print the response.

There's a small problem with the below code. Printing output on the screen prints the command execution status(nil/error status) at the end of response. How do I exclude this from my output?

SSH.go

  1. package main
  2. import &quot;fmt&quot;
  3. import &quot;io&quot;
  4. import &quot;bufio&quot;
  5. import &quot;os&quot;
  6. import &quot;net&quot;
  7. import &quot;golang.org/x/crypto/ssh&quot;
  8. func main(){
  9. sshConfig := &amp;ssh.ClientConfig{
  10. User: &quot;[USERNAME]&quot;,
  11. Auth: []ssh.AuthMethod{
  12. ssh.Password(&quot;[PASSWORD]&quot;),
  13. },
  14. HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
  15. return nil
  16. },
  17. }
  18. connection,err := ssh.Dial(&quot;tcp&quot;, &quot;[IP]:[PORT]&quot;, sshConfig)
  19. if err != nil {
  20. fmt.Println(&quot;Failed to connect: %s&quot;, err)
  21. }
  22. reader := bufio.NewReader(os.Stdin)
  23. for{
  24. input, _ := reader.ReadString(&#39;\n&#39;)
  25. session,err := connection.NewSession()
  26. if err != nil {
  27. fmt.Println(&quot;Failed to create session: %s&quot;, err)
  28. }
  29. stdout,err := session.StdoutPipe()
  30. if err != nil {
  31. fmt.Println(&quot;Failed to get stdout: %s&quot;, err)
  32. }
  33. go io.Copy(os.Stdout, stdout)
  34. output := session.Run(input);
  35. fmt.Println(output)
  36. }
  37. }

Current Result

  1. hello
  2. Process exited with status 127
  3. df -hP /tmp
  4. Filesystem Size Used Avail Use% Mounted on
  5. /dev/sda6 198G 13G 176G 7% /
  6. &lt;nil&gt;

Expected Result

  1. hello
  2. df -hP /tmp
  3. Filesystem Size Used Avail Use% Mounted on
  4. /dev/sda6 198G 13G 176G 7% /

答案1

得分: 1

上面打印的&lt;nil&gt;session.Run的输出,它返回一个错误(根据文档)。命令的输出已经通过以下goroutine显示在控制台上:

  1. go io.Copy(os.Stdout, stdout)

你不需要打印output,因为当session.Run调用完成时,outputnil,表示没有错误发生。更好的做法是检查错误:

  1. // ...
  2. if err := session.Run(input); err != nil {
  3. // 处理错误
  4. }
  5. // ...
  6. [1]: https://godoc.org/golang.org/x/crypto/ssh#Session.Run
英文:

The &lt;nil&gt; pinted above is the output of session.Run, which returns an error (as per docs).

The output of the command is already being displayed on your console by the following goroutine:

  1. go io.Copy(os.Stdout, stdout)

You don't need to print the output, which is nil since no error occurs when session.Run is call complete. A better thing to do would be to check the error:

  1. // ...
  2. if err := session.Run(input); err != nil {
  3. // handle error
  4. }
  5. // ...

huangapple
  • 本文由 发表于 2017年8月9日 13:59:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/45582678.html
匿名

发表评论

匿名网友

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

确定