Package sshrpc provides rpc access over ssh.
Go to file
2016-07-30 21:40:28 +09:00
testdata Adding fully functional sshrpc code for basic use cases 2014-12-28 23:06:09 -05:00
.gitignore Init commit 2014-12-28 17:45:19 +00:00
client.go Moved logging to use package level logger. Disabled by default. 2016-07-30 21:40:20 +09:00
common.go Moved logging to use package level logger. Disabled by default. 2016-07-30 21:40:28 +09:00
doc.go updated to use easyssh library 2015-05-30 13:25:21 -04:00
example_server_test.go updated to use easyssh library 2015-05-30 13:25:21 -04:00
example_test.go updated to use easyssh library 2015-05-30 13:25:21 -04:00
LICENSE Init commit 2014-12-28 17:45:19 +00:00
README.md updated readme with examples 2015-05-30 23:21:21 -04:00
server_test.go updated to use easyssh library 2015-05-30 13:25:21 -04:00
server.go Moved logging to use package level logger. Disabled by default. 2016-07-30 21:40:20 +09:00

sshrpc

GoDoc

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())
	}
}