sshc

package module
v4.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: MIT Imports: 20 Imported by: 4

README

sshc Build Status Go Reference Coverage Code to Test Ratio

sshc.NewClient() returns *ssh.Client using ssh_config(5)

Usage

Describe ~/.ssh/config.

Host myhost
  HostName 203.0.113.1
  User k1low
  Port 10022
  IdentityFile ~/.ssh/myhost_rsa

Use sshc.NewClient() as follows

package main

import (
	"bytes"
	"log"

	"github.com/k1LoW/sshc/v4"
)

func main() {
	client, err := sshc.NewClient("myhost")
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	session, err := client.NewSession()
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	defer session.Close()
	var stdout = &bytes.Buffer{}
	session.Stdout = stdout
	err = session.Run("hostname")
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	log.Printf("result: %s", stdout.String())
}
sshc.Option
client, err := sshc.NewClient("myhost", User("k1low"), Port(1022))

See godoc page

Supported ssh_config keywords

  • Hostname
  • Port
  • User
  • IdentityFile
  • ProxyCommand
  • ProxyJump

References

Documentation

Overview

Package sshc provides sshc.NewClient() that returns *ssh.Client using ssh_config(5)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(dc *DialConfig) (*ssh.Client, error)

Dial returns *ssh.Client using Config

func NewClient

func NewClient(host string, options ...Option) (*ssh.Client, error)

NewClient reads ssh_config(5) ( Default is ~/.ssh/config and /etc/ssh/ssh_config ) and returns *ssh.Client.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config is the type for the SSH Client config. not ssh_config.

func NewConfig

func NewConfig(options ...Option) (*Config, error)

NewConfig creates SSH client config.

func (*Config) Get

func (c *Config) Get(host, key string) string

Get returns Config value.

type DialConfig

type DialConfig struct {
	Hostname          string
	User              string
	Port              int
	UseAgent          bool
	Knownhosts        []string
	KeyAndPassphrases []KeyAndPassphrase
	ProxyCommand      string
	ProxyJump         string
	Password          string
	Timeout           time.Duration
	Wd                string
	Auth              []ssh.AuthMethod
	DialTimeoutFunc   func(network, addr string, timeout time.Duration) (net.Conn, error)
}

type KeyAndPassphrase

type KeyAndPassphrase struct {
	// contains filtered or unexported fields
}

type Option

type Option func(*Config) error

Option is the type for change Config.

func AppendConfigData

func AppendConfigData(b []byte) Option

AppendConfigData returns Option that append ssh_config data to Config.configs.

func AppendConfigPath

func AppendConfigPath(p string) Option

AppendConfigPath returns Option that append ssh_config path to Config.configs.

func AuthMethod

func AuthMethod(m ssh.AuthMethod) Option

AuthMethod returns Option that append ssh.AuthMethod to Config.auth

func ClearConfig

func ClearConfig() Option

ClearConfig returns Option thet clear Config.configs,

func ConfigData

func ConfigData(b []byte) Option

ConfigData returns Option that unshift ssh_config data to Config.configs (alias of UnshiftConfigPath).

func ConfigPath

func ConfigPath(p string) Option

ConfigPath returns Option that unshift ssh_config path to Config.configs (alias of UnshiftConfigPath).

func DialTimeoutFunc added in v4.2.0

func DialTimeoutFunc(fn func(network, addr string, timeout time.Duration) (net.Conn, error)) Option

DialTimeoutFunc returns Option that set Config.dialTimeoutFunc for set SSH client dial func

func Hostname

func Hostname(h string) Option

Hostname returns Option that set Config.hostname for override SSH client port.

func IdentityFile

func IdentityFile(f string, hostPatterns ...string) Option

IdentityFile returns Option that append to Config.identityKeys for SSH client identity file.

func IdentityFileWithPassphrase

func IdentityFileWithPassphrase(f string, passphrase []byte, hostPatterns ...string) Option

IdentityFileWithPassphrase returns Option that append to Config.identityKeys for SSH client identity file.

func IdentityKey

func IdentityKey(b []byte, hostPatterns ...string) Option

IdentityKey returns Option that append to Config.identityKeys for SSH client identity file.

func IdentityKeyWithPassphrase

func IdentityKeyWithPassphrase(b, passphrase []byte, hostPatterns ...string) Option

IdentityKeyWithPassphrase returns Option that append to Config.identityKeys for SSH client identity file.

func Knownhosts

func Knownhosts(files ...string) Option

Knownhosts returns Option that override Config.knownhosts.

func Passphrase

func Passphrase(p []byte) Option

Passphrase returns Option that set Config.passphrase for set SSH key passphrase.

func Password

func Password(pass string) Option

Password returns Option that override Config.password

func Port

func Port(p int) Option

Port returns Option that set Config.port for override SSH client port.

func UnshiftConfigData

func UnshiftConfigData(b []byte) Option

UnshiftConfigData returns Option that unshift ssh_config data to Config.configs.

func UnshiftConfigPath

func UnshiftConfigPath(p string) Option

UnshiftConfigPath returns Option that unshift ssh_config path to Config.configs.

func UseAgent

func UseAgent(u bool) Option

UseAgent returns Option that override Config.useAgent.

func User

func User(u string) Option

User returns Option that set Config.user for override SSH client user.

Jump to

Keyboard shortcuts

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