Installation β Features β Usage β Examples β License
go get github.com/melbahja/goph
- Easy to use.
- 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.
Run a command via ssh:
package main
import (
"log"
"fmt"
"github.com/melbahja/goph"
)
func main() {
// Start new ssh connection with private key.
client, err := goph.New("root", "192.1.1.3", goph.Key("/home/mohamed/.ssh/id_rsa", ""))
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))
}
client, err := goph.New("root", "192.1.1.3", goph.Key("/home/mohamed/.ssh/id_rsa", "you_passphrase_here"))
client, err := goph.New("root", "192.1.1.3", goph.Password("you_password_here"))
client, err := goph.New("root", "192.1.1.3", goph.UseAgent())
err := client.Upload("/path/to/local/file", "/path/to/remote/file")
err := client.Download("/path/to/remote/file", "/path/to/local/file")
out, err := client.Run("bash -c 'printenv'")
out, err := client.Run(`env MYVAR="MY VALUE" bash -c 'echo $MYVAR;'`)
For more read the go docs.
See Examples.
Goph is provided under the MIT License.