ssh

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: MIT Imports: 11 Imported by: 0

README

SSH

Go Reference

Tiny wrapper around golang.org/x/crypto/ssh that has better defaults and a slightly higher-level API.

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).
  • Returns an *golang.org/x/crypto/ssh.Client that can be used elsewhere
  • Adds Run(ssh, cmd) (stdout, error) and Exec(ssh, cmd) error commands.

Example

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

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

Install

go get github.com/matthewmueller/ssh

Development

First, clone the repo:

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

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 Exec

func Exec(ssh *Client, cmd string) error

Exec a command on the remote host

func Run

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

Run a command on the remote host

func Split

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

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

Types

type Client

type Client = ssh.Client

Client is an alias for ssh.Client

func Dial

func Dial(userHost string) (*Client, error)

Dial creates a new ssh.Client with sensible defaults

Example
package main

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

func main() {
	// Dial a server
	client, err := ssh.Dial("vagrant@127.0.0.1:2222")
	if err != nil {
		panic(err)
	}
	defer client.Close()
	err = ssh.Exec(client, "ls -al")
	if err != nil {
		panic(err)
	}
}

type ClientConfig

type ClientConfig = ssh.ClientConfig

func Configure

func Configure(userHost string) (*ClientConfig, error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL