• Stars
    star
    1,334
  • Rank 35,232 (Top 0.7 %)
  • Language
    Go
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿค˜ The native golang ssh client to execute your commands over ssh connection. ๐Ÿš€๐Ÿš€

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/goph

You 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...
  • Supports context.Context for command cancellation.

๐Ÿ“„ย  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 timeout:

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:

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

// 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 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()

๐Ÿ—’๏ธ For more file operations see SFTP Docs.

๐Ÿฅ™ย  Examples

See Examples.

๐Ÿคย  Missing a Feature?

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

๐Ÿ“˜ย  License

Goph is provided under the MIT License.

More Repositories

1

got

Got: Simple golang package and CLI tool to download large files faster ๐Ÿƒ than cURL and Wget!
Go
636
star
2

seo

๐Ÿ˜ Simple PHP library to help developers ๐Ÿป do better on-page SEO optimization ๐Ÿค–
PHP
263
star
3

Http2Pusher

PHP Http2 Server Push
PHP
27
star
4

semver

Simple PHP Semantic Versioning Parser and Comparator
PHP
22
star
5

environ

๐Ÿ˜ PHP environment variables ๐Ÿ”ƒ loader in $_ENV ONLY ๐Ÿšค with the power of INI syntax and ARRAY support
PHP
11
star
6

loginer

oauth Login with facebook and google & twitter using php/mysqli
PHP
10
star
7

ron

Ron: A simple bash task runner to run any executable file inside a .ron directory or `ron.yaml` file!
Go
10
star
8

ctl

๐Ÿ™Œ All systemd commands and tools in one easy command `ctl`! One to rule them all.๐Ÿ˜ผ
Go
7
star
9

Sevent

Sevent: Server-Sent Events PHP & JQuery Plugin
PHP
6
star
10

phpsls

phpsls is a Secure Login System for any website
PHP
5
star
11

Simple_SEO_PHP

Meta Tags & Social Meta and XML Sitemaps PHP Generator for Search Engines
5
star
12

image_sizer

Resize and save or show png, jpg and gif images using PHP
PHP
3
star
13

localcert

Localcert: A command-line utility to generate self signed certificates for localhost servers. (DO NOT USE IN PRODUCTION)
Shell
2
star
14

submy

Submy: JQuery Validation Forms and Ajax Submit Data
JavaScript
1
star
15

bundler

(WIP) The simplest tool to create installers and bundle GUI apps.
Go
1
star
16

Mysqli_Manager

PHP MySQLi Manager Class : Access MySQL database using MySQLi
PHP
1
star
17

fordelay

Javascript foreach with delay โŒš๐’ฅ
JavaScript
1
star
18

global-laravel-artisan

make laravel artisan command global
PHP
1
star