Documentation ¶
Overview ¶
Package gosher provides types and methods for operations on remote machines via SSH e.g. execution of commands, download/upload of files
Index ¶
- Constants
- type Host
- type MultipleHostsSshClient
- func (msc *MultipleHostsSshClient) Download(remotePath string, localPath string)
- func (msc *MultipleHostsSshClient) Run(command string)
- func (msc *MultipleHostsSshClient) RunOnFile(filePath string, alterContentsFunction func(fileContent string) string)
- func (msc *MultipleHostsSshClient) RunScript(filePath string)
- func (msc *MultipleHostsSshClient) Upload(localPath string, remotePath string)
- type SshClient
- func (s *SshClient) CloseSession() error
- func (s *SshClient) Download(remotePath string, localPath string) (*SshResponse, error)
- func (s *SshClient) Run(command string) (*SshResponse, error)
- func (s *SshClient) RunOnFile(filePath string, alterContentsFunction func(fileContent string) string) (*SshResponse, error)
- func (s *SshClient) RunScript(scriptPath string) (*SshResponse, error)
- func (s *SshClient) Upload(localPath string, remotePath string) (*SshResponse, error)
- type SshConnectionError
- type SshResponse
Constants ¶
const ( PasswordAuthentication = iota KeyAuthentication )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Host ¶
type Host struct { Client *SshClient ResultChannel chan *SshResponse ErrorChannel chan error }
Host - remote machine definition type. Use with the multipleHostsSshClient with asynchronous execution. ResultChannel - the channel via which the SshResponse of the operations will be passed. ErrorChannel - the channel via which the error of the operations will be passed.
type MultipleHostsSshClient ¶
type MultipleHostsSshClient struct {
Hosts []*Host
}
func NewMultipleHostsSshClient ¶
func NewMultipleHostsSshClient(hosts ...*Host) *MultipleHostsSshClient
Constructor method for MultipleHostsSshClient Use this client when dealing with multiple hosts asynchronously.
func (*MultipleHostsSshClient) Download ¶
func (msc *MultipleHostsSshClient) Download(remotePath string, localPath string)
Downloads files/folders from all hosts of the MultipleHostsSshClient's list. They will be suffixed with the index of the host they are downloaded from
func (*MultipleHostsSshClient) Run ¶
func (msc *MultipleHostsSshClient) Run(command string)
Executes shell command on all hosts in a separate goroutine for each. The result from execution is passed via the hosts' channels
func (*MultipleHostsSshClient) RunOnFile ¶
func (msc *MultipleHostsSshClient) RunOnFile(filePath string, alterContentsFunction func(fileContent string) string)
Executes an function on a remote text file on all hosts. Can be used as an alternative of executing sed or awk on the remote machine. alterContentsFunction - the function to be executed, the content of the file as string will be passed to it and it should return the modified content.
func (*MultipleHostsSshClient) RunScript ¶
func (msc *MultipleHostsSshClient) RunScript(filePath string)
Executes shell script on all hosts in a separate goroutine for each. The result from execution is passed via the hosts' channels
func (*MultipleHostsSshClient) Upload ¶
func (msc *MultipleHostsSshClient) Upload(localPath string, remotePath string)
Uploads a file/folder to all hosts of the MultipleHostsSshClient. The sshResponse is passed via the channels of the hosts
type SshClient ¶
type SshClient struct { Port int StickySession bool Address string // contains filtered or unexported fields }
Port - 22 by default StickySession - false by default, if true sessions won't be closed automatically and one would have to use CloseSession()
func NewSshClient ¶
func NewSshClient(address string, user string, authenticationType int, authentication string) (*SshClient, error)
Initializes the SshClient. This client is meant for synchronous usage with a single host. authenticationType is the type of authentication used, can be PasswordAuthentication or KeyAuthentication. authentication is the password or the path to the path to the key accorrding to the authenticationType.
func (*SshClient) CloseSession ¶
Closes the session, use only with StickySession set to true
func (*SshClient) Download ¶
func (s *SshClient) Download(remotePath string, localPath string) (*SshResponse, error)
Downloads file/folder from the remote machine. Can be used as an alternative to scp. Returns an SshResponse and an error if any has occured.
func (*SshClient) Run ¶
func (s *SshClient) Run(command string) (*SshResponse, error)
Executes shell command on the remote machine synchronously. Returns an SshResponse and an error if any has occured.
func (*SshClient) RunOnFile ¶
func (s *SshClient) RunOnFile(filePath string, alterContentsFunction func(fileContent string) string) (*SshResponse, error)
Executes an function on a remote text file. Can be used as an alternative of executing sed or awk on the remote machine. alterContentsFunction is the function to be executed, the content of the file as string will be passed to it and it should return the modified content. Returns SshResponse and an error if any has occured.
func (*SshClient) RunScript ¶
func (s *SshClient) RunScript(scriptPath string) (*SshResponse, error)
Executes a shell script file on the remote machine. It is copied in the tmp folder and ran in a single session. chmod +x is applied before running. Returns an SshResponse and an error if any has occured
type SshConnectionError ¶
type SshConnectionError struct {
// contains filtered or unexported fields
}
Standard error returned on all ssh operations This means there was an error with the connection or the command returned an error code different from 0.
func NewSshConnectionError ¶
func NewSshConnectionError(errorMessage string) *SshConnectionError
func (*SshConnectionError) Error ¶
func (se *SshConnectionError) Error() string
Returns the error message of the SshConnectionError
type SshResponse ¶
Standard response returned from ssh operations
func NewSshResponse ¶
func NewSshResponse(host string, session *ssh.Session) *SshResponse