switchgo

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	INFO    = 1
	DEBUG   = 2
	WARNING = 3
	ERROR   = 4
)
View Source
const (
	HUAWEI = "huawei"
	H3C    = "h3c"
	CISCO  = "cisco"
)
View Source
const LOGLEVEL = 4

Variables

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

Functions

func DebugLog

func DebugLog(format string, s ...interface{})

func ErrorLog

func ErrorLog(format string, s ...interface{})

func InfoLog

func InfoLog(format string, s ...interface{})

Types

type SSHConfig

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

SSH配置

func SSHConfigCreate

func SSHConfigCreate(user, password, hostname, port, brand string) (*SSHConfig, error)

func (*SSHConfig) GetSessionKey

func (config *SSHConfig) GetSessionKey() string

type SSHSession

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

*

  • 封装的ssh session,包含原生的ssh.Ssssion及其标准的输入输出管道,同时记录最后的使用时间
  • @attr session:原生的ssh session,in:绑定了session标准输入的管道,out:绑定了session标准输出的管道,lastUseTime:最后的使用时间
  • @author shenbowei

func NewSSHSession

func NewSSHSession(config *SSHConfig, brand string, manager *SessionManager) (*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) 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) 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) RunCmds

func (sshSession *SSHSession) RunCmds(cmds ...string) (string, error)

func (*SSHSession) RunCmdsAndClose

func (sshSession *SSHSession) RunCmdsAndClose(cmds ...string) (string, error)

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) DeleteSession

func (this *SessionManager) DeleteSession(sessionKey string)

func (*SessionManager) GetSSHSession

func (this *SessionManager) GetSSHSession(config *SSHConfig) (*SSHSession, error)

*

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

Jump to

Keyboard shortcuts

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