Package sshrpc provides rpc access over ssh.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Justin Judd f9c07f8395 Moved logging to use package level logger. Disabled by default. 6 years ago
testdata Adding fully functional sshrpc code for basic use cases 8 years ago
.gitignore Init commit 8 years ago
LICENSE Init commit 8 years ago
README.md updated readme with examples 7 years ago
client.go Moved logging to use package level logger. Disabled by default. 6 years ago
common.go Moved logging to use package level logger. Disabled by default. 6 years ago
doc.go updated to use easyssh library 7 years ago
example_server_test.go updated to use easyssh library 7 years ago
example_test.go updated to use easyssh library 7 years ago
server.go Moved logging to use package level logger. Disabled by default. 6 years ago
server_test.go updated to use easyssh library 7 years ago

README.md

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