Skip to end of metadata
Go to start of metadata

Contents

Requirements

  • goruss is available on bitbucket.org
  • russng v6.3 C implementation

Client Calling +/debug/daytime

Code:

// daytime.go

package main

/*
#cgo LDFLAGS: -lruss
#include <russ/russ.h>
*/
import "C"

import (
	"bitbucket.org/russng/goruss"
	"fmt"
	"os"

)
// usage: daytime [<hostname> ...]
//
// Get daytime value from +/debug from one or more local or remote hosts.
func main() {
	var spath string

	hostnames := []string{""}
	hostnames = append(hostnames, os.Args[1:]...)
	basespath := "+/debug/daytime"
	for _, hostname := range hostnames {
		spath = basespath
		if hostname != "" {
			spath = "+/ssh/" + hostname + "/" + spath
		}
		res, _ := goruss.DialvWaitInOutErrTimeout(10000,
			"execute", spath, nil, nil, "", 1<<20, 1<<20)
		if res.Rv == 0 && res.Ev == 0 {
			fmt.Printf("[%s] %s", hostname, res.Sout)
		}
	}
	os.Exit(0)
}

Notes:

  • the cgo directives are provided in the comments
    • must link to libruss.so
    • include files are at <russ/russ.h>
  • the goruss package is at bitbucket.org/russng/goruss
  • +/debug/daytime is the default service path (spath)
  • for any non-empty hostname, +/ssh/<hostname>/ is prepend to the spath
  • the DialvWaitInOutErrTimeout() convenience function is used to connect and collect the result
  • no attributes or positional arguments are provided
  • res is of type DialWaitInOutErrResult
  • res.Rv is the function return value
  • res.Ev is the service exit value
  • res.Sout is the captured stdout

Build:

mkdir-p  ~/tmp/daytime/src
cd ~/tmp/daytime
export GOPATH=$PWD
cd src
vi daytime.go
go get -d
go build daytime.go

Run:

./daytime
./daytime macha machb

A Comparison with pyruss Implementation

The following is the same client using pyruss:

#! /usr/bin/env python2
#
# daytime.py

import sys

sys.path.insert(1, "/usr/lib/russng")
import pyruss

# usage: daytime [<hostname>]
#
# Get daytime value from +/debug from one of more local or remote hosts.
if __name__ == "__main__":
    hostnames = [""]+sys.argv[1:]
    basespath = "+/debug/daytime"

    for hostname in hostnames:
        spath = basespath
        if hostname:
            spath = "+/ssh/"+hostname+"/"+spath
        t = pyruss.dialv_wait_inouterr_timeout(10000,
            "execute", spath)
        if t[0:2] == (0, 0):
            sys.stdout.write("[%s] %s" % (hostname, t[2]))

Comparing the execution times, it is not unusual that the Go version is ~3x faster.

  • No labels