sshx

package module
v0.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 13 Imported by: 1

README

sshx

Go Reference

Tiny wrapper around golang.org/x/crypto/ssh that has better defaults. Returns an *golang.org/x/crypto/ssh.Client that can be used elsewhere

The goal being it works exactly like if you did ssh user@host on your machine.

Features

  • Handles ~/.ssh/known_hosts on OSX thanks to skeema/knownhosts.
  • Uses the active SSH agent on your machine if there is one, allowing you to seamlessly connect without providing a private key (and often the password needed to decrypt that private key).
  • Adds Run(ssh, cmd) (stdout, error) and Exec(ssh, cmd) error commands.
  • Allocate an interactive shell with sshx.Shell(sshClient, workDir)

Example

// Dial a server
client, err := sshx.Dial("vagrant", "127.0.0.1:2222")
if err != nil {
  // handle error
}
defer client.Close()

// Run a command
stdout, err := sshx.Run(client, "ls -al")
if err != nil {
  // handle error
}

Install

go get github.com/matthewmueller/sshx

Development

First, clone the repo:

git clone https://github.com/matthewmueller/sshx
cd sshx

Next, install dependencies:

go mod tidy

Finally, try running the tests:

go test ./...

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(user, host string, signers ...ssh.Signer) *ssh.ClientConfig

Configure creates a new *ClientConfig based on sensible defaults. This method is fairly error-resistent and intended for advanced use cases.

func Dial

func Dial(user, host string, signers ...ssh.Signer) (*ssh.Client, error)

Dial creates a new ssh.Client with sensible defaults

Example
package main

import (
	"github.com/matthewmueller/sshx"
)

func main() {
	// Dial a server
	client, err := sshx.Dial("vagrant", "127.0.0.1:2222")
	if err != nil {
		panic(err)
	}
	defer client.Close()
	err = sshx.Exec(client, "echo 'sshx'")
	if err != nil {
		panic(err)
	}
}
Output:

sshx

func DialConfig added in v0.0.12

func DialConfig(host string, config *ssh.ClientConfig) (*ssh.Client, error)

DialConfig creates a new ssh.Client with the provided ssh config

Example
package main

import (
	"time"

	"github.com/matthewmueller/sshx"
)

func main() {
	cfg := sshx.Configure("vagrant", "127.0.0.1:2222")
	cfg.Timeout = time.Second
	// Dial a server
	client, err := sshx.DialConfig("127.0.0.1:2222", cfg)
	if err != nil {
		panic(err)
	}
	defer client.Close()
	err = sshx.Exec(client, "echo 'sshx'")
	if err != nil {
		panic(err)
	}
}
Output:

sshx

func DialEach added in v0.0.8

func DialEach(user, host string, signers ...ssh.Signer) (*ssh.Client, ssh.Signer, error)

Dial each signer until we find one that works

func Exec

func Exec(ssh *ssh.Client, cmd string) error

Exec a command on the remote host, piping output to os.Stdout and os.Stderr

func Run

func Run(ssh *ssh.Client, cmd string) (string, error)

Run a command on the remote host, piping stderr to os.Stderr and returning stdout

func Shell added in v0.0.6

func Shell(sshc *ssh.Client, dir string, args ...string) error

func Split

func Split(userHost string) (user string, host string, err error)

Split a user@host[:port] string into user and host.

func Test added in v0.0.7

func Test(user, host string, signers ...ssh.Signer) (ssh.Signer, error)

Test the remote host connection, returning the first signer that was successfully used to connect to the remote host.

Example
package main

import (
	"github.com/matthewmueller/sshx"
)

func main() {
	// Dial a server
	signer, err := sshx.Test("vagrant", "127.0.0.1:2222")
	if err != nil {
		panic(err)
	}
	_ = signer.PublicKey()
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳