ssh/README.md

100 lines
2.0 KiB
Markdown
Raw Normal View History

2018-10-30 17:45:30 +08:00
## 项目简介
本项目是基于golang标准库 ssh 和 sftp 开发
本项目是对标准库进行一个简单的高层封装,使得可以在在 Windows Linux Mac 上非常容易的执行 ssh 命令,
以及文件,文件夹的上传,下载等操作.
2018-11-13 17:42:29 +08:00
1. 当src 为目录时
文件上传下载模仿rsync: 只和源有关.
2018-10-30 17:46:57 +08:00
// 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 为文件时
当dst为目录以"/"结尾,则自动拼接上文件名
当dst为文件不以“/”结尾时,则重命名文件
2018-11-06 15:37:35 +08:00
## Install
`go get github.com/pytool/ssh`
2018-10-30 17:45:30 +08:00
## Example
### 在远程执行ssh命令
```go
package main
import (
"fmt"
"github.com/pytool/ssh"
)
func main() {
2018-11-13 17:42:29 +08:00
c, err := ssh.NewClient("localhost", "22", "root", "ubuntu")
2018-10-30 17:45:30 +08:00
if err != nil {
panic(err)
}
defer c.Close()
output, err := c.Exec("uptime")
if err != nil {
panic(err)
}
fmt.Printf("Uptime: %s\n", output)
}
```
### 文件下载
```go
package main
import (
"github.com/pytool/ssh"
)
func main() {
2018-11-13 17:42:29 +08:00
client, err := ssh.NewClient( "localhost", "22", "root", "ubuntu")
2018-10-30 17:45:30 +08:00
if err != nil {
panic(err)
}
defer client.Close()
var remotedir = "/root/test/"
// download dir
var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/download/"
client.Download(remotedir, local)
// upload file
var remotefile = "/root/test/file"
client.Download(remotefile, local)
}
```
### 文件上传
```go
package main
import (
"github.com/pytool/ssh"
)
func main() {
2018-11-13 17:42:29 +08:00
client, err := ssh.NewClient( "localhost", "22", "root", "ubuntu")
2018-10-30 17:45:30 +08:00
if err != nil {
panic(err)
}
defer client.Close()
var remotedir = "/root/test/"
// upload dir
var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/"
client.Upload(local, remotedir)
// upload file
local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/file"
client.Upload(local, remotedir)
}
```