A Go library and common interface for running local and remote commands

go-runcmd

Go

go-runcmd is a Go library and common interface for running local and remote commands providing the Runner interface which helps to abstract away running local and remote shell commands

Borrowed from kovetskiy/runcmd-

Install

go get github.com/aucloud/go-runcmd

Usage

First, import the library:

import "github.com/aucloud/go-runcmd"

Next, create a runner: this is a type, that holds:

  • for local commands: empty struct
  • for remote commands: connecttion to a remote host; so, you can create only one remote runner to remote host

Local Runner:

runner, err := runcmd.NewLocalRunner()
if err != nil {
  //handle error
}

Remote Runner:

runner, err := runcmd.NewRemoteKeyAuthRunner(
  "user",
  "127.0.0.1:22",
  "/home/user/id_rsa",
)
if err != nil {
  //handle error
}

After that, create a command, and call .Run():

c, err := runner.Command("date")
if err != nil {
  //handle error
}
out, err := c.Run()
if err != nil {
  //handle error
}

Both local and remote runners implements the Runner interface, so, you can work with them as Runner:

func listSomeDir(r Runner) error {
  c, err := r.Command("ls -la")
  if err != nil {
    //handle error
  }
  out, err := c.Run()
  if err != nil {
    //handle error
  }
  for _, i := range out {
    fmt.Println(i)
  }
}

Another useful code snippet: pipe from local to remote command:

lRunner, err := NewLocalRunner()
if err != nil {
  //handle error
}

rRunner, err := NewRemoteKeyAuthRunner(user, host, key)
if err != nil {
  //handle error
}

cLocal, err := lRunner.Command("date")
if err != nil {
  //handle error
}
if err = cmdLocal.Start(); err != nil {
  //handle error
}
cRemote, err := rRunner.Command("tee /tmp/tmpfile")
if err != nil {
  //handle error
}
if err = cRemote.Start(); err != nil {
  //handle error
}
if _, err = io.Copy(cRemote.StdinPipe(),cLocal.StdoutPipe(),); err != nil {
  //handle error
}

// Correct handle end of copying:
cmdLocal.Wait()
cmdRemote.StdinPipe().Close()
cmdRemote.Wait()

License

go-runcmd is licensed under the terms of the AGPLv3

Owner
AUCloud
AUCloud AO&M Open Source Projects
AUCloud
Similar Resources

Integrated console application library, using Go structs as commands, with menus, completions, hints, history, Vim mode, $EDITOR usage, and more ...

Integrated console application library, using Go structs as commands, with menus, completions, hints, history, Vim mode, $EDITOR usage, and more ...

Gonsole - Integrated Console Application library This package rests on a readline console library, (giving advanced completion, hint, input and histor

Nov 20, 2022

Go Library to Execute Commands Over SSH at Scale

Go Library to Execute Commands Over SSH at Scale

Go library to handle tens of thousands SSH connections and execute the command(s) with higher-level API for building network device / server automation.

Dec 9, 2022

Fast, realtime regex-extraction, and aggregation into common formats such as histograms, numerical summaries, tables, and more!

Fast, realtime regex-extraction, and aggregation into common formats such as histograms, numerical summaries, tables, and more!

rare A file scanner/regex extractor and realtime summarizor. Supports various CLI-based graphing and metric formats (histogram, table, etc). Features

Dec 29, 2022

kubeaudit helps you audit your Kubernetes clusters against common security controls

kubeaudit helps you audit your Kubernetes clusters against common security controls

Dec 30, 2022

CLI tool to convert many common document types to plane text.

Textify. CLI tool to convert many common document types to plane text. Goals. SO many different document types exist today. PDFs, EPUB books, Microsof

Nov 19, 2021

Command-line tool to load csv and excel (xlsx) files and run sql commands

Command-line tool to load csv and excel (xlsx) files and run sql commands

csv-sql supports loading and saving results as CSV and XLSX files with data processing with SQLite compatible sql commands including joins.

Nov 2, 2022

GC2 is a Command and Control application that allows an attacker to execute commands on the target machine using Google Sheet and exfiltrate data using Google Drive.

GC2 is a Command and Control application that allows an attacker to execute commands on the target machine using Google Sheet and exfiltrate data using Google Drive.

GC2 GC2 (Google Command and Control) is a Command and Control application that allows an attacker to execute commands on the target machine using Goog

Dec 13, 2022

Go-ipfs-cmds - Cmds offers tools for describing and calling commands both locally and remotely

Go-ipfs-cmds - Cmds offers tools for describing and calling commands both locally and remotely

Go-ipfs-cmds - Cmds offers tools for describing and calling commands both locally and remotely

Jan 18, 2022

VSCode Remote-SSH and configuration sharing on WSL.

Wslssh Wslssh forwards SSH command from VSCode Remote-SSH to WSL and enables SSH configuration sharing. Quick Start Guide Download or compile wslssh a

Dec 2, 2021
Comments
  • Add support for ssh agent authentication

    Add support for ssh agent authentication

    I'm working through enabling the https://github.com/aucloud/terraform-provider-swarm provider to use the ssh agent socket instead of a key-based or a password-based authentication. I've tried to add support for bastion hosts as well, but for some reason it failed and I didn't spend more time investigating as it is not required for my use case.

    There doesn't seem to be contribution guide, but let me know whether that's good enough.

Testing local and remote shell commands in Go

Testing local and remote shell commands in Go. This is an (intentionally simplified) example of how unix shell commands can be unit-tested in Go. The

Nov 30, 2021
painless task queue manager for shell commands with an intuitive cli interface (execute shell commands in distributed cloud-native queue manager).

EXEQ DOCS STILL IN PROGRESS. Execute shell commands in queues via cli or http interface. Features Simple intuitive tiny cli app. Modular queue backend

Dec 14, 2022
Chore is a elegant and simple tool for executing common tasks on remote servers.
Chore is a elegant and simple tool for executing common tasks on remote servers.

Chore is a tool for executing common tasks you run on your remote servers. You can easily setup tasks for deployment, commands, and more.

May 20, 2022
cTRL is a server for remote execution of pending tasks and commands in real time, supporting a queue with continuous thread limiting and throttling.

Документация на русском: https://github.com/eltaline/ctrl/blob/master/README-RUS.md cTRL is a server written in Go language that uses a modified versi

Mar 3, 2022
cTRL: a server for remote execution of pending tasks and commands in real time

Документация на русском: https://github.com/eltaline/ctrl/blob/master/README-RUS.md cTRL is a server written in Go language that uses a modified versi

Mar 3, 2022
Go package for running Linux distributed shell commands via SSH.
Go package for running Linux distributed shell commands via SSH.

Go package for running Linux distributed shell commands via SSH.

Dec 7, 2022
A CLI tool for running Go commands with colorized output
A CLI tool for running Go commands with colorized output

Goli Goli is a CLI Tool for running Go commands with colorized output. Note: Goli is still a WIP. It has very basic commands and limitations. Feel fre

Nov 24, 2022
A flexible commandline tool for template rendering. Supports lots of local and remote datasources.
A flexible commandline tool for template rendering. Supports lots of local and remote datasources.

Read the docs at docs.gomplate.ca, chat with developers and community in the #gomplate channel on Gophers Slack gomplate is a template renderer which

Jan 5, 2023
A small CLI tool to check connection from a local machine to a remote target in various protocols.

CHK chk is a small CLI tool to check connection from a local machine to a remote target in various protocols.

Oct 10, 2022
An os/exec like interface for running a command in a container, and being able to easily interact with stdin, stdout, and other adjustments

dockerexec An "os/exec" like interface for running a command in a container, and being able to easily interact with stdin, stdout, and other adjustmen

Jul 14, 2022