A Go(Golang) package to provide a simple abstraction around SSH (Secure Shell) and SFTP (SSH File Transfer Protocol) packages.
- Establishing SSH connections:
- ... password.
- ... key with passphrase.
- ... key without passphrase.
- Running remote commands over SSH.
- Openning interactive shells over SSH.
- Transferring files over SFTP.
go get -v -u github.com/hueristiq/hqgossh
auth, err := authentication.Password("Password")
if err != nil {
log.Fatal(err)
}
client, err := ssh.New(&ssh.Configuration{
Host: "xxx.xxx.xxx.xxx",
Port: 22,
User: "some-user",
Authentication: auth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
log.Println(err)
}
defer client.Close()
auth, err := authentication.KeyWithPassphrase(privateKey, "Passphrase")
if err != nil {
log.Fatal(err)
}
client, err := ssh.New(&ssh.Configuration{
Host: "xxx.xxx.xxx.xxx",
Port: 22,
User: "some-user",
Authentication: auth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
log.Println(err)
}
defer client.Close()
auth, err := authentication.Key(privateKey)
if err != nil {
log.Fatal(err)
}
client, err := ssh.New(&ssh.Configuration{
Host: "xxx.xxx.xxx.xxx",
Port: 22,
User: "some-user",
Authentication: auth,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
if err != nil {
log.Println(err)
}
defer client.Close()
if err = client.Run(&ssh.Command{
CMD: "echo ${LC_TEST}",
ENV: map[string]string{"LC_TEST":"working"},
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}); err != nil {
log.Fatal(err)
}
if err = client.Shell(); err != nil {
log.Println(err)
}
if err := client.Upload("/path/to/local/file", "/path/to/remote/file"); err != nil {
log.Println(err)
}
if err := client.Download("/path/to/remote/file", "/path/to/local/file"); err != nil {
log.Println(err)
}
Issues and Pull Requests are welcome! Check out the contribution guidelines.
This package is distributed under the MIT license.