Package sshrpc provides rpc access over ssh.
testdata | ||
.gitignore | ||
client.go | ||
common.go | ||
doc.go | ||
example_server_test.go | ||
example_test.go | ||
LICENSE | ||
README.md | ||
server_test.go | ||
server.go |
sshrpc
Package sshrpc provides rpc access over ssh.
Install
go get dev.justinjudd.org/justin/sshrpc
Usage
Server Example
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
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())
}
}