Write an SIE script in Go
SIE doesn’t need an interpreter: the command can name any program the web user can run. A Go program builds into a single file with nothing else to install on the rConfig server, which makes it easy to deploy and hard to break with a server upgrade. The cost is a build step every time you change it.
Good for
- Teams that write Go and want a collector with no runtime dependencies.
- Collectors that run against many devices and need to be fast and small.
Set it up
Build on a development machine, not on the rConfig server, then copy the single file across:
go mod init sie-ssh-backupgo get golang.org/x/crypto/sshCGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o sie-ssh-backup .On the rConfig server, put it in a bin folder inside the scripts folder. Unlike a script, a program must be executable:
SCRIPTS=/var/www/html/rconfig8/current/storage/app/rconfig/scriptssudo install -o apache -g apache -m 0750 sie-ssh-backup $SCRIPTS/bin/sie-ssh-backup # www-data on UbuntuThe command
There’s no interpreter, so the program itself comes first:
{script_path}bin/sie-ssh-backup {device_ip} {device_username} {device_password}A minimal program
// Minimal SIE collector in Go: password login over SSH, one command.//// Usage: sie-ssh-backup <ip> <username> <password>package main
import ( "fmt" "net" "os" "strings" "time"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/knownhosts")
const ( keys = "/var/www/html/rconfig8/current/storage/app/rconfig/scripts/keys" configCommand = "show running-config")
func note(format string, a ...any) { fmt.Fprintf(os.Stderr, format+"\n", a...) }
func fail(format string, a ...any) { fmt.Fprintf(os.Stderr, "ERROR: "+format+"\n", a...) os.Exit(1)}
func main() { if len(os.Args) < 4 { fail("usage: sie-ssh-backup <ip> <username> <password>") } host, user, password := os.Args[1], os.Args[2], os.Args[3]
// Host keys must already be in keys/known_hosts; an unknown or changed key fails. hostKeys, err := knownhosts.New(keys + "/known_hosts") if err != nil { fail("cannot read known_hosts: %v", err) }
config := &ssh.ClientConfig{ User: user, Auth: []ssh.AuthMethod{ssh.Password(password)}, HostKeyCallback: hostKeys, Timeout: 15 * time.Second, }
note("connecting to %s as %s", host, user) client, err := ssh.Dial("tcp", net.JoinHostPort(host, "22"), config) if err != nil { fail("cannot connect to %s: %v", host, err) } defer client.Close()
session, err := client.NewSession() if err != nil { fail("cannot open a session on %s: %v", host, err) } defer session.Close()
output, err := session.Output(configCommand) if err != nil { fail("'%s' failed on %s: %v", configCommand, host, err) }
text := strings.TrimSpace(strings.ReplaceAll(string(output), "\r", "")) if text == "" { fail("no output from %s", host) }
fmt.Println(text) note("SIE-OK: go %d lines from %s", strings.Count(text, "\n")+1, host)}Record each device’s host key first, as the web user: ssh-keyscan 192.0.2.10 >> keys/known_hosts. Check it against the device before trusting it.
Habits that matter in Go
fmt.Fprintf(os.Stderr, ...)for progress,fmt.Printlnonly for the config.os.Exit(1)on failure. It skips deferred calls, which is fine here because nothing has been printed to stdout yet.- Keep host key checking on.
ssh.InsecureIgnoreHostKey()is tempting in examples and wrong for a backup job.