英文:
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 "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)
}
}
Current Result
hello
Process exited with status 127
df -hP /tmp
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 198G 13G 176G 7% /
<nil>
Expected Result
hello
df -hP /tmp
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 198G 13G 176G 7% /
答案1
得分: 1
上面打印的<nil>
是session.Run
的输出,它返回一个错误(根据文档)。命令的输出已经通过以下goroutine显示在控制台上:
go io.Copy(os.Stdout, stdout)
你不需要打印output
,因为当session.Run
调用完成时,output
是nil
,表示没有错误发生。更好的做法是检查错误:
// ...
if err := session.Run(input); err != nil {
// 处理错误
}
// ...
[1]: https://godoc.org/golang.org/x/crypto/ssh#Session.Run
英文:
The <nil>
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
}
// ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论