A small Go utility package to handle easier SSH operations such as different kinds of SSH connections (user/pass, private key or signed certificate), command execution, file transfer with SCP protocol.
Currently, it supports:
- Connection with user & password
- Connection with SSH key pair
- Connection with signed SSH certificate
- SCP content, files or directories recursively from local to remote hosts
- SCP files or directories recursively from remote hosts to local
config, err := NewClientConfigWithUserPass("user", "pass", "myremotemachine.com", 22, false)
if err != nil {
return err
}
client, err := NewClient(config)
if err != nil {
return err
}
config, err := NewClientConfigWithUserPass("user", "/home/user/.ssh/id_rsa", "myremotemachine.com", 22, false)
if err != nil {
return err
}
client, err := NewClient(config)
if err != nil {
return err
}
config, err := NewClientConfigWithUserPass("user", "/home/user/.ssh/id_rsa", "/home/user/.ssh/id_rsa-cert.pub", "myremotemachine.com", 22, false)
if err != nil {
return err
}
client, err := NewClient(config)
if err != nil {
return err
}
res, err := client.ExecCommand("ls -la")
err = client.SCPSendBytes([]byte(`SCP single file transfer test`), "/tmp/scp_single_file", "0777")
err = client.SCPSendFile("./data/scp_single_file", "/tmp/scp_single_file", "0777")
err = client.SCPSendDir("./data", "/tmp/scp", "0777")
err = client.SCPGetFile("/tmp/data/scp_single_file", "/tmp/remote/scp_single_file")
err = client.SCPGetDir("/tmp/data", "/tmp/remote")
By default, log is disabled but it can be enabled to debug easily using either function or environment variables:
- Set log level:
client.SetVerbosity(5)
orGOSSH_VERBOSITY=5
- Disable log colors:
client.DisableLogVerbosity()
or GOSSH_DISABLE_COLOR=1`
`
Cf. Godoc or test files in this repository.
Cf.SCP protocol or its copy here