{"id":19865930,"url":"https://github.com/helloyi/go-sshclient","last_synced_at":"2026-02-15T22:11:17.575Z","repository":{"id":41092981,"uuid":"60513853","full_name":"helloyi/go-sshclient","owner":"helloyi","description":"simple sshclient with go","archived":false,"fork":false,"pushed_at":"2024-05-22T06:20:36.000Z","size":52,"stargazers_count":276,"open_issues_count":2,"forks_count":78,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-11-12T15:31:33.792Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/helloyi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-06-06T09:00:16.000Z","updated_at":"2024-10-23T05:11:40.000Z","dependencies_parsed_at":"2024-04-26T11:43:29.583Z","dependency_job_id":"f041f563-4226-44ff-8c97-2a68fc10ec46","html_url":"https://github.com/helloyi/go-sshclient","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helloyi%2Fgo-sshclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helloyi%2Fgo-sshclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helloyi%2Fgo-sshclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/helloyi%2Fgo-sshclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/helloyi","download_url":"https://codeload.github.com/helloyi/go-sshclient/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251992993,"owners_count":21677022,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T15:24:34.984Z","updated_at":"2026-02-15T22:11:12.555Z","avatar_url":"https://github.com/helloyi.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# simple sshclient with golang\n\n[![GoDoc](https://godoc.org/github.com/helloyi/go-sshclient?status.svg)](https://godoc.org/github.com/helloyi/go-sshclient)\n\nThis package implemented a ssh client. It can run remote command, execute\nremote script, request terminal and request non-interactive shell simply.\n\n## install package\n\n```shell\n# The -u flag instructs get to update modules providing dependencies\n# of packages named on the command line to use newer minor or patch\n# releases when available.\ngo get -u github.com/helloyi/go-sshclient\n```\n\n## create a ssh client\n\n+ Dial with passwd\n\n```go\nclient, err := DialWithPasswd(\"host:port\", \"username\", \"passwd\")\nif err != nil {\n  handleErr(err)\n}\ndefer client.Close()\n```\n\n+ Dial with private key\n\n```go\nclient, err := DialWithKey(\"host:port\", \"username\", \"prikeyFile\")\nif err != nil {\n  handleErr(err)\n}\ndefer client.Close()\n```\n\n+ Dial with private key and a passphrase to decrypt the key\n\n```go\nclient, err := DialWithKeyWithPassphrase(\"host:port\", \"username\", \"prikeyFile\", \"my-passphrase\"))\nif err != nil {\n  handleErr(err)\n}\ndefer client.Close()\n```\n\n+ Dial\n\n```go\nconfig := \u0026ssh.ClientConfig{\n\tUser: user,\n\tAuth: []ssh.AuthMethod{\n\t\tssh.Password(\"yourpasswd\"),\n\t},\n}\nclient, err := Dial(\"network\", \"host:port\", config)\nif err != nil {\n  handleErr(err)\n}\ndefer client.Close()\n```\n\n+ Dial from remote host\n\n[To PR](https://github.com/helloyi/go-sshclient/pull/13)\n\n```go\nclient0, err := Dial(\"tcp\", \"host:port\", config{})\nif err != nil {\n  handleErr(err)\n}\ndefer client0.Close()\n\nclient, err := client0.Dail(\"tcp\", \"host:port\", config{})\nif err != nil {\n  handleErr(err)\n}\ndefer client.Close()\n```\n\n## execute commmand\n\n+ Don't care about output, calling Run\n\n```go\n// run one command\nif err := client.Cmd(\"cmd\").Run(); err {\n  handleErr(err)\n}\n\n// run muti command one time\n// if there is a command run err, and the next commands will not run\nif err := client.Cmd(\"cmd1\").Cmd(\"cmd2\").Cmd(\"cmd3\").Run(); err != nil {\n  handleErr(err)\n}\n```\n\n+ Get output, calling Output\n\n```go\nout, err := client.Cmd(\"cmd\").Output()\nif err != nil {\n  handleErr(err)\n}\nfmt.Println(string(out))\n```\n\n+ Return stderr message, when execution error, calling SmartOutput\n\n```go\nout, err := client.Cmd(\"cmd\").SmartOutput()\nif err != nil {\n  // the 'out' is stderr output\n  handleErr(err, out)\n}\n// the 'out' is stdout output\nfmt.Println(string(out))\n```\n\n+ Write stdout and stderr to your buffer, calling SetStdio\n\n```go\nvar (\n  stdout bytes.Buffer\n  stderr bytes.Buffer\n)\n\nif err := client.Cmd(\"cmd\").SetStdio(\u0026stdout, \u0026stderr).Run(); err {\n  handleErr(err)\n}\n\n// get it\nfmt.Println(string(stdout))\nfmt.Println(string(stderr))\n```\n\n## execute script\n\n+ Run script\n\n```go\nscript = `\n  statment1\n  statment2\n`\n\n// It's as same as Cmd\nclient.Script(script).Run()\nclient.Script(script).Output()\nclient.Script(script).SmartOutput()\n```\n\n+ Run a shell script file\n\n```go\nclient.ScriptFile(\"/path/to/the/script\").Run()\nclient.ScriptFile(\"/path/to/the/script\").Output()\nclient.ScriptFile(\"/path/to/the/script\").SmartOutput()\n```\n\n## get shell\n\n+ Get a non-interactive shell\n\n```go\nif err := client.Shell().Start(); err != nil {\n  handleErr(err)\n}\n```\n\n+ Get a interactive shell\n\n```go\n// default terminal\nif err := client.Terminal(nil).Start(); err != nil {\n  handleErr(err)\n}\n\n// with a terminal config\nconfig := \u0026sshclient.TerminalConfig {\n  Term: \"xterm\",\n  Height: 40,\n  Weight: 80,\n  Modes: ssh.TerminalModes {\n\t  ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud\n\t  ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud\n  }\n}\nif err := client.Terminal(config).Start(); err != nil {\n  handleErr(err)\n}\n```\n\n+ And sometimes, you could set your stdio buffer\n\n```go\nvar (\n  stdin  bytes.Buffer\n  stdout bytes.Buffer\n  stderr bytes.Buffer\n)\n\n// Now, it's like client.Script(\"script\").Run()\nstdin.NewBufferString(\"script\")\nif err := client.Shell().SetStdio(\u0026stdin, \u0026stdout, \u0026stderr).Start(); err != nil {\n  handleErr(err)\n}\n\nfmt.Println(stdout.String())\nfmt.Println(stderr.String())\n```\n\n\n\n## remote file operations\n\nUse  `sftp := client.Sftp()` to obtain a `RemoteFileSystem`. Use `sftp.Closer()` to close it after use, `sftp` can be passively closed using the `client.Close()` if it is used during the client lifetime. \n\nBecause it is designed to have a one-to-one configuration-to-instance relationship, you can obtain the same `RemoteFileSystem` everywhere with the same configuration. Here is an example of the code:\n\n```go\n// The following are the same Sftp specified by opts\nopts := []client.SftpOption{client.SftpMaxPacket(16384)}\nwd, err := client.Sftp(opts...).Getwd()\n// ...\nerr := client.Sftp(opts...).Mkdir(\"path\")\n// ...\nclient.Sftp(opts...).Close()\n```\n\n\n\n### upload file\n\n+ Get a RemoteFileSystem\n\n```go\nsftp := client.Sftp()\n```\n\n+ Then upload local file to remote\n\n```go\n// upload\nif err := sftp.Upload(\"host/file/path\"，\"remote/file/path\"); err != nil {\n  handleErr(err)\n}\n```\n\n+ Close RemoteFileSystem\n\n```go\nif err := sftp.Close(); err != nil {\n  handleErr(err)\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelloyi%2Fgo-sshclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhelloyi%2Fgo-sshclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelloyi%2Fgo-sshclient/lists"}