mirror of
https://github.com/eznix86/sshx.git
synced 2026-07-25 21:09:16 +00:00
Idiomatic way of interacting with a remote machine
- Go 100%
| constants.go | ||
| doc.go | ||
| go.mod | ||
| go.sum | ||
| keys.go | ||
| LICENSE | ||
| machine.go | ||
| options.go | ||
| README.md | ||
| sftp.go | ||
| template.go | ||
| usergroup.go | ||
| utils.go | ||
sshx
A SSH client library for Go with support for command execution, file uploads, and template rendering.
Features
- Command execution: Execute command on a remote machines
- File Uploads: Upload files with permissions and ownership control
- Template Support: Render and upload Go templates to remote machines
Quick Start
package main
import (
"log"
"github.com/eznix86/sshx"
)
func main() {
// Create a machine
m := sshx.NewMachine("user@host:22")
defer m.Close()
// Run a command
output, err := m.Run("uptime")
if err != nil {
log.Fatal(err)
}
log.Println(output)
// Upload a file
err = m.Upload("local.txt", "/remote/path.txt")
if err != nil {
log.Fatal(err)
}
}
Configuration Options
Connection Options
WithTimeout(duration)- Set connection timeoutWithKnownHosts(path)- Set known_hosts file pathWithAutoKnownHosts()- Auto-load ~/.ssh/known_hosts
Authentication Options
WithAuth(method)- Use custom SSH auth methodWithKeyPath(path)- Load specific SSH keyWithDefaultKeys()- Load default keys from ~/.ssh/WithAgent()- Use SSH agent
Usage
Upload with Metadata
err := m.UploadWithMeta("app.conf", "/etc/app.conf", sshx.FileMeta{
Perm: 0644,
Owner: "www-data",
Group: "www-data",
})
Template Upload
data := struct {
Port int
Host string
}{Port: 8080, Host: "localhost"}
err := m.UploadTemplate("/etc/config.conf",
sshx.WithTemplateString("Server={{.Host}}:{{.Port}}", data),
sshx.WithPermission(0644),
sshx.WithOwner("root", "root"),
)
Custom Authentication
m := sshx.NewMachine("host",
sshx.WithKeyPath("/path/to/private/key"),
sshx.WithKnownHosts("/custom/known_hosts"),
sshx.WithTimeout(30*time.Second),
)
License
MIT License - see LICENSE file for details.