Skip to content

A Go(Golang) package to provide a simple abstraction around SSH (Secure Shell) and SFTP (SSH File Transfer Protocol) packages.

License

Notifications You must be signed in to change notification settings

hueristiq/hqgossh

Repository files navigation

hqgossh

license maintenance open issues closed issues contribution

A Go(Golang) package to provide a simple abstraction around SSH (Secure Shell) and SFTP (SSH File Transfer Protocol) packages.

Resources

Features

  • Establishing SSH connections:
    • ... password.
    • ... key with passphrase.
    • ... key without passphrase.
  • Running remote commands over SSH.
  • Openning interactive shells over SSH.
  • Transferring files over SFTP.

Installation

go get -v -u github.com/hueristiq/hqgossh

Usage

Authentication with password

auth, err := authentication.Password("Password")
if err != nil {
	log.Fatal(err)
}

client, err := ssh.New(&ssh.Configuration{
	Host:            "xxx.xxx.xxx.xxx",
	Port:            22,
	User:            "some-user",
	Authentication:  auth,
	HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
    log.Println(err)
}

defer client.Close()

Authentication with key with passphrase

auth, err := authentication.KeyWithPassphrase(privateKey, "Passphrase")
if err != nil {
	log.Fatal(err)
}

client, err := ssh.New(&ssh.Configuration{
	Host:            "xxx.xxx.xxx.xxx",
	Port:            22,
	User:            "some-user",
	Authentication:  auth,
	HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
	log.Println(err)
}

defer client.Close()

Authentication with key without passphrase

auth, err := authentication.Key(privateKey)
if err != nil {
	log.Fatal(err)
}

client, err := ssh.New(&ssh.Configuration{
	Host:            "xxx.xxx.xxx.xxx",
	Port:            22,
	User:            "some-user",
	Authentication:  auth,
	HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
	log.Println(err)
}

defer client.Close()

Run remote commands

if err = client.Run(&ssh.Command{
	CMD:    "echo ${LC_TEST}",
	ENV:    map[string]string{"LC_TEST":"working"},
	Stdin:  os.Stdin,
	Stdout: os.Stdout,
	Stderr: os.Stderr,
}); err != nil {
	log.Fatal(err)
}

Attach interactive shell

if err = client.Shell(); err != nil {
	log.Println(err)
}

Files upload and download

Upload Local File/Directory to Remote

if err := client.Upload("/path/to/local/file", "/path/to/remote/file"); err != nil {
	log.Println(err)
}

Download Remote File/Directory to Local

if err := client.Download("/path/to/remote/file", "/path/to/local/file"); err != nil {
	log.Println(err)
}

Contributing

Issues and Pull Requests are welcome! Check out the contribution guidelines.

Licensing

This package is distributed under the MIT license.

About

A Go(Golang) package to provide a simple abstraction around SSH (Secure Shell) and SFTP (SSH File Transfer Protocol) packages.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages