gssh

package module
v0.0.0-...-6061cb0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2019 License: Apache-2.0 Imports: 13 Imported by: 0

README

golang ssh client tool

支持通过ssh执行命令,上传、下载文件,并支持通过跳板执行私网地址服务器

Supports executing commands through ssh, uploading and downloading files, and supporting the execution of private network address servers through the springboard.

Installation

go get -u github.com/260by/gssh

Quick start

  1. ssh
package main

import (
	"fmt"
	"github.com/260by/gssh"
)

func main() {
	ssh := &gssh.Server{
		Options: gssh.ServerOptions{
			Addr: "192.168.1.173",
			Port: "22",
			User: "root",
			KeyFile: "/root/.ssh/id_rsa",
		},
	}

	stdout, err := ssh.Command("sudo /sbin/ifconfig")
	if err != nil {
		panic(err)
	}
	fmt.Println(stdout)
}
  1. sftp 支持文件或目录通配符(support file or directory wildcard)
package main

import (
	"fmt"
	"github.com/260by/gssh"
)

func main() {
	ssh := &gssh.Server{
		Options: gssh.ServerOptions{
			Addr: "192.168.1.173",
			Port: "22",
			User: "root",
			KeyFile: "/root/.ssh/id_rsa",
		},
	}

	// upload
	err := ssh.Put("tttt1111.txt", "/tmp")
	if err != nil {
	 	panic(err)
	}

	// download
	err := ssh.Get("/data/test-logs", "tmp")
	if err != nil {
		panic(err)
	}
}
  1. proxy
package main

import (
	"fmt"
	"github.com/260by/gssh"
)

func main() {
	ssh := &gssh.Server{
		Options: gssh.ServerOptions{
			Addr: "10.111.1.12",
			Port: "22",
			User: "root",
			KeyFile: "/root/.ssh/internal",
		},
		ProxyOptions: gssh.ServerOptions{
			Addr: "123.43.34.9",
			Port: "22",
			User: "root",
			KeyFile: "/root/.ssh/id_rsa",
		},
	}

	stdout, err := ssh.Command("ls /data/logs")
	if err != nil {
		panic(err)
	}
	fmt.Println(stdout)

	err = ssh.Get("/data/logs", "/tmp")
	if err != nil {
		panic(err)
	}

	err = ssh.Put("tmp/a.txt", "/data")
	if err != nil {
	    panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	Options      ServerOptions
	ProxyOptions ServerOptions
}

Server ssh配置信息

func (*Server) Command

func (s *Server) Command(command string) (string, error)

Command 执行命令

func (*Server) Get

func (s *Server) Get(src, dst string) (err error)

Get 使用sftp从远程服务器下载文件

func (*Server) Put

func (s *Server) Put(src, dst string) (err error)

Put 使用sftp从本地上传文件到远程服务器

type ServerOptions

type ServerOptions struct {
	Addr       string
	Port       string
	User       string
	Key        string
	KeyFile    string
	Password   string
	SocketFile string
	Timeout    time.Duration
}

ServerOptions ssh配置

func (ServerOptions) ToSSHClientConfig

func (opt ServerOptions) ToSSHClientConfig() (*ssh.ClientConfig, error)

ToSSHClientConfig 转换成SSHClientConfig

Directories

Path Synopsis
example
ssh

Jump to

Keyboard shortcuts

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