ssh

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2020 License: MIT Imports: 8 Imported by: 0

README

Simple Golang SSH Client

Golang module to execute commands over SSH connection

Installation

go get github.com/melbahja/ssh

Usage

package main

import (
	"log"
	"fmt"
	"github.com/melbahja/ssh"
)

func main() {

	// Start ssh connection
	client, err := ssh.New(ssh.Config{
		User: "root",
		Addr: "192.168.122.163",
		Auth: ssh.Key("/home/mohamed/.ssh/id_rsa", ""),
	})

	// Execute a command
	out, err := client.Exec("ls /tmp/")

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(out)
}

Run Custom Command
cmd := client.Command("ls", []string{"-alh"})

result, err := cmd.Run()

if err != nil {
	log.Fatal(err)
}

fmt.Println(result.Stdout.String())

SSH Connection With Passphrase
client, err := ssh.New(ssh.Config{
	User: "root",
	Addr: "192.168.122.163",
	Auth: ssh.Key("/home/mohamed/.ssh/id_rsa", "123456_your_passphrase_here"),
})
SSH Connection With Password (Unsafe!)
client, err := ssh.New(ssh.Config{
	User: "root",
	Addr: "192.168.122.163",
	Auth: ssh.Password("123456_your_password_here"),
})
Add ClientConfig
// import sh "golang.org/x/crypto/ssh"

client, err := ssh.New(ssh.Config{
	User: "root",
	Addr: "192.168.122.163",
	Port: 2000, // You can change the default 22 port
	Auth: ssh.Key("/path/to/privateKey", ""),
	Config: &sh.ClientConfig{
		Timeout: 10 * time.Second,
		// options here: https://pkg.go.dev/golang.org/x/crypto/ssh?tab=doc#ClientConfig 
	},
})
Examples

See Examples

Contributing

Welcome!

License

MIT © Mohammed El Bahja

Documentation

Index

Constants

View Source
const (
	UDP string = "udp"
	TCP string = "tcp"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth []gossh.AuthMethod

func Key

func Key(path string, passphrase string) Auth

func Password

func Password(pass string) Auth

type Client

type Client struct {
	Client *gossh.Client
	Conn   ConnectionConfig
}

func New

func New(c ConnectionConfig) (*Client, error)

func (*Client) Command

func (c *Client) Command(command string, args []string) *Command

Get a Command

func (Client) CopyFileToRemote

func (c Client) CopyFileToRemote(src string, dist string) (err error)

Copy file from local machine to remote machine

func (Client) Exec

func (c Client) Exec(command string) (string, error)

Exec command and get output as a string

func (Client) Run

func (c Client) Run(command string) (Result, error)

Run Cmd command and get Result and error

func (Client) RunFile

func (c Client) RunFile(file string) (res Result, err error)

Run local executable file in remote machine

type ClientConfig

type ClientConfig *gossh.ClientConfig

type Command

type Command struct {

	// Working directory
	Dir string

	Command string
	Args    []string
	Client  *Client
	Stdin   io.Reader
}

func (Command) Run

func (cmd Command) Run() (res Result, err error)

type Config

type Config struct {
	Net    string
	Env    Env
	Addr   string
	Port   int
	User   string
	Auth   Auth
	Config ClientConfig
}

func (Config) GetAddr

func (d Config) GetAddr() string

func (Config) GetClientConfig

func (d Config) GetClientConfig() ClientConfig

func (Config) GetEnv

func (d Config) GetEnv() Env

func (Config) GetNet

func (d Config) GetNet() string

type ConnectionConfig

type ConnectionConfig interface {
	GetNet() string

	GetAddr() string

	GetEnv() Env

	GetClientConfig() ClientConfig
}

type Env

type Env map[string]string

type Network

type Network string

type Result

type Result struct {
	Stdout bytes.Buffer

	Stderr bytes.Buffer
}

Jump to

Keyboard shortcuts

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