simplessh

package module
v0.0.0-...-27fa108 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2021 License: MIT Imports: 13 Imported by: 0

README

simplessh

GoDoc

SimpleSSH is a simple wrapper around go ssh and sftp libraries.

Features

  • Multiple authentication methods (password, private key and ssh-agent)
  • Sudo support
  • Simple file upload/download support

Installation

go get github.com/sfreiberg/simplessh

Example

package main

import (
	"fmt"
	
	"github.com/sfreiberg/simplessh"
)

func main() {
	/*
		Leave privKeyPath empty to use $HOME/.ssh/id_rsa.
		If username is blank simplessh will attempt to use the current user.
	*/
	client, err := simplessh.ConnectWithKeyFile("localhost:22", "root", "/home/user/.ssh/id_rsa")
	if err != nil {
		panic(err)
	}
	defer client.Close()

	output, err := client.Exec("uptime")
	if err != nil {
		panic(err)
	}

	fmt.Printf("Uptime: %s\n", output)
}

License

SimpleSSH is licensed under the MIT license.

Documentation

Index

Constants

View Source
const DefaultTimeout = 1 * time.Second

Variables

View Source
var HostKeyCallback = ssh.InsecureIgnoreHostKey()

Set a default HostKeyCallback variable. This may not be desireable for some environments.

Functions

This section is empty.

Types

type Client

type Client struct {
	SSHClient *ssh.Client
}

func ConnectWithAgent

func ConnectWithAgent(host, username string) (*Client, error)

Connect to an ssh agent. If username is empty simplessh will attempt to get the current user. The windows implementation uses a different library which expects pageant to be running.

func ConnectWithAgentTimeout

func ConnectWithAgentTimeout(host, username string, timeout time.Duration) (*Client, error)

Connect to an ssh agent with a custom timeout. If username is empty simplessh will attempt to get the current user. The windows implementation uses a different library which expects pageant to be running.

func ConnectWithKey

func ConnectWithKey(host, username, privKey string) (*Client, error)

Connect with a private key. If username is empty simplessh will attempt to get the current user.

func ConnectWithKeyFile

func ConnectWithKeyFile(host, username, privKeyPath string) (*Client, error)

Same as ConnectWithKeyFile but allows a custom timeout. If username is empty simplessh will attempt to get the current user.

func ConnectWithKeyFileTimeout

func ConnectWithKeyFileTimeout(host, username, privKeyPath string, timeout time.Duration) (*Client, error)

Connect with a private key. If privKeyPath is an empty string it will attempt to use $HOME/.ssh/id_rsa. If username is empty simplessh will attempt to get the current user.

func ConnectWithKeyTimeout

func ConnectWithKeyTimeout(host, username, privKey string, timeout time.Duration) (*Client, error)

Connect with a private key with a custom timeout. If username is empty simplessh will attempt to get the current user.

func ConnectWithPassword

func ConnectWithPassword(host, username, pass string) (*Client, error)

Connect with a password. If username is empty simplessh will attempt to get the current user.

func ConnectWithPasswordTimeout

func ConnectWithPasswordTimeout(host, username, pass string, timeout time.Duration) (*Client, error)

Same as ConnectWithPassword but allows a custom timeout. If username is empty simplessh will attempt to get the current user.

func (*Client) Close

func (c *Client) Close() error

Close the underlying SSH connection

func (*Client) Download

func (c *Client) Download(remote, local string) error

Download a file from the remote server

func (*Client) Exec

func (c *Client) Exec(cmd string) ([]byte, error)

Execute cmd on the remote host and return stderr and stdout

func (*Client) ExecSudo

func (c *Client) ExecSudo(cmd, passwd string) ([]byte, error)

Execute cmd via sudo. Do not include the sudo command in the cmd string. For example: Client.ExecSudo("uptime", "password"). If you are using passwordless sudo you can use the regular Exec() function.

func (*Client) ReadAll

func (c *Client) ReadAll(filepath string) ([]byte, error)

Read a remote file and return the contents.

func (*Client) Remove

func (c *Client) Remove(path string) error

Remove a file from the remote server

func (*Client) RemoveDirectory

func (c *Client) RemoveDirectory(path string) error

Remove a directory from the remote server

func (*Client) SFTPClient

func (c *Client) SFTPClient() (*sftp.Client, error)

Return an sftp client. The client needs to be closed when it's no longer needed.

func (*Client) Upload

func (c *Client) Upload(local, remote string) error

Upload a file to the remote server

Jump to

Keyboard shortcuts

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