Documentation ¶
Index ¶
- Constants
- Variables
- func BulkRunCmd(devices []Device, timeOut int) error
- func LogDebug(format string, a ...interface{})
- func LogError(format string, a ...interface{})
- func TextFsmParseViaContent(waitToParse string, textFmsContent string) ([]map[string]interface{}, error)
- func TextFsmParseViaTemplateFile(brand string, waitToParse string, templateName string) ([]map[string]interface{}, error)
- type Device
- type OneCMDRes
- type SSHSession
- func (s *SSHSession) CheckSelf() bool
- func (s *SSHSession) ClearChannel()
- func (s *SSHSession) Close()
- func (s *SSHSession) GetLastUseTime() time.Time
- func (s *SSHSession) GetSSHBrand() string
- func (s *SSHSession) ReadChannelExpect(timeout time.Duration, expects ...string) string
- func (s *SSHSession) ReadChannelTiming(timeout int) (string, bool)
- func (s *SSHSession) UpdateLastUseTime()
- func (s *SSHSession) WriteChannel(cmds ...string)
- type SessionManager
- func (s *SessionManager) GetSession(user, password, ipPort, brand string) (*SSHSession, error)
- func (s *SessionManager) GetSessionCache(sessionKey string) *SSHSession
- func (s *SessionManager) LockSession(sessionKey string)
- func (s *SessionManager) RunAutoClean()
- func (s *SessionManager) SetSessionCache(sessionKey string, session *SSHSession)
- func (s *SessionManager) UnlockSession(sessionKey string)
Constants ¶
View Source
const ( HUAWEI = "huawei" HUARONG = "huarong" FutureMatrix = "futurematrix" AnShi = "anshi" SANGFOR = "sangfor" H3C = "h3c" CISCO = "cisco" LINUX = "linux" DIPU = "dptech" ZTE = "zte" PROMPT = `\n<[^!]{1,100}>\s*$|\n\[[^\]]{1,100}\]\s*$|\n[^!\n ]{3,30}[^ \n]#\s*$|\n.*\[[^\]]{1,100}\]#\s*$` )
Variables ¶
View Source
var ( HuaweiNoPage = "screen-length 0 temporary" H3cNoPage = "screen-length disable" CiscoNoPage = "terminal length 0" SangforNoPage = "terminal length 0" DiPuNoPage = "terminal line 0" ZTENoPage = "terminal length 0" AnshiNoPage = "" LinuxNopage = "" )
View Source
var IsLogDebug bool
Functions ¶
func BulkRunCmd ¶
*
- 批量推送命令,带有brand的设备
- @param devices,一组包含Device结构体实例的切片,timeOut,推送命令后等待的超时时间(秒)
- @return 无,直接修改了原切片
- @author gulilin 2023/7/10 16:12
Types ¶
type Device ¶
type Device struct { ID string `bson:"id,omitempty" json:"id,omitempty" unique:"true"` DCName string `bson:"dc_name,omitempty" json:"dc_name,omitempty"` DCType string `bson:"dc_type,omitempty" json:"dc_type,omitempty"` IP string `bson:"ip,omitempty" json:"ip,omitempty"` Port string `bson:"port,omitempty" json:"port,omitempty"` Username string `bson:"username,omitempty" json:"username,omitempty"` Password string `bson:"password,omitempty" json:"password,omitempty"` Brand string `bson:"brand,omitempty" json:"brand,omitempty"` Status string `bson:"status,omitempty" json:"status,omitempty"` SSHStatus string `bson:"ssh_status,omitempty" json:"ssh_status,omitempty"` WEBStatus string `bson:"web_status,omitempty" json:"web_status,omitempty"` CreateTime string `bson:"create_time,omitempty" json:"create_time,omitempty"` LastUpdate string `bson:"last_update,omitempty" json:"last_update,omitempty"` NotPong int `bson:"not_pong,omitempty" json:"not_pong,omitempty"` //探测主机22端口连续失败次数 ProdTestSimple bool `bson:"prod_test_simple,omitempty" json:"prod_test_simple,omitempty"` //是否为生产环境测试用例使用设备.. // 推送命令相关 Cmds []string `bson:"cmds,omitempty" json:"cmds,omitempty"` Timeout int `bson:"timeout,omitempty" json:"timeout,omitempty"` SendStatus string `bson:"send_status,omitempty" json:"send_status,omitempty"` //命令推送的状态,成功为success RawResult string `bson:"raw_result,omitempty" json:"raw_result,omitempty"` //原始的回显 MapResult map[string]OneCMDRes `bson:"map_result,omitempty" json:"map_result,omitempty"` //origin_ssh使用 ResultMap map[string]string `bson:"result,omitempty" json:"result,omitempty"` //scrapli_ssh使用,将每行命令为key,结果为value写入map Detect string `bson:"detect,omitempty" json:"detect,omitempty"` UnzipTextFsmResults bool `bson:"unzip_text_fsm_results,omitempty" json:"unzip_text_fsm_results,omitempty"` //是否解压TextFsmResults结果 TextFsmTemplateFilenames []string `bson:"textfsm_templates,omitempty" json:"textfsm_templates,omitempty"` TextFsmContent string `bson:"textfsm_content,omitempty" json:"textfsm_content,omitempty"` TextFsmResults map[string]interface{} `bson:"textfsm_results,omitempty" json:"textfsm_results,omitempty"` // 登录验证相关 LoginSuccessTimes int `bson:"login_success_times,omitempty" json:"login_success_times,omitempty"` //登录成功次数 LoginTotalTimes int `bson:"login_total_times,omitempty" json:"login_total_times"` //登录总次数 LoginFailContinuesTimes int `bson:"login_fail_continues_times,omitempty" json:"login_fail_continues_times"` //连续登录失败次数 }
func (*Device) GetBrand ¶
*
- 外部调用的统一方法,完成获取交换机的型号
- @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口
- @return 设备品牌(huawei,h3c,cisco,"")和执行错误
- @author gulilin 2023/7/10 15:27
func (*Device) RunCmdWithBrand ¶
*
- 已知brand设备推送命令
- @param timeOut,批量推送命令的超时时间
- @return error,无错误则直接赋值传入的device
- @author gulilin 2023/7/10 15:27
func (*Device) RunCmdWithoutBrand ¶
*
- 未知brand设备推送命令
- @param 无
- @return 无
- @author gulilin 2023/7/10 15:27
type OneCMDRes ¶
type OneCMDRes struct { RES string `bson:"res,omitempty" json:"res,omitempty"` Status string `bson:"status,omitempty" json:"status,omitempty"` }
单个命令的执行情况
type SSHSession ¶
type SSHSession struct {
// contains filtered or unexported fields
}
*
- 封装的ssh session,包含原生的ssh.Ssssion及其标准的输入输出管道,同时记录最后的使用时间
- @attr session:原生的ssh session,in:绑定了session标准输入的管道,out:绑定了session标准输出的管道,lastUseTime:最后的使用时间
func NewSSHSession ¶
func NewSSHSession(user, password, ipPort string) (*SSHSession, error)
*
- 创建一个SSHSession,相当于SSHSession的构造函数
- @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口
- @return 打开的SSHSession,执行的错误
func (*SSHSession) CheckSelf ¶
func (s *SSHSession) CheckSelf() bool
*
- 检查当前session是否可用,通过向管道中发送一个回车,若匹配到字符则表示当前管道可用
- @return bool
- @author gulilin 2023/8/7 17:48
func (*SSHSession) ClearChannel ¶
func (s *SSHSession) ClearChannel()
*
- 清除管道缓存的内容,避免管道中上次未读取的残余内容影响下次的结果
func (*SSHSession) GetLastUseTime ¶
func (s *SSHSession) GetLastUseTime() time.Time
*
- 获取最后的使用时间
- @return time.Time
func (*SSHSession) GetSSHBrand ¶
func (s *SSHSession) GetSSHBrand() string
*
- 获取当前SSH到的交换机的品牌
- @return string (huawei,h3c,cisco)
func (*SSHSession) ReadChannelExpect ¶
func (s *SSHSession) ReadChannelExpect(timeout time.Duration, expects ...string) string
*
- 从输出管道中读取设备返回的执行结果,若输出流间隔超过timeout或者包含expects中的字符便会返回
- @param timeout 从设备读取不到数据时的超时等待时间(超过超时等待时间即认为设备的响应内容已经被完全读取), expects...:期望得到的字符(可多个),得到便返回
- @return 从输出管道读出的返回结果
- @author gulilin 2023/7/31 10:31
func (*SSHSession) ReadChannelTiming ¶
func (s *SSHSession) ReadChannelTiming(timeout int) (string, bool)
*
- 从输出管道中读取设备返回的执行结果,若匹配到prompt则直接返回,否则输出流间隔超过timeout便会返回
- @param timeout,超时时间
- @return string,拼接的通道回显,bool若为false则表示超时退出
- @author gulilin 2023/7/31 10:27
func (*SSHSession) WriteChannel ¶
func (s *SSHSession) WriteChannel(cmds ...string)
*
- 向管道写入执行指令
- @param cmds... 执行的命令(可多条)
type SessionManager ¶
type SessionManager struct {
// contains filtered or unexported fields
}
*
- session(SSHSession)的管理类,会统一缓存打开的session,自动处理未使用超过10分钟的session
- @attr sessionCache:缓存所有打开的map(10分钟内使用过的),sessionLocker设备锁,globalLocker全局锁
func NewSessionManager ¶
func NewSessionManager() *SessionManager
*
- 创建一个SessionManager,相当于SessionManager的构造函数
- @return SessionManager实例
func (*SessionManager) GetSession ¶
func (s *SessionManager) GetSession(user, password, ipPort, brand string) (*SSHSession, error)
*
- 从缓存中获取session。如果不存在或者不可用,则重新创建
- @param user ssh连接的用户名, password 密码, ipPort 交换机的ip和端口
- @return SSHSession
func (*SessionManager) GetSessionCache ¶
func (s *SessionManager) GetSessionCache(sessionKey string) *SSHSession
func (*SessionManager) LockSession ¶
func (s *SessionManager) LockSession(sessionKey string)
*
- 给指定的session上锁
- @param sessionKey:session的索引键值
func (*SessionManager) RunAutoClean ¶
func (s *SessionManager) RunAutoClean()
*
- 开始自动清理session缓存中未使用超过10分钟的session
func (*SessionManager) SetSessionCache ¶
func (s *SessionManager) SetSessionCache(sessionKey string, session *SSHSession)
func (*SessionManager) UnlockSession ¶
func (s *SessionManager) UnlockSession(sessionKey string)
*
- 给指定的session解锁
- @param sessionKey:session的索引键值
Click to show internal directories.
Click to hide internal directories.