Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/melbahja/goph
๐ค The native golang ssh client to execute your commands over ssh connection. ๐๐
https://github.com/melbahja/goph
downloader go golang golang-module goph hacktoberfest remote-execution sftp ssh ssh-agent ssh-client ssh-keys unix-systems uploader
Last synced: 23 days ago
JSON representation
๐ค The native golang ssh client to execute your commands over ssh connection. ๐๐
- Host: GitHub
- URL: https://github.com/melbahja/goph
- Owner: melbahja
- License: mit
- Created: 2020-03-10T20:34:17.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-08T10:30:07.000Z (8 months ago)
- Last Synced: 2024-10-01T21:41:41.346Z (about 1 month ago)
- Topics: downloader, go, golang, golang-module, goph, hacktoberfest, remote-execution, sftp, ssh, ssh-agent, ssh-client, ssh-keys, unix-systems, uploader
- Language: Go
- Homepage: http://git.io/bfpiw
- Size: 104 KB
- Stars: 1,753
- Watchers: 28
- Forks: 131
- Open Issues: 34
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ccamel - melbahja/goph - ๐ค The native golang ssh client to execute your commands over ssh connection. ๐๐ (Go)
- awesome-repositories - melbahja/goph - ๐ค The native golang ssh client to execute your commands over ssh connection. ๐๐ (Go)
- awesome-morocco - goph - Native SSH client to execute commands, upload/download files. by [@melbahja](https://git.io/mohamed) (Uncategorized / Uncategorized)
README
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
```bash
go get github.com/melbahja/goph
```You can find the docs at [go docs](https://pkg.go.dev/github.com/melbahja/goph).
## ๐คย 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...`
- Supports **context.Context** for command cancellation.## ๐ย Usage
Run a command via ssh:
```go
package mainimport (
"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:
```go
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:
```go
client, err := goph.New("root", "192.1.1.3", goph.Password("you_password_here"))
```#### โ Start Connection With SSH Agent (Unix systems only):
```go
auth, err := goph.UseAgent()
if err != nil {
// handle error
}client, err := goph.New("root", "192.1.1.3", auth)
```#### โคด๏ธ Upload Local File to Remote:
```go
err := client.Upload("/path/to/local/file", "/path/to/remote/file")
```#### โคต๏ธ Download Remote File to Local:
```go
err := client.Download("/path/to/remote/file", "/path/to/local/file")
```#### โ Execute Bash Commands:
```go
out, err := client.Run("bash -c 'printenv'")
```#### โ Execute Bash Command with timeout:
```go
context, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
// will send SIGINT and return error after 1 second
out, err := client.RunContext(ctx, "sleep 5")
```#### โ Execute Bash Command With Env Variables:
```go
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`.
```go
// Get new `Goph.Cmd`
cmd, err := client.Command("ls", "-alh", "/tmp")// or with context:
// cmd, err := client.CommandContext(ctx, "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()
```๐๏ธ Just like `os/exec.Cmd` you can run `CombinedOutput, Output, Start, Wait`, and [`ssh.Session`](https://pkg.go.dev/golang.org/x/crypto/ssh#Session) methods like `Signal`...
#### ๐ File System Operations Via SFTP:
You can easily get a [SFTP](https://github.com/pkg/sftp) client from Goph client:
```gosftp, err := client.NewSftp()
if err != nil {
// handle the error!
}file, err := sftp.Create("/tmp/remote_file")
file.Write([]byte(`Hello world`))
file.Close()```
๐๏ธ For more file operations see [SFTP Docs](https://github.com/pkg/sftp).## ๐ฅย Examples
See [Examples](https://github.com/melbahja/ssh/blob/master/examples).
## ๐คย Missing a Feature?
Feel free to open a new issue, or contact me.
## ๐ย License
Goph is provided under the [MIT License](https://github.com/melbahja/goph/blob/master/LICENSE).