cmdr

package module
v0.0.0-...-b0953d9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2015 License: Apache-2.0 Imports: 10 Imported by: 0

README

cmdr

Go command execution runner for local or remote ssh commands

###Running Tests:

#username and password authentication (Runs an "ls -la /" command)
go test -v -username <username> -password <password> -sshserver <ip addr:port>

#username and pemfile authentication (Runs an "ls -la" command)
go test -v -username <username> -pemfile <pemfile> -sshserver <ip addr:port>

###Local Command Example:

package main

import (
  "github.com/dsledge/cmdr"
  "fmt"
)

func main() {
  output := make(chan string)
  errout := make(chan string)
  
  cmd, err := cmdr.NewCommand(nil, output, errout)
  if err != nil {
    return err
  }
  
  go func() {
    for {
      select {
      case out := <-output:
        fmt.Printf("%s", out)
      case err := <-errout:
        fmt.Printf("%s", err)
      }
    }
  }()
  
  err = cmd.Execute("ls", "-la", "/")
  if err != nil {
    fmt.Printf("%s", err)
  }
}

###Remote SSH Command Example:

package main

import (
  "github.com/dsledge/cmdr"
  "fmt"
)

func main() {
  output := make(chan string)
  errout := make(chan string)
  
  config, err := cmdr.NewClientConfig(*username, *password, *pemfile)
  if err != nil {
    t.Errorf("%s\n", err)
  }
  
  cmd, err := cmdr.NewSSHCommand(config, "<ip address>:<port>", nil, output, errout)
  defer cmd.Close()
  if err != nil {
    t.Errorf("%s\n", err)
  }
  
  go func() {
    for {
      select {
      case out := <-output:
        fmt.Printf("%s", out)
      case err := <-errout:
        fmt.Printf("%s", err)
      }
    }
  }()
  
  err = cmd.Execute("ls -la")
  if err != nil {
    fmt.Printf("%s", err)
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClientConfig

func NewClientConfig(username, password, pemfile string) (*ssh.ClientConfig, error)

Types

type Command

type Command struct {
	Session      *exec.Cmd
	Stdin        chan string
	Stdout       chan string
	Stderr       chan string
	ErrDelimiter byte
	OutDelimiter byte
	ErrMaxBytes  int
	OutMaxBytes  int
	// contains filtered or unexported fields
}

func NewCommand

func NewCommand(inchan, outchan, errchan chan string) (*Command, error)

func (*Command) Execute

func (c *Command) Execute(cmd string, args ...string) error

func (*Command) ProcessStdErr

func (c *Command) ProcessStdErr(notifier chan error, r io.Reader)

func (*Command) ProcessStdIn

func (c *Command) ProcessStdIn(notifier chan error, w io.WriteCloser)

func (*Command) ProcessStdOut

func (c *Command) ProcessStdOut(notifier chan error, r io.Reader)

func (*Command) SetTimeout

func (c *Command) SetTimeout(timeout time.Duration)

type SSHCommand

type SSHCommand struct {
	Command
	Config  *ssh.ClientConfig
	Server  string
	Session *ssh.Session
	// contains filtered or unexported fields
}

func NewSSHCommand

func NewSSHCommand(cfg *ssh.ClientConfig, server string, inchan, outchan, errchan chan string) (*SSHCommand, error)

func (*SSHCommand) Close

func (s *SSHCommand) Close()

func (*SSHCommand) Execute

func (s *SSHCommand) Execute(cmd string) (err error)

func (*SSHCommand) ProcessStdErr

func (s *SSHCommand) ProcessStdErr(notifier chan error, r io.Reader)

func (*SSHCommand) ProcessStdIn

func (c *SSHCommand) ProcessStdIn(notifier chan error, w io.WriteCloser)

func (*SSHCommand) ProcessStdOut

func (s *SSHCommand) ProcessStdOut(notifier chan error, r io.Reader)

func (*SSHCommand) SetTimeout

func (s *SSHCommand) SetTimeout(timeout time.Duration)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL