From a9e6dd5ffccad6848106ef5e39bd5e9828553147 Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 30 May 2015 23:21:21 -0400 Subject: [PATCH] updated readme with examples --- README.md | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/README.md b/README.md index e3daa3b..79f4f28 100644 --- a/README.md +++ b/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()) + } +} + + +```