Skip to content

Simple ssh client in Go to run several commands in remote host

License

Notifications You must be signed in to change notification settings

williamchanrico/go-sshc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

go-sshc

Simple ssh client in Go to run several commands in remote host

Installation

go get github.com/williamchanrico/go-sshc

Example

package main

import (
	"log"

	sshc "github.com/williamchanrico/go-sshc"
)

func main() {
	// Prepare ssh client
	client, err := sshc.NewClient(&sshc.Config{
		User:           "root",
		PrivateKeyFile: "/home/william/.ssh/test-unencrypted.pem",
	})
	if err != nil {
		log.Fatalln(err)
	}

	// Connect to remote host
	conn, err := client.Connect("172.21.45.20", "22")
	if err != nil {
		log.Fatalln(err)
	}
	defer conn.Close()
	log.Println("Connected, running command(s)...")

	// Run command(s) on remote host
	cmds := []string{
		"echo 'Hello World!' > /root/hello.txt",
		"sleep 10",
		"echo 'Good bye!' >> /root/hello.txt",
	}
	if err = client.Run(conn, cmds); err != nil {
		log.Println(err)
	}
}
$ go run main.go
2018/10/26 17:39:11 Connected, running command(s)...
2018/10/26 17:39:11 Running cmd[0]: [echo 'Hello World!' > /root/hello.txt]
2018/10/26 17:39:11 Running cmd[1]: [sleep 10]
2018/10/26 17:39:22 Running cmd[2]: [echo 'Good bye!' >> /root/hello.txt]

About

Simple ssh client in Go to run several commands in remote host

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages