Go的SSH开发包 gossh

码农软件 · 软件分类 · 网络工具包 · 2019-02-27 17:26:52

软件介绍

gossh 是 Go 语言实现 SSH 通讯的开发包。

示例代码:

package main

import (
  "github.com/dynport/gossh"
  "log"
)

// returns a function of type gossh.Writer func(...interface{})
// MakeLogger just adds a prefix (DEBUG, INFO, ERROR)
func MakeLogger(prefix string) gossh.Writer {
  return func(args ...interface{}) {
    log.Println((append([]interface{}{prefix}, args...))...)
  }
}

func main() {
  client := gossh.New("some.host", "user")
  // my default agent authentication is used. use
  // client.SetPassword("<secret>")
  // for password authentication
  client.DebugWriter = MakeLogger("DEBUG")
  client.InfoWriter = MakeLogger("INFO ")
  client.ErrorWriter = MakeLogger("ERROR")

  defer client.Close()
  rsp, e := client.Execute("uptime")
  if e != nil {
    client.ErrorWriter(e.Error())
  }
  client.InfoWriter(rsp.String())

  rsp, e = client.Execute("echo -n $(cat /proc/loadavg); cat /does/not/exists")
  if e != nil {
    client.ErrorWriter(e.Error())
    client.ErrorWriter("STDOUT: " + rsp.Stdout())
    client.ErrorWriter("STDERR: " + rsp.Stderr())
  }
}
运行结果:
2013/08/25 00:31:40 DEBUG connecting some.host
2013/08/25 00:31:41 INFO  [EXEC  ] uptime
2013/08/25 00:31:41 DEBUG 22:31:41 up 375 days, 10:44,  0 users,  load average: 0.09, 0.13, 0.22
2013/08/25 00:31:41 INFO  => 0.944143
2013/08/25 00:31:41 INFO  map[stdout:72 bytes stderr:0 bytes runtime:0.944202 status:0]
2013/08/25 00:31:41 DEBUG already connected
2013/08/25 00:31:41 INFO  [EXEC  ] echo -n $(cat /proc/loadavg); cat /does/not/exists
2013/08/25 00:31:41 DEBUG 0.09 0.13 0.22 1/455 23396
2013/08/25 00:31:41 ERROR cat: /does/not/exists
2013/08/25 00:31:41 ERROR : No such file or directory
2013/08/25 00:31:41 INFO  => 0.067075
2013/08/25 00:31:41 ERROR Process exited with: 1. Reason was:  ()
2013/08/25 00:31:41 ERROR STDOUT: 0.09 0.13 0.22 1/455 23396
2013/08/25 00:31:41 ERROR STDERR: cat: /does/not/exists: No such file or directory

本文地址:https://www.codercto.com/soft/d/262.html

The Practice of Programming

The Practice of Programming

Brian W. Kernighan、Rob Pike / Addison-Wesley / 1999-2-14 / USD 49.99

With the same insight and authority that made their book The Unix Programming Environment a classic, Brian Kernighan and Rob Pike have written The Practice of Programming to help make individual progr......一起来看看 《The Practice of Programming》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具