- Notifications
You must be signed in to change notification settings - Fork140
🤘 The native golang ssh client to execute your commands over ssh connection. 🚀🚀
License
NotificationsYou must be signed in to change notification settings
melbahja/goph
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Installation ❘Features ❘Usage ❘Examples ❘License
go get github.com/melbahja/goph
You can find the docs atgo docs.
- Easy to use andsimple API.
- Supportsknown hosts by default.
- Supports connections withpasswords.
- Supports connections withprivate keys.
- Supports connections withprotected private keys with passphrase.
- Supportsupload files from local to remote.
- Supportsdownload files from remote to local.
- Supports connections withssh agent (Unix systems only).
- Supports adding new hosts toknown_hosts file.
- Supportsfile system operations like:
Open, Create, Chmod... - Supportscontext.Context for command cancellation.
Run a command via ssh:
package mainimport ("log""fmt""github.com/melbahja/goph")funcmain() {// Start new ssh connection with private key.auth,err:=goph.Key("/home/mohamed/.ssh/id_rsa","")iferr!=nil {log.Fatal(err)}client,err:=goph.New("root","192.1.1.3",auth)iferr!=nil {log.Fatal(err)}// Defer closing the network connection.deferclient.Close()// Execute your command.out,err:=client.Run("ls /tmp/")iferr!=nil {log.Fatal(err)}// Get your output as []byte.fmt.Println(string(out))}
auth,err:=goph.Key("/home/mohamed/.ssh/id_rsa","you_passphrase_here")iferr!=nil {// handle error}client,err:=goph.New("root","192.1.1.3",auth)
client,err:=goph.New("root","192.1.1.3",goph.Password("you_password_here"))
auth,err:=goph.UseAgent()iferr!=nil {// handle error}client,err:=goph.New("root","192.1.1.3",auth)
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'")
context,cancel:=context.WithTimeout(ctx,time.Second)defercancel()// will send SIGINT and return error after 1 secondout,err:=client.RunContext(ctx,"sleep 5")
out,err:=client.Run(`env MYVAR="MY VALUE" bash -c 'echo $MYVAR;'`)
Goph.Cmd struct is like the Go standardos/exec.Cmd.
// Get new `Goph.Cmd`cmd,err:=client.Command("ls","-alh","/tmp")// or with context:// cmd, err := client.CommandContext(ctx, "ls", "-alh", "/tmp")iferr!=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 likeos/exec.Cmd you can runCombinedOutput, Output, Start, Wait, andssh.Session methods likeSignal...
You can easily get aSFTP client from Goph client:
sftp,err:=client.NewSftp()iferr!=nil {// handle the error!}file,err:=sftp.Create("/tmp/remote_file")file.Write([]byte(`Hello world`))file.Close()
🗒️ For more file operations seeSFTP Docs.
SeeExamples.
Feel free to open a new issue, or contact me.
Goph is provided under theMIT License.
About
🤘 The native golang ssh client to execute your commands over ssh connection. 🚀🚀
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Contributors10
Uh oh!
There was an error while loading.Please reload this page.
