gossh

package module
v0.0.0-...-de718b2 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: MIT Imports: 5 Imported by: 0

README

gossh

golang实现的一个ssh远程执行小工具

  • 依赖:
    require (
      github.com/pkg/sftp v1.13.5 // sftp连接工具包
      golang.org/x/crypto v0.11.0 // ssh连接工具包
    )
    

使用示例

  1. 使用username+password连接到远端服务器,并执行shell

    func TestGetRemote1(t *testing.T) {
      con, err := gossh.Remote1("root", "password", "远端服务器ip:22")
      if err != nil {
        t.Error(err)
        return
      }
      defer con.Close()
      s, err2 := con.ExecShell(context.Background(), "df -h")
      if err2 != nil {
        t.Error(err2)
        return
      }
      t.Logf(s)
    }
    
  2. 使用username+私钥连接到远端服务器,并执行shell

    func TestGetRemote2(t *testing.T) {
      con, err := gossh.Remote2("root", "/root/.ssh/id_rsa", "xxx.xxx.xxx.xxx:22")
      if err != nil {
        t.Error(err)
        return
      }
      defer con.Close()
      s, err2 := con.ExecShell(context.Background(), "df -h")
      if err2 != nil {
        t.Error(err2)
        return
      }
      t.Logf(s)
    }
    
  3. 本地执行shell

    func TestGetLocal(t *testing.T) {
      l := gossh.Local()
      defer l.Close()
      s, err := l.ExecShell(context.Background(), "df -h")
      if err != nil {
        t.Error(err)
        return
      }
      t.Logf(s)
    }
    
  4. 使用默认方式远程连接到远端服务器,并执行shell

    func TestGetRemoteDefault(t *testing.T) {
      con, err := gossh.RemoteDefault("目标机器ip:22")
      if err != nil {
        t.Error(err)
        return
      }
      defer con.Close()
      s, err2 := con.ExecShell(context.Background(), "df -h")
      if err2 != nil {
        t.Error(err2)
        return
      }
      t.Logf(s)
    }
    

    注意:该方式需要将下面公钥添加到目标机器的/root/.ssh/authorized_keys文件中

    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDP3qG9zzNQkhKuWWJTCHAuY04bQ9h/vhZplVrTSnEoWL1SsbT7v/dCDXuyazNDo9ikd9BS6H/nE5lOKp+Omi1W2uWs/kxrCNhouXRO8kMLViRB3DRP2VYFDo36UafzNdGKkH/vW4Ptilga/ucForW05SindT3KeKf+tB1u3RBlRz6rzpeuqrflVtcaWtQ33exWMO8CxgzCtsDexWWLP+TLdeOaWyfn0hj4tf36+K7oENAzGGhQuEwETiMUkJKfykBThBenWgU9mM1/5VbgvGiW7xIoeyDX8RI6Lz5q8mb3+ajuEqPyX/qwiNasYkQ7bWGaLDAVF3yJ20w7EpP54yi9rEoiBt6GAEo2JX5OuibzwMsz2CCykiB8H4YyiOlBY0q5GwrXC87fslvEt4KcYdk/XrZT+ikrJePgTCbQJhUGf8yYe1aDKoTBf/uIuT/O7aJ49KxnfeQdxel3xIykyROxnNisQ8Iz3vdC/QZlZsQUnJzo0UXtwDpwmwLwjpLFCMM= [email protected]
    
  5. 文件操作

TODO

  • 增加耗时监控

Documentation

Overview

* @Author: duanzt * @Date: 2023-07-14 10:26:52 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 18:54:52 * @FilePath: gossh.go * @Description: 暴露文件,提供使用的方法 * * Copyright (c) 2023 by duanzt, All Rights Reserved.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Local

func Local() internal.IConnection

Local 获取本地ssh连接()

@author duanzt
@date 2023-07-14 05:05:44
@return internal.IConnection

func Remote1

func Remote1(username, password, addr string) (internal.IConnection, error)

Remote1 获取远程ssh连接(使用username+password验证方式)

@author duanzt
@date 2023-07-14 04:59:22
@param username string 用户名
@param password string 密码
@param addr string ssh连接地址,例如:192.168.10.100:22(只传入ip的情况会默认使用22端口)
@return internal.IConnection ssh连接
@return error 连接异常时返回

func Remote2

func Remote2(username, privateKey, addr string) (internal.IConnection, error)

Remote2 获取远程ssh连接(使用username+私钥验证方式)

@author duanzt
@date 2023-07-14 05:01:36
@param user string 用户名
@param privateKey string 私钥地址
@param addr string  ssh连接地址,例如:192.168.10.100:22(只传入ip的情况会默认使用22端口)
@return internal.IConnection ssh连接
@return error 连接异常时返回

func RemoteDefault

func RemoteDefault(addr string) (internal.IConnection, error)

RemoteDefault 获取远程ssh连接(使用username+默认私钥验证方式) 注意:请添加以下公钥到目标机器的/root/.ssh/authorized_keys文件中 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDP3qG9zzNQkhKuWWJTCHAuY04bQ9h/vhZplVrTSnEoWL1SsbT7v/dCDXuyazNDo9ikd9BS6H/nE5lOKp+Omi1W2uWs/kxrCNhouXRO8kMLViRB3DRP2VYFDo36UafzNdGKkH/vW4Ptilga/ucForW05SindT3KeKf+tB1u3RBlRz6rzpeuqrflVtcaWtQ33exWMO8CxgzCtsDexWWLP+TLdeOaWyfn0hj4tf36+K7oENAzGGhQuEwETiMUkJKfykBThBenWgU9mM1/5VbgvGiW7xIoeyDX8RI6Lz5q8mb3+ajuEqPyX/qwiNasYkQ7bWGaLDAVF3yJ20w7EpP54yi9rEoiBt6GAEo2JX5OuibzwMsz2CCykiB8H4YyiOlBY0q5GwrXC87fslvEt4KcYdk/XrZT+ikrJePgTCbQJhUGf8yYe1aDKoTBf/uIuT/O7aJ49KxnfeQdxel3xIykyROxnNisQ8Iz3vdC/QZlZsQUnJzo0UXtwDpwmwLwjpLFCMM= [email protected]

@author duanzt
@date 2023-07-14 06:24:43
@param addr string
@return internal.IConnection
@return error

Types

This section is empty.

Directories

Path Synopsis
* @Author: duanzt * @Date: 2023-07-14 09:41:38 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 13:10:00 * @FilePath: iconnection.go * @Description: 定义connection interface * * Copyright (c) 2023 by duanzt, All Rights Reserved.
* @Author: duanzt * @Date: 2023-07-14 09:41:38 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 13:10:00 * @FilePath: iconnection.go * @Description: 定义connection interface * * Copyright (c) 2023 by duanzt, All Rights Reserved.
local
* @Author: duanzt * @Date: 2023-07-14 10:27:45 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 18:53:14 * @FilePath: connection.go * @Description: 本地连接(逻辑上,并没有建立任何连接) * * Copyright (c) 2023 by duanzt, All Rights Reserved.
* @Author: duanzt * @Date: 2023-07-14 10:27:45 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 18:53:14 * @FilePath: connection.go * @Description: 本地连接(逻辑上,并没有建立任何连接) * * Copyright (c) 2023 by duanzt, All Rights Reserved.
remote
* @Author: duanzt * @Date: 2023-07-14 10:27:51 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 13:12:18 * @FilePath: connection.go * @Description: 远程ssh连接 * * Copyright (c) 2023 by duanzt, All Rights Reserved.
* @Author: duanzt * @Date: 2023-07-14 10:27:51 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 13:12:18 * @FilePath: connection.go * @Description: 远程ssh连接 * * Copyright (c) 2023 by duanzt, All Rights Reserved.
tools
* @Author: duanzt * @Date: 2023-07-14 18:21:26 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 13:16:55 * @FilePath: filetools.go * @Description: 文件处理工具 * * Copyright (c) 2023 by duanzt, All Rights Reserved.
* @Author: duanzt * @Date: 2023-07-14 18:21:26 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 13:16:55 * @FilePath: filetools.go * @Description: 文件处理工具 * * Copyright (c) 2023 by duanzt, All Rights Reserved.
unit
example/localfile
* @Author: duanzt * @Date: 2023-07-17 12:30:24 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 16:07:59 * @FilePath: main.go * @Description: 本地连接操作文件的使用示例 * * Copyright (c) 2023 by duanzt, All Rights Reserved.
* @Author: duanzt * @Date: 2023-07-17 12:30:24 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 16:07:59 * @FilePath: main.go * @Description: 本地连接操作文件的使用示例 * * Copyright (c) 2023 by duanzt, All Rights Reserved.
example/processon
* @Author: duanzt * @Date: 2023-07-17 12:23:38 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 12:43:27 * @FilePath: main.go * @Description: 进度条的使用示例 * * Copyright (c) 2023 by duanzt, All Rights Reserved.
* @Author: duanzt * @Date: 2023-07-17 12:23:38 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 12:43:27 * @FilePath: main.go * @Description: 进度条的使用示例 * * Copyright (c) 2023 by duanzt, All Rights Reserved.
example/remotefile
* @Author: duanzt * @Date: 2023-07-17 16:04:32 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 16:17:36 * @FilePath: main.go * @Description: 远程ssh连接操作文件的使用示例 * * Copyright (c) 2023 by duanzt, All Rights Reserved.
* @Author: duanzt * @Date: 2023-07-17 16:04:32 * @LastEditors: duanzt * @LastEditTime: 2023-07-17 16:17:36 * @FilePath: main.go * @Description: 远程ssh连接操作文件的使用示例 * * Copyright (c) 2023 by duanzt, All Rights Reserved.

Jump to

Keyboard shortcuts

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