迁移仓库地址.

Signed-off-by: chen.yang <chen.yang@yuzhen-iot.com>
This commit is contained in:
chen.yang 2022-02-25 11:25:09 +08:00
parent f7718fbf01
commit b9622b737d
8 changed files with 79 additions and 65 deletions

110
README.md
View File

@ -1,103 +1,117 @@
# README
## 项目简介
本项目是基于golang标准库 ssh 和 sftp 开发
本项目是对标准库进行一个简单的高层封装,使得可以在在 Windows Linux Mac 上非常容易的执行 ssh 命令,
以及文件,文件夹的上传,下载等操作.
1. 当src 为目录时
文件上传下载模仿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/*
````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/*
````
2. 当src 为文件时
当dst为目录以"/"结尾,则自动拼接上文件名
当dst为文件不以“/”结尾时,则重命名文件
## Install
`go get github.com/pytool/ssh`
`go get gitee.com/yuzhen-iot-team/ssh`
## Example
### 在远程执行ssh命令
提供3个方法: Run() Exec() Output()
提供3个方法: Run() Exec() Output()
1. Run() : 程序执行后,不再受执行者控制. 适用于启动服务端进程.
2. Exec() : 在控制台同步实时输出程序的执行结果.
3. Output() : 会等待程序执行完成后,输出执行结果,在需要对执行的结果进行操作时使用.
```go
package main
import (
"fmt"
"github.com/pytool/ssh"
"fmt"
"gitee.com/yuzhen-iot-team/ssh"
)
func main() {
c, err := ssh.NewClient("localhost", "22", "root", "ubuntu")
if err != nil {
panic(err)
}
defer c.Close()
c, err := ssh.NewClient("localhost", "22", "root", "ubuntu")
if err != nil {
panic(err)
}
defer c.Close()
output, err := c.Output("uptime")
if err != nil {
panic(err)
}
output, err := c.Output("uptime")
if err != nil {
panic(err)
}
fmt.Printf("Uptime: %s\n", output)
fmt.Printf("Uptime: %s\n", output)
}
```
### 文件下载
```go
package main
import (
"github.com/pytool/ssh"
"gitee.com/yuzhen-iot-team/ssh"
)
func main() {
client, err := ssh.NewClient( "localhost", "22", "root", "ubuntu")
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)
client, err := ssh.NewClient( "localhost", "22", "root", "ubuntu")
if err != nil {
panic(err)
}
defer client.Close()
var remotedir = "/root/test/"
// download dir
var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/download/"
client.Download(remotedir, local)
// upload file
var remotefile = "/root/test/file"
// upload file
var remotefile = "/root/test/file"
client.Download(remotefile, local)
client.Download(remotefile, local)
}
```
### 文件上传
```go
package main
import (
"github.com/pytool/ssh"
"gitee.com/yuzhen-iot-team/ssh"
)
func main() {
client, err := ssh.NewClient( "localhost", "22", "root", "ubuntu")
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)
client, err := ssh.NewClient( "localhost", "22", "root", "ubuntu")
if err != nil {
panic(err)
}
defer client.Close()
var remotedir = "/root/test/"
// upload dir
var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/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)
// upload file
local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/upload/file"
client.Upload(local, remotedir)
}
```

View File

@ -9,7 +9,7 @@ import (
"github.com/yeka/zip"
"github.com/pytool/ssh"
"gitee.com/yuzhen-iot-team/ssh"
)
var (
@ -32,7 +32,7 @@ func main() {
fmt.Println(tmp)
// var remotedir = "/root/test/"
// // download dir
// var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/download/"
// var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/download/"
// client.Download(remotedir, local)
// // download file

View File

@ -5,7 +5,7 @@ import (
"fmt"
"net"
"github.com/pytool/ssh"
"gitee.com/yuzhen-iot-team/ssh"
"github.com/go-sql-driver/mysql"
)
@ -65,7 +65,7 @@ func main() {
// 2. 使用自定义命名为:mysql+ssh的 Dial 进行mysql连接
// And now we can use our new driver with the regular mysql connection string tunneled through the SSH connection
if db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@mysql+ssh(%s)/%s", dbUser, dbPass, dbHost, dbName)); err == nil {
if rows, err := db.Query("SELECT user, host FROM mysql.user "); err == nil {
for rows.Next() {
var id string

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/pytool/ssh"
"gitee.com/yuzhen-iot-team/ssh"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/pytool/ssh"
"gitee.com/yuzhen-iot-team/ssh"
)
func main() {

View File

@ -1,7 +1,7 @@
package main
import (
"github.com/pytool/ssh"
"gitee.com/yuzhen-iot-team/ssh"
)
func main() {
@ -13,10 +13,10 @@ func main() {
defer client.Close()
var remotedir = "/root/test/"
// upload dir
var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/"
var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/upload/"
client.Upload(local, remotedir)
// upload file
local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/file"
local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/upload/file"
client.Upload(local, remotedir)
}

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/go-sql-driver/mysql v1.6.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/sftp v1.13.4
github.com/pytool/ssh v0.0.0-20190312091242-5aaea5918db7
gitee.com/yuzhen-iot-team/ssh v0.0.0-20190312091242-5aaea5918db7
github.com/yeka/zip v0.0.0-20180914125537-d046722c6feb
golang.org/x/crypto v0.0.0-20220214200702-86341886e292
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1

View File

@ -29,7 +29,7 @@ func GetClient() *Client {
func TestClient_Init(t *testing.T) {
c := GetClient()
defer c.Close()
var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/"
var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/upload/"
var remotedir = "/root/test/"
c.RemoveAll("/root/upload/")
@ -43,7 +43,7 @@ func TestClient_Upload(t *testing.T) {
c := GetClient()
defer c.Close()
var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/"
var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/upload/"
var uploads = map[string][]string{
local + "null/": {"/root/upload/test/null/1", "/root/upload/test/null/2/"},
local + "null/": {"/root/upload/test/null/3", "/root/upload/test/null/4/"},
@ -69,7 +69,7 @@ func TestClient_Download(t *testing.T) {
c := GetClient()
defer c.Close()
var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/download"
var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/download"
var download = map[string][]string{
"/root/test/notExist": {local + "/localNotExist/null/1", local + "/localNotExist/null/2/"},
"/root/test/notExist/": {local + "/localNotExist/null/3", local + "/localNotExist/null/4/"},
@ -96,7 +96,7 @@ func TestClient_DownloadFile(t *testing.T) {
c := GetClient()
defer c.Close()
var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/downloadfile"
var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/downloadfile"
var download = map[string][]string{
"/root/test/notExist": {local + "/localNotExist/null/1", local + "/localNotExist/null/2/"},
"/root/test/notExist/": {local + "/localNotExist/null/3", local + "/localNotExist/null/4/"},
@ -121,7 +121,7 @@ func TestClient_DownloadDir(t *testing.T) {
c := GetClient()
defer c.Close()
var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/downloaddir"
var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/downloaddir"
var download = map[string][]string{
"/root/test/notExist": {local + "/localNotExist/null/1", local + "/localNotExist/null/2/"},
"/root/test/notExist/": {local + "/localNotExist/null/3", local + "/localNotExist/null/4/"},
@ -145,7 +145,7 @@ func TestClient_DownloadDir(t *testing.T) {
func TestClient_UploadFile(t *testing.T) {
c := GetClient()
defer c.Close()
var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/"
var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/upload/"
var uploads = map[string][]string{
local + "null": {"/root/upload/file_test/null/1", "/root/upload/file_test/null/2/"},
local + "null/": {"/root/upload/file_test/null/3", "/root/upload/file_test/null/4/"},
@ -170,7 +170,7 @@ func TestClient_UploadFile(t *testing.T) {
func TestClient_UploadDir(t *testing.T) {
c := GetClient()
defer c.Close()
var local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/"
var local = "/home/ubuntu/go/src/gitee.com/yuzhen-iot-team/ssh/test/upload/"
var uploads = map[string][]string{
local + "null/": {"/root/upload/dir_test/null/1", "/root/upload/dir_test/null/2/"},
local + "null/": {"/root/upload/dir_test/null/3", "/root/upload/dir_test/null/4/"},