Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🤘 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

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fast and easy golang ssh client module.

Goph is a lightweight Go SSH client focusing on simplicity!

InstallationFeaturesUsageExamplesLicense

🚀  Installation and Documentation

go get github.com/melbahja/goph

You can find the docs atgo docs.

🤘  Features

  • 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.

📄  Usage

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))}

🔐 Start Connection With Protected Private Key:

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)

🔑 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()iferr!=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 timeout:

context,cancel:=context.WithTimeout(ctx,time.Second)defercancel()// will send SIGINT and return error after 1 secondout,err:=client.RunContext(ctx,"sleep 5")

☛ 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 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...

📂 File System Operations Via SFTP:

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.

🥙  Examples

SeeExamples.

🤝  Missing a Feature?

Feel free to open a new issue, or contact me.

📘  License

Goph is provided under theMIT License.

About

🤘 The native golang ssh client to execute your commands over ssh connection. 🚀🚀

Topics

Resources

License

Stars

Watchers

Forks

Contributors10

Languages


[8]ページ先頭

©2009-2025 Movatter.jp