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

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

Exclude SSH Command Execution Status from Output

问题

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

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

SSH.go

package main

import "fmt"
import "io"
import "bufio"
import "os"
import "net"
import "golang.org/x/crypto/ssh"

func main(){

	sshConfig := &ssh.ClientConfig{
		User: "[USERNAME]",
		Auth: []ssh.AuthMethod{
			ssh.Password("[PASSWORD]"),
		},
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
			return nil
		},
	}

	connection,err := ssh.Dial("tcp", "[IP]:[PORT]", sshConfig)
	if err != nil {
		fmt.Println("Failed to connect: %s", err)
	}

	reader := bufio.NewReader(os.Stdin)

	for{
		input, _ := reader.ReadString('\n')
		session,err := connection.NewSession()
		if err != nil {
			fmt.Println("Failed to create session: %s", err)
		}
		stdout,err := session.StdoutPipe()
		if err != nil {
			fmt.Println("Failed to get stdout: %s", err)
		}
		go io.Copy(os.Stdout, stdout)
		output := session.Run(input);
		fmt.Println(output)
	}

}

当前结果

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

期望结果

hello
df -hP /tmp
Filesystem      Size  Used Avail Use% Mounted on
/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

package main

import &quot;fmt&quot;
import &quot;io&quot;
import &quot;bufio&quot;
import &quot;os&quot;
import &quot;net&quot;
import &quot;golang.org/x/crypto/ssh&quot;

func main(){

	sshConfig := &amp;ssh.ClientConfig{
		User: &quot;[USERNAME]&quot;,
		Auth: []ssh.AuthMethod{
			ssh.Password(&quot;[PASSWORD]&quot;),
		},
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
			return nil
		},
	}

	connection,err := ssh.Dial(&quot;tcp&quot;, &quot;[IP]:[PORT]&quot;, sshConfig)
	if err != nil {
		fmt.Println(&quot;Failed to connect: %s&quot;, err)
	}

	reader := bufio.NewReader(os.Stdin)

	for{
		input, _ := reader.ReadString(&#39;\n&#39;)
		session,err := connection.NewSession()
		if err != nil {
			fmt.Println(&quot;Failed to create session: %s&quot;, err)
		}
		stdout,err := session.StdoutPipe()
		if err != nil {
			fmt.Println(&quot;Failed to get stdout: %s&quot;, err)
		}
		go io.Copy(os.Stdout, stdout)
		output := session.Run(input);
		fmt.Println(output)
	}

}

Current Result

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

Expected Result

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

答案1

得分: 1

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

go io.Copy(os.Stdout, stdout)

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

// ...
if err := session.Run(input); err != nil {
  // 处理错误
}
// ...

[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:

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:

// ...
if err := session.Run(input); err != nil {
  // handle error
}
// ...

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:

确定