ssh

package module
v0.0.0-...-5f3b67b Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2022 License: MIT Imports: 10 Imported by: 0

README

switch-ssh-go

A packaged SSH library for switches (huawei,h3c,cisco). A session pool is implemented to avoid repeated connection devices and automatically clear sessions that are not used for 10 minutes.

Installation

$ go get github.com/shenbowei/switch-ssh-go

Basic Usage

In Code

switch-ssh-go implemented a connection pool to save the session, and each session verifies its availability before executing the commands, so you can call the following method repeatedly (not repeatedly connecting the device).

//get the switch brand(vendor), include h3c,huawei and cisco
brand, err := ssh.GetSSHBrand(user, password, ipPort)

//run the cmds in the switch, and get the execution results
result, err := ssh.RunCommands(user, password, ipPort, cmds...)

//run the cmds in the switch with the device brand(the first connection will be faster), and get the execution results
result, err := ssh.RunCommandsWithBrand(user, password, ipPort, ssh.CISCO, cmds...)
example
package main

import (
    "fmt"
    "github.com/shenbowei/switch-ssh-go"
)

func main() {
    user := "your device ssh name"
    password := "your device ssh password"
    ipPort := "ip:22"

    //get the switch brand(vendor), include h3c,huawei and cisco
    brand, err := ssh.GetSSHBrand(user, password, ipPort)
    if err != nil {
        fmt.Println("GetSSHBrand err:\n", err.Error())
    }
    fmt.Println("Device brand is:\n", brand)

    //run the cmds in the switch, and get the execution results
    cmds := make([]string, 0)
    cmds = append(cmds, "dis clock")
    cmds = append(cmds, "dis vlan")
    result, err := ssh.RunCommands(user, password, ipPort, cmds...)
    if err != nil {
        fmt.Println("RunCommands err:\n", err.Error())
    }
    fmt.Println("RunCommands result:\n", result)
}

Licenses

switch-ssh-go is released under the MIT License.

Documentation

Index

Constants

View Source
const (
	// HUAWEI 华为
	HUAWEI = "huawei"
	// H3C 新华三
	H3C = "h3c"
	// CISCO 思科
	CISCO = "cisco"
	// TOPSEC 天融信
	TOPSEC = "topsec"
)

Variables

View Source
var (
	HuaweiNoPage = "screen-length 0 temporary"
	H3cNoPage    = "screen-length disable"
	CiscoNoPage  = "terminal length 0"
)
View Source
var IsLogDebug = true

Functions

func GetSSHBrand

func GetSSHBrand(user, password, ipPort string) (string, error)

*

  • 外部调用的统一方法,完成获取交换机的型号
  • @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口
  • @return 设备品牌(huawei,h3c,cisco,"")和执行错误
  • @author shenbowei

func LogDebug

func LogDebug(format string, a ...interface{})

func LogError

func LogError(format string, a ...interface{})

func RunCommands

func RunCommands(user, password, ipPort string, cmds ...string) (string, error)

*

  • 外部调用的统一方法,完成获取会话(若不存在,则会创建连接和会话,并存放入缓存),执行指令的流程,返回执行结果
  • @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口, cmds 执行的指令(可以多个)
  • @return 执行的输出结果和执行错误
  • @author shenbowei

func RunCommandsWithBrand

func RunCommandsWithBrand(user, password, ipPort, brand string, cmds ...string) (string, error)

*

  • 外部调用的统一方法,完成获取会话(若不存在,则会创建连接和会话,并存放入缓存),执行指令的流程,返回执行结果
  • @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口, brand 交换机品牌(可为空), cmds 执行的指令(可以多个)
  • @return 执行的输出结果和执行错误
  • @author shenbowei

func RunCommandsWithBrandAndCharset

func RunCommandsWithBrandAndCharset(user, password, ipPort, brand string, charset string, cmds ...string) (string, error)

RunCommandsWithBrandAndCharset 外部调用的统一方法,完成获取会话(若不存在,则会创建连接和会话,并存放入缓存),执行指令的流程,返回执行结果

Types

type SSHSession

type SSHSession struct {
	// contains filtered or unexported fields
}

SSHSession SSH会话 装的ssh session,包含原生的ssh.Ssssion及其标准的输入输出管道,同时记录最后的使用时间

func NewSSHSession

func NewSSHSession(user, password, ipPort string) (*SSHSession, error)

*

  • 创建一个SSHSession,相当于SSHSession的构造函数
  • @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口
  • @return 打开的SSHSession,执行的错误
  • @author shenbowei

func (*SSHSession) CheckSelf

func (this *SSHSession) CheckSelf() bool

*

  • 检查当前session是否可用
  • @return true:可用,false:不可用
  • @author shenbowei

func (*SSHSession) ClearChannel

func (this *SSHSession) ClearChannel()

*

  • 清除管道缓存的内容,避免管道中上次未读取的残余内容影响下次的结果

func (*SSHSession) Close

func (this *SSHSession) Close()

*

  • SSHSession的关闭方法,会关闭session和输入输出管道
  • @author shenbowei

func (*SSHSession) GetLastUseTime

func (this *SSHSession) GetLastUseTime() time.Time

*

  • 获取最后的使用时间
  • @return time.Time
  • @author shenbowei

func (*SSHSession) GetSSHBrand

func (this *SSHSession) GetSSHBrand() string

*

  • 获取当前SSH到的交换机的品牌
  • @return string (huawei,h3c,cisco)
  • @author shenbowei

func (*SSHSession) ReadChannelExpect

func (this *SSHSession) ReadChannelExpect(timeout time.Duration, expects ...string) string

*

  • 从输出管道中读取设备返回的执行结果,若输出流间隔超过timeout或者包含expects中的字符便会返回
  • @param timeout 从设备读取不到数据时的超时等待时间(超过超时等待时间即认为设备的响应内容已经被完全读取), expects...:期望得到的字符(可多个),得到便返回
  • @return 从输出管道读出的返回结果
  • @author shenbowei

func (*SSHSession) ReadChannelTiming

func (this *SSHSession) ReadChannelTiming(timeout time.Duration) string

*

  • 从输出管道中读取设备返回的执行结果,若输出流间隔超过timeout便会返回
  • @param timeout 从设备读取不到数据时的超时等待时间(超过超时等待时间即认为设备的响应内容已经被完全读取)
  • @return 从输出管道读出的返回结果
  • @author shenbowei

func (*SSHSession) UpdateLastUseTime

func (this *SSHSession) UpdateLastUseTime()

*

  • 更新最后的使用时间
  • @author shenbowei

func (*SSHSession) WriteChannel

func (this *SSHSession) WriteChannel(cmds ...string)

*

  • 向管道写入执行指令
  • @param cmds... 执行的命令(可多条)
  • @author shenbowei

type SessionManager

type SessionManager struct {
	// contains filtered or unexported fields
}

*

  • session(SSHSession)的管理类,会统一缓存打开的session,自动处理未使用超过10分钟的session
  • @attr sessionCache:缓存所有打开的map(10分钟内使用过的),sessionLocker设备锁,globalLocker全局锁
  • @author shenbowei

func NewSessionManager

func NewSessionManager() *SessionManager

*

  • 创建一个SessionManager,相当于SessionManager的构造函数
  • @return SessionManager实例
  • @author shenbowei

func (*SessionManager) GetSession

func (this *SessionManager) GetSession(user, password, ipPort, brand string) (*SSHSession, error)

*

  • 从缓存中获取session。如果不存在或者不可用,则重新创建
  • @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口
  • @return SSHSession
  • @author shenbowei

func (*SessionManager) GetSessionCache

func (this *SessionManager) GetSessionCache(sessionKey string) *SSHSession

func (*SessionManager) LockSession

func (this *SessionManager) LockSession(sessionKey string)

*

  • 给指定的session上锁
  • @param sessionKey:session的索引键值
  • @author shenbowei

func (*SessionManager) RunAutoClean

func (this *SessionManager) RunAutoClean()

*

  • 开始自动清理session缓存中未使用超过10分钟的session
  • @author shenbowei

func (*SessionManager) SetSessionCache

func (this *SessionManager) SetSessionCache(sessionKey string, session *SSHSession)

func (*SessionManager) UnlockSession

func (this *SessionManager) UnlockSession(sessionKey string)

*

  • 给指定的session解锁
  • @param sessionKey:session的索引键值
  • @author shenbowei

Jump to

Keyboard shortcuts

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