updated readme with examples
This commit is contained in:
parent
b4b856a17a
commit
a9e6dd5ffc
119
README.md
119
README.md
@ -1,4 +1,123 @@
|
||||
sshrpc
|
||||
======
|
||||
|
||||
[![GoDoc](https://godoc.org/dev.justinjudd.org/justin/sshrpc?status.svg)](https://godoc.org/dev.justinjudd.org/justin/sshrpc)
|
||||
|
||||
|
||||
Package sshrpc provides rpc access over ssh.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
go get dev.justinjudd.org/justin/sshrpc
|
||||
|
||||
## Usage
|
||||
|
||||
### Server Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/rpc"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
|
||||
"dev.justinjudd.org/justin/sshrpc"
|
||||
)
|
||||
|
||||
type SimpleServer struct{ rpcClient *rpc.Client }
|
||||
|
||||
func (s *SimpleServer) Hello(name *string, out *string) error {
|
||||
log.Println("Name: ", *name)
|
||||
*out = fmt.Sprintf("Hello %s", *name)
|
||||
|
||||
var reply string
|
||||
err := s.rpcClient.Call("SimpleServer.Hello", "This is Server", &reply)
|
||||
if err != nil {
|
||||
log.Printf("Unable to make rpc call: %s", err.Error())
|
||||
}
|
||||
log.Println("Reply: ", reply)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := sshrpc.NewServer()
|
||||
|
||||
privateBytes, err := ioutil.ReadFile("id_rsa")
|
||||
if err != nil {
|
||||
log.Fatal("Failed to load private key (./id_rsa)")
|
||||
}
|
||||
|
||||
private, err := ssh.ParsePrivateKey(privateBytes)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to parse private key")
|
||||
}
|
||||
|
||||
s.Config.AddHostKey(private)
|
||||
|
||||
s.ChannelName = "Nexus"
|
||||
simpleServer := new(SimpleServer)
|
||||
//simpleServer.rpc = s
|
||||
s.Register(simpleServer)
|
||||
|
||||
s.CallbackFunc = func(rpcClient *rpc.Client, conn ssh.Conn) {
|
||||
simpleServer.rpcClient = rpcClient
|
||||
}
|
||||
s.StartServer("localhost:2022")
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
### Client Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"dev.justinjudd.org/justin/sshrpc"
|
||||
)
|
||||
|
||||
type SimpleServer struct{}
|
||||
|
||||
func (s *SimpleServer) Hello(name *string, out *string) error {
|
||||
log.Println("Name: ", *name)
|
||||
*out = fmt.Sprintf("Hello %s", *name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
client := sshrpc.NewClient()
|
||||
client.ChannelName = "Nexus"
|
||||
|
||||
client.RPCServer.Register(new(SimpleServer))
|
||||
|
||||
client.Connect("localhost:2022")
|
||||
defer client.Close()
|
||||
log.Println(client)
|
||||
var reply string
|
||||
|
||||
err := client.Call("SimpleServer.Hello", "Test Name", &reply)
|
||||
if err != nil {
|
||||
log.Printf("Unable to make rpc call: %s", err.Error())
|
||||
}
|
||||
log.Println("Reply: ", reply)
|
||||
|
||||
|
||||
|
||||
err = client.Wait()
|
||||
if err != nil {
|
||||
log.Printf("Client closed: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user