本项目是对标准库进行一个简单的高层封装,使得可以在在 Windows Linux Mac 上非常容易的执行 ssh 命令, 以及文件,文件夹的上传,下载等操作.
Go to file
rinetd 375b34f1ea fix some error 2018-11-06 15:37:35 +08:00
cmd golang lib ssh 2018-10-30 17:45:54 +08:00
example golang lib ssh 2018-10-30 17:45:54 +08:00
test/upload golang lib ssh 2018-10-30 17:45:54 +08:00
.gitignore Initial commit 2018-10-30 14:42:31 +08:00
LICENSE Initial commit 2018-10-30 14:42:31 +08:00
README.md fix some error 2018-11-06 15:37:35 +08:00
auth.go fix some error 2018-11-06 15:37:35 +08:00
client.go fix some error 2018-11-06 15:37:35 +08:00
client_test.go fix some error 2018-11-06 15:37:35 +08:00
config.go fix some error 2018-11-06 15:37:35 +08:00
sftp.go golang lib ssh 2018-10-30 17:45:54 +08:00
sftp_is_test.go golang lib ssh 2018-10-30 17:45:54 +08:00
sftp_test.go golang lib ssh 2018-10-30 17:45:54 +08:00
ssh.go fix some error 2018-11-06 15:37:35 +08:00
sudo.go golang lib ssh 2018-10-30 17:45:54 +08:00

README.md

项目简介

本项目是基于golang标准库 ssh 和 sftp 开发

本项目是对标准库进行一个简单的高层封装,使得可以在在 Windows Linux Mac 上非常容易的执行 ssh 命令, 以及文件,文件夹的上传,下载等操作.

文件上传下载模仿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/*

Install

go get github.com/pytool/ssh

Example

在远程执行ssh命令

package main
import (
	"fmt"
	"github.com/pytool/ssh"
)
func main() {

	c, err := ssh.NewClient("root", "localhost", "22", "ubuntu")
	if err != nil {
		panic(err)
	}
	defer c.Close()

	output, err := c.Exec("uptime")
	if err != nil {
		panic(err)
	}

	fmt.Printf("Uptime: %s\n", output)
}

文件下载

package main

import (
	"github.com/pytool/ssh"
)

func main() {

	client, err := ssh.NewClient("root", "localhost", "22", "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)

	// upload file
	var remotefile = "/root/test/file"

	client.Download(remotefile, local)
}

文件上传

package main

import (
	"github.com/pytool/ssh"
)

func main() {

	client, err := ssh.NewClient("root", "localhost", "22", "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)

	// upload file
	local = "/home/ubuntu/go/src/github.com/pytool/ssh/test/upload/file"
	client.Upload(local, remotedir)
}