Skip to content

metrue/go-ssh-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-ssh-client

This is a little pacakge helps you run command on remote host via SSH

CI Go Report Card Go Doc asciicast

package main

import (
  "log"
  "os"

  ssh "github.com/metrue/go-ssh-client"
)

func main() {
  host := "127.0.0.1"
  script := `
x=1
while [ $x -le 5 ]; do
  echo 'hello'
  x=$(( $x + 1 ))
  sleep 1
done
`
  err := ssh.New(host).
    WithUser("root").
    WithKey("/your/path/to/id_ras").  // Default is ~/.ssh/id_rsa
    WithPort("2222").    // Default is 22
    RunCommand(script, ssh.CommandOptions{
      Stdout: os.Stdout,
      Stderr: os.Stderr,
      Stdin:  os.Stdin,
    })
  if err != nil {
    log.Fatal(err)
  }
}

Test

$ make start_ssh_server
$ make test
$ make clean #clean up running Docker container