2022-02-25 11:25:09 +08:00
|
|
|
|
# README
|
2018-10-30 17:45:30 +08:00
|
|
|
|
|
|
|
|
|
## 项目简介
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
2018-10-30 17:45:30 +08:00
|
|
|
|
本项目是基于golang标准库 ssh 和 sftp 开发
|
|
|
|
|
|
|
|
|
|
本项目是对标准库进行一个简单的高层封装,使得可以在在 Windows Linux Mac 上非常容易的执行 ssh 命令,
|
|
|
|
|
以及文件,文件夹的上传,下载等操作.
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
2018-11-13 17:42:29 +08:00
|
|
|
|
1. 当src 为目录时
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
|
|
|
|
````bash
|
|
|
|
|
文件上传下载模仿rsync: 只和源有关.
|
|
|
|
|
// rsync -av src/ dst ./src/* --> /root/dst/*
|
|
|
|
|
// rsync -av src/ dst/ ./src/* --> /root/dst/*
|
|
|
|
|
// rsync -av src dst ./src/* --> /root/dst/src/*
|
|
|
|
|
// rsync -av src dst/ ./src/* --> /root/dst/src/*
|
|
|
|
|
````
|
|
|
|
|
|
2018-11-13 17:42:29 +08:00
|
|
|
|
2. 当src 为文件时
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
2018-11-13 17:42:29 +08:00
|
|
|
|
当dst为目录,以"/"结尾,则自动拼接上文件名
|
|
|
|
|
当dst为文件,不以“/”结尾时,则重命名文件
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
2018-11-06 15:37:35 +08:00
|
|
|
|
## Install
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
2022-02-25 11:34:46 +08:00
|
|
|
|
`go get blacktea.vip.cpolar.top/OrgGo/ssh`
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
2018-10-30 17:45:30 +08:00
|
|
|
|
## Example
|
|
|
|
|
|
|
|
|
|
### 在远程执行ssh命令
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
|
|
|
|
提供3个方法: Run() Exec() Output()
|
|
|
|
|
|
2018-12-19 14:33:31 +08:00
|
|
|
|
1. Run() : 程序执行后,不再受执行者控制. 适用于启动服务端进程.
|
|
|
|
|
2. Exec() : 在控制台同步实时输出程序的执行结果.
|
|
|
|
|
3. Output() : 会等待程序执行完成后,输出执行结果,在需要对执行的结果进行操作时使用.
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
2018-10-30 17:45:30 +08:00
|
|
|
|
```go
|
|
|
|
|
package main
|
|
|
|
|
import (
|
2022-02-25 11:25:09 +08:00
|
|
|
|
"fmt"
|
2022-02-25 11:34:46 +08:00
|
|
|
|
"blacktea.vip.cpolar.top/OrgGo/ssh"
|
2018-10-30 17:45:30 +08:00
|
|
|
|
)
|
|
|
|
|
func main() {
|
|
|
|
|
|
2022-02-25 11:25:09 +08:00
|
|
|
|
c, err := ssh.NewClient("localhost", "22", "root", "ubuntu")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer c.Close()
|
2018-10-30 17:45:30 +08:00
|
|
|
|
|
2022-02-25 11:25:09 +08:00
|
|
|
|
output, err := c.Output("uptime")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2018-10-30 17:45:30 +08:00
|
|
|
|
|
2022-02-25 11:25:09 +08:00
|
|
|
|
fmt.Printf("Uptime: %s\n", output)
|
2018-10-30 17:45:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
2018-10-30 17:45:30 +08:00
|
|
|
|
### 文件下载
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
2018-10-30 17:45:30 +08:00
|
|
|
|
```go
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2022-02-25 11:34:46 +08:00
|
|
|
|
"blacktea.vip.cpolar.top/OrgGo/ssh"
|
2018-10-30 17:45:30 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
2022-02-25 11:25:09 +08:00
|
|
|
|
client, err := ssh.NewClient( "localhost", "22", "root", "ubuntu")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer client.Close()
|
|
|
|
|
var remotedir = "/root/test/"
|
|
|
|
|
// download dir
|
2022-02-25 11:34:46 +08:00
|
|
|
|
var local = "/home/ubuntu/go/src/blacktea.vip.cpolar.top/OrgGo/ssh/test/download/"
|
2022-02-25 11:25:09 +08:00
|
|
|
|
client.Download(remotedir, local)
|
2018-10-30 17:45:30 +08:00
|
|
|
|
|
2022-02-25 11:25:09 +08:00
|
|
|
|
// upload file
|
|
|
|
|
var remotefile = "/root/test/file"
|
2018-10-30 17:45:30 +08:00
|
|
|
|
|
2022-02-25 11:25:09 +08:00
|
|
|
|
client.Download(remotefile, local)
|
2018-10-30 17:45:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 文件上传
|
2022-02-25 11:25:09 +08:00
|
|
|
|
|
2018-10-30 17:45:30 +08:00
|
|
|
|
```go
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2022-02-25 11:34:46 +08:00
|
|
|
|
"blacktea.vip.cpolar.top/OrgGo/ssh"
|
2018-10-30 17:45:30 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
2022-02-25 11:25:09 +08:00
|
|
|
|
client, err := ssh.NewClient( "localhost", "22", "root", "ubuntu")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer client.Close()
|
|
|
|
|
var remotedir = "/root/test/"
|
|
|
|
|
// upload dir
|
2022-02-25 11:34:46 +08:00
|
|
|
|
var local = "/home/ubuntu/go/src/blacktea.vip.cpolar.top/OrgGo/ssh/test/upload/"
|
2022-02-25 11:25:09 +08:00
|
|
|
|
client.Upload(local, remotedir)
|
|
|
|
|
|
|
|
|
|
// upload file
|
2022-02-25 11:34:46 +08:00
|
|
|
|
local = "/home/ubuntu/go/src/blacktea.vip.cpolar.top/OrgGo/ssh/test/upload/file"
|
2022-02-25 11:25:09 +08:00
|
|
|
|
client.Upload(local, remotedir)
|
2018-10-30 17:45:30 +08:00
|
|
|
|
}
|
|
|
|
|
```
|