zdpgo_ssh

package module
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2023 License: MIT Imports: 8 Imported by: 2

README

zdpgo_ssh

使用Golang执行ssh命令

版本历史

  • v1.0.3 2022/06/26 升级:日志组件升级
  • v1.0.4 2022/06/26 新增:文件上传和下载
  • v1.0.5 2022/06/26 优化:移除日志
  • v1.0.6 2023/01/08 新增:通过公钥连接SSH,即就是免密登录

快速入门

执行shell命令
package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_ssh"
)

func main() {
	// 创建对象
	s := zdpgo_ssh.NewWithConfig(&zdpgo_ssh.Config{
		Host:     "192.168.33.10",
		Port:     22,
		Username: "zhangdapeng",
		Password: "zhangdapeng",
	})

	// 进行连接
	output, err := s.Run("free -h")

	// 查看命令结果
	fmt.Printf("%v\n%v", output, err)

	// 查看健康状态
	fmt.Println(s.Status())
}
执行sudo命令
package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_ssh"
)

func main() {
	s := zdpgo_ssh.NewWithConfig(&zdpgo_ssh.Config{
		Host:     "192.168.33.10",
		Port:     22,
		Username: "zhangdapeng",
		Password: "zhangdapeng",
	})
	output, err := s.Sudo("ls -lah")
	fmt.Printf("%v\n%v", output, err)
}

文件上传

package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_ssh"
)

func main() {
	s := zdpgo_ssh.NewWithConfig(&zdpgo_ssh.Config{
		Host:     "192.168.33.10",
		Port:     22,
		Username: "zhangdapeng",
		Password: "zhangdapeng",
	})
	output, err := s.Sudo("ls -lah")
	fmt.Printf("%v\n%v", output, err)

	// 上传文件
	s.UploadFile("README.md", "README111.md")

	output, err = s.Sudo("ls -lah")
	fmt.Printf("%v\n%v", output, err)
}

文件下载

package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_ssh"
)

func main() {
	s := zdpgo_ssh.NewWithConfig(&zdpgo_ssh.Config{
		Host:     "192.168.33.10",
		Port:     22,
		Username: "zhangdapeng",
		Password: "zhangdapeng",
	})
	output, err := s.Sudo("ls -lah")
	fmt.Printf("%v\n%v", output, err)

	// 下载文件
	s.DownloadFile("README111.md", "README111.md")

	output, err = s.Sudo("ls -lah")
	fmt.Printf("%v\n%v", output, err)
}

公共公钥连接SSH

Windows配置SSH免密登录

这一步很重要,大家可以自行谷歌解决。如果嫌麻烦,也可以关注我的公众号“Python私教”,里面有详细的配置教程和zdpgo_ssh库的使用说明。

使用示例

本示例的地址位于本开源项目的:examples/new_with_public_key/main.go

package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_ssh"
)

func main() {
	var (
		user = "root"
		host = "192.168.1.81"
		port = 22
	)
	ssh, err := zdpgo_ssh.NewWithPublicKey(user, host, port)
	if err != nil {
		panic(err)
	}
	result, err := ssh.Run("pwd")
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host     string `yaml:"host" json:"host"`
	Port     int    `yaml:"port" json:"port"`
	Username string `yaml:"username" json:"username"`
	Password string `yaml:"password" json:"password"`
}

type FileResult added in v1.0.4

type FileResult struct {
	Status         bool   `yaml:"status" json:"status"`
	LocalFileName  string `yaml:"local_file_name" json:"local_file_name"`
	LocalFileSize  uint64 `yaml:"local_file_size" json:"local_file_size"`
	RemoteFileName string `yaml:"remote_file_name" json:"remote_file_name"`
	RemoteFileSize uint64 `yaml:"remote_file_size" json:"remote_file_size"`
}

type SSH

type SSH struct {
	Client *ssh.Client // ssh客户端
	Config *Config
}

SSH SSH连接对象

func New

func New() *SSH

New 创建SSH对象 @param host 主机地址 @param username 用户名 @param password 密码 @param port 端口号,默认22

func NewWithConfig

func NewWithConfig(config *Config) *SSH

func NewWithPublicKey added in v1.0.6

func NewWithPublicKey(user, host string, port int) (s *SSH, err error)

NewWithPublicKey 通过公钥的方式连接 @param user 主机用户名 @param host 主机IP地址或域名 @param port 主机SSH服务端口号,一般是22 @return s SSH连接对象 @return err 错误信息

func (*SSH) Connect

func (s *SSH) Connect() error

Connect 建立连接

func (*SSH) DownloadFile added in v1.0.0

func (s *SSH) DownloadFile(remoteFileName, localFileName string) FileResult

DownloadFile 下载文件

func (*SSH) FormatFileSize added in v1.0.4

func (ssh *SSH) FormatFileSize(s int64) (size string)

FormatFileSize 字节的单位转换 保留两位小数

func (*SSH) GetSshConfig added in v1.0.4

func (s *SSH) GetSshConfig() *ssh.ClientConfig

GetSshConfig 获取SSH客户端配置

func (*SSH) Run

func (s *SSH) Run(command string) (string, error)

Run 执行sh命令 @param command 命令

func (*SSH) Status

func (ssh *SSH) Status() bool

Status 获取服务器的健康状态

func (*SSH) Sudo

func (s *SSH) Sudo(command string) (string, error)

Sudo 使用管理员身份执行sh命令 @param command 命令

func (*SSH) UploadFile added in v1.0.0

func (s *SSH) UploadFile(localFileName, remoteFileName string) FileResult

UploadFile 上传文件

Directories

Path Synopsis
examples
run
Package fs provides filesystem-related functions.
Package fs provides filesystem-related functions.
Package sftp implements the SSH File Transfer Protocol as described in https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02
Package sftp implements the SSH File Transfer Protocol as described in https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02
examples/buffered-read-benchmark
buffered-read-benchmark benchmarks the peformance of reading from /dev/zero on the server to a []byte on the client via io.Copy.
buffered-read-benchmark benchmarks the peformance of reading from /dev/zero on the server to a []byte on the client via io.Copy.
examples/buffered-write-benchmark
buffered-write-benchmark benchmarks the peformance of writing a single large []byte on the client to /dev/null on the server via io.Copy.
buffered-write-benchmark benchmarks the peformance of writing a single large []byte on the client to /dev/null on the server via io.Copy.
examples/go-sftp-server
An example SFTP server implementation using the golang SSH package.
An example SFTP server implementation using the golang SSH package.
examples/request-server
An example SFTP server implementation using the golang SSH package.
An example SFTP server implementation using the golang SSH package.
examples/streaming-read-benchmark
streaming-read-benchmark benchmarks the peformance of reading from /dev/zero on the server to /dev/null on the client via io.Copy.
streaming-read-benchmark benchmarks the peformance of reading from /dev/zero on the server to /dev/null on the client via io.Copy.
examples/streaming-write-benchmark
streaming-write-benchmark benchmarks the peformance of writing from /dev/zero on the client to /dev/null on the server via io.Copy.
streaming-write-benchmark benchmarks the peformance of writing from /dev/zero on the client to /dev/null on the server via io.Copy.
internal/encoding/ssh/filexfer
Package filexfer implements the wire encoding for secsh-filexfer as described in https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02
Package filexfer implements the wire encoding for secsh-filexfer as described in https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02
internal/encoding/ssh/filexfer/openssh
Package openssh implements the openssh secsh-filexfer extensions as described in https://github.com/openssh/openssh-portable/blob/master/PROTOCOL
Package openssh implements the openssh secsh-filexfer extensions as described in https://github.com/openssh/openssh-portable/blob/master/PROTOCOL

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL