Golang SSH Client.
Fast and easy golang ssh client module.
Goph is a lightweight Go SSH client focusing on simplicity!
Installation οΏ½?οΏ½ Features οΏ½?οΏ½ Usage οΏ½?οΏ½ Examples οΏ½?οΏ½ License
π Β Installation and Documentation
go get github.com/melbahja/gophYou can find the docs at go docs.
π€ Β Features
- Easy to use and simple API.
- Supports known hosts by default.
- Supports connections with passwords.
- Supports connections with private keys.
- Supports connections with protected private keys with passphrase.
- Supports upload files from local to remote.
- Supports download files from remote to local.
- Supports connections with ssh agent (Unix systems only).
- Supports adding new hosts to known_hosts file.
- Supports file system operations like:
Open, Create, Chmod...
π Β Usage
Run a command via ssh:
package main
import (
"log"
"fmt"
"github.com/melbahja/goph"
)
func main() {
// Start new ssh connection with private key.
auth, err := goph.Key("/home/mohamed/.ssh/id_rsa", "")
if err != nil {
log.Fatal(err)
}
client, err := goph.New("root", "192.1.1.3", auth)
if err != nil {
log.Fatal(err)
}
// Defer closing the network connection.
defer client.Close()
// Execute your command.
out, err := client.Run("ls /tmp/")
if err != nil {
log.Fatal(err)
}
// Get your output as []byte.
fmt.Println(string(out))
}οΏ½? Start Connection With Protected Private Key:
auth, err := goph.Key("/home/mohamed/.ssh/id_rsa", "you_passphrase_here")
if err != nil {
// handle error
}
client, err := goph.New("root", "192.1.1.3", auth)π Start Connection With Password:
client, err := goph.New("root", "192.1.1.3", goph.Password("you_password_here"))β Start Connection With SSH Agent (Unix systems only):
auth, err := goph.UseAgent()
if err != nil {
// handle error
}
client, err := goph.New("root", "192.1.1.3", auth)‴�? Upload Local File to Remote:
err := client.Upload("/path/to/local/file", "/path/to/remote/file")‡�? Download Remote File to Local:
err := client.Download("/path/to/remote/file", "/path/to/local/file")β Execute Bash Commands:
out, err := client.Run("bash -c 'printenv'")β Execute Bash Command With Env Variables:
out, err := client.Run(`env MYVAR="MY VALUE" bash -c 'echo $MYVAR;'`)π₯ͺ Using Goph Cmd:
Goph.Cmd struct is like the Go standard os/exec.Cmd.
// Get new `Goph.Cmd`
cmd, err := client.Command("ls", "-alh", "/tmp")
if err != nil {
// handle the error!
}
// You can set env vars, but the server must be configured to `AcceptEnv line`.
cmd.Env = []string{"MY_VAR=MYVALUE"}
// Run you command.
err = cmd.Run()os/exec.Cmd you can run CombinedOutput, Output, Start, Wait, and ssh.Session methods like Signal...
π File System Operations Via SFTP:
You can easily get a SFTP client from Goph client:
sftp, err := client.NewSftp()
if err != nil {
// handle the error!
}
file, err := sftp.Create("/tmp/remote_file")
file.Write([]byte(`Hello world`))
file.Close()π₯ Β Examples
See Examples.
οΏ½? Β Missing a Feature?
Feel free to open a new issue, or contact me.
π Β License
Goph is provided under the MIT License.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
