arkssh

package module
v0.0.0-...-2cbeef3 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 10 Imported by: 0

README

ing

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Adapter = make(map[string]PlatformConfig)
View Source
var GeneralPrompt string
View Source
var IsLogDebug bool

debug回显是否打印

Functions

func AddNewCipherAndKeyChain

func AddNewCipherAndKeyChain(yamlContent ...[]byte) (err error)

*

  • 增加额外的cipher和kex
  • @param yamlContent,可接受多个[]byte字节流
  • @return err
  • @author gulilin 2023/12/15 10:17

func AddNewPlatform

func AddNewPlatform(yamlContent ...[]byte) (err error)

*

  • 新增额外的平台适配参数
  • @param yamlContent,可接受多个[]byte字节流
  • @return err
  • @author gulilin 2023/12/13 17:40

func BulkRunCmd

func BulkRunCmd(devices []Device, timeOut int) error

*

  • 批量推送命令,带有brand的设备
  • @param devices,一组包含Device结构体实例的切片,timeOut,推送命令后等待的超时时间(秒)
  • @return 无,直接修改了原切片
  • @author gulilin 2023/7/10 16:12

func IfAinB

func IfAinB(A string, B []string) bool

*

  • 判断一个字符串是否在某个字符串切片中,若A为空字符串也返回false
  • @param A,目标字符串,集合,字符串切片
  • @return bool值
  • @author gulilin 2023/8/21 10:36

func IfSomeStrInLongString

func IfSomeStrInLongString(str string, sub ...string) bool

*

  • 查看某个长字符串中是否包含某几个字符串中的一个
  • @param str,目标字符串,sub,子字符串
  • @return 任意一个sub命中则返回true
  • @author gulilin 2023/12/13 17:09

func LogDebug

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

func LogError

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

func TextFsmParseViaContent

func TextFsmParseViaContent(waitToParse string, textFmsContent string) ([]map[string]interface{}, error)

*

  • 将文本通过textfsm模板解析,通过直接的外参作为模板内容进行解析
  • @param waitToParse-待解析的内容,textFmsContent-解析模板内容
  • @return 切片包map,key为属性名称,值为任意
  • @author gulilin 2023/11/9 11:08

func TextFsmParseViaTemplateFile

func TextFsmParseViaTemplateFile(brand string, waitToParse string, templateName string) ([]map[string]interface{}, error)

*

  • 将文本通过textfsm模板解析
  • @param brand,品牌名称,waitToParse,待解析的文本,templateName,解析模板名称
  • @return 切片包map,key为属性名称,值为任意
  • @author gulilin 2023/9/21 11:41

Types

type Configuration

type Configuration struct {
	Name           string `yaml:"name"`
	Pattern        string `yaml:"pattern"`
	PreviousLevel  string `yaml:"previous_level"`
	Deescalate     string `yaml:"deescalate"`
	Escalate       string `yaml:"escalate"`
	EscalateAuth   bool   `yaml:"escalate_auth"`
	EscalatePrompt string `yaml:"escalate_prompt"`
}

Configuration 配置模式下的参数

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"` //是否为生产环境测试用例使用设备
	// 采集字段,长期保存
	LastGatherTime string       `bson:"last_gather_time,omitempty" json:"last_gather_time,omitempty"`
	Hostname       string       `bson:"hostname,omitempty" json:"hostname,omitempty"`
	Role           string       `bson:"role,omitempty" json:"role,omitempty"`
	Stack          string       `bson:"stack,omitempty" json:"stack,omitempty"`
	Mlag           string       `bson:"mlag,omitempty" json:"mlag,omitempty"`
	IntfUpDowns    []IntfUpDown `bson:"intf_up_downs,omitempty" json:"intf_up_downs,omitempty"`
	IntfInfos      []IntfInfo   `bson:"intf_infos,omitempty" json:"intf_infos,omitempty"`
	// 巡检问题
	SingelPower bool `bson:"singel_power,omitempty" json:"singel_power,omitempty"`
	// 采集字段,临时保存
	MacTables  []MacTable `bson:"mac_tables,omitempty" json:"mac_tables,omitempty"`
	CanGetVolt string     `bson:"can_get_volt,omitempty" json:"can_get_volt,omitempty"` //是否能获取电压,ok
	Volt1      string     `bson:"volt_1,omitempty" json:"volt_1,omitempty"`
	Volt2      string     `bson:"volt_2,omitempty" json:"volt_2,omitempty"`
	// 推送命令相关
	Cmds                     []string               `bson:"cmds,omitempty" json:"cmds,omitempty"`
	Timeout                  int                    `bson:"timeout,omitempty" json:"timeout,omitempty"`
	DontWaitRes              bool                   `bson:"dont_wait_res,omitempty" json:"dont_wait_res,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"` //连续登录失败次数
	// 其他
	SearchVlanID string `bson:"search_vlan_id,omitempty" json:"search_vlan_id,omitempty"` //查询该vlan是否被使用
}

func (*Device) GetBrand

func (d *Device) GetBrand() (string, error)

*

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

func (*Device) LoginCheck

func (d *Device) LoginCheck() (bool, error)

*

  • 登录测试
  • @return bool,是否能登录
  • @author gulilin 2023/8/22 11:16

func (*Device) RunCmdWithBrand

func (d *Device) RunCmdWithBrand(timeOut int) error

*

  • 已知brand设备推送命令
  • @param timeOut,批量推送命令的超时时间
  • @return error,无错误则直接赋值传入的device
  • @author gulilin 2023/7/10 15:27

func (*Device) RunCmdWithoutBrand

func (d *Device) RunCmdWithoutBrand() error

*

  • 未知brand设备推送命令
  • @param 无
  • @return 无
  • @author gulilin 2023/7/10 15:27

type DevicePower

type DevicePower struct {
	IP       string `bson:"ip,omitempty" json:"ip,omitempty"`
	Hostname string `bson:"hostname,omitempty" json:"hostname,omitempty"`
	DCName   string `bson:"dc_name,omitempty" json:"dc_name,omitempty"`
	Records  []struct {
		LastGatherTime string `bson:"last_gather_time,omitempty" json:"last_gather_time,omitempty"`
		Volt1          string `bson:"volt_1,omitempty" json:"volt_1,omitempty"`
		Volt2          string `bson:"volt_2,omitempty" json:"volt_2,omitempty"`
		Changed        bool   `bson:"changed,omitempty" json:"changed,omitempty"`
		Same           bool   `bson:"same,omitempty" json:"same,omitempty"`
		AbNormal       bool   `bson:"ab_normal,omitempty" json:"ab_normal,omitempty"`
	} `bson:"records,omitempty" json:"records,omitempty"`
	AbNormalTimes int `bson:"ab_normal_times,omitempty" json:"ab_normal_times,omitempty"`
}

设备电源

type IntfInfo

type IntfInfo struct {
	PortName       string   `bson:"port_name,omitempty" json:"port_name,omitempty"`
	Description    string   `bson:"description,omitempty" json:"description,omitempty"`
	PortType       string   `bson:"port_type,omitempty" json:"port_type,omitempty"`           //trunk/access/l3
	AggPortID      string   `bson:"agg_port_id,omitempty" json:"agg_port_id,omitempty"`       //若该端口为聚合口,聚合口ID
	BeLongToAgg    string   `bson:"be_long_to_agg,omitempty" json:"be_long_to_agg,omitempty"` //若该端口为成员口,属于哪个聚合口
	PVID           string   `bson:"pvid,omitempty" json:"pvid,omitempty"`
	AllowTmp       []string `bson:"allow_tmp,omitempty" json:"allow_tmp,omitempty"` //临时存储,最终要二次处理成字符串
	Allow          string   `bson:"allow,omitempty" json:"allow,omitempty"`
	NotAllow       string   `bson:"not_allow,omitempty" json:"not_allow,omitempty"`
	AccessVlan     string   `bson:"access_vlan,omitempty" json:"access_vlan,omitempty"`
	UseTo          string   `bson:"use_to,omitempty" json:"use_to,omitempty"`                   //用途,srv,hub
	PhysicalOrAgg  string   `bson:"physical_or_agg,omitempty" json:"physical_or_agg,omitempty"` //物理口还是聚合口
	PhysicalStatus string   `bson:"physical_status,omitempty" json:"physical_status,omitempty"`
	ProtocolStatus string   `bson:"protocol_status,omitempty" json:"protocol_status,omitempty"`
}

端口的信息

type IntfUpDown

type IntfUpDown struct {
	PortName       string `bson:"port_name,omitempty" json:"port_name,omitempty"`
	PhysicalStatus string `bson:"physical_status,omitempty" json:"physical_status,omitempty"`
	ProtocolStatus string `bson:"protocol_status,omitempty" json:"protocol_status,omitempty"`
}

端口的状态,物理状态与协议状态

type LoginAccount

type LoginAccount struct {
	ID         string `bson:"id,omitempty" json:"id,omitempty"`
	CreateTime string `bson:"create_time,omitempty" json:"create_time,omitempty"`
	Username   string `bson:"username,omitempty" json:"username,omitempty"`
	Password   string `bson:"password,omitempty" json:"password,omitempty"`
}

设备常规的登录账号

type MacTable

type MacTable struct {
	LastGatherTime string `bson:"last_gather_time,omitempty" json:"last_gather_time,omitempty"`
	IP             string `bson:"ip,omitempty" json:"ip,omitempty"`
	Hostname       string `bson:"hostname,omitempty" json:"hostname,omitempty"`
	DCName         string `bson:"dc_name,omitempty" json:"dc_name,omitempty"`
	PortName       string `bson:"port_name,omitempty" json:"port_name,omitempty"`
	PhysicalOrAgg  string `bson:"physical_or_agg,omitempty" json:"physical_or_agg,omitempty"` //物理口还是聚合口
	AggPortID      string `bson:"agg_port_id,omitempty" json:"agg_port_id,omitempty"`         //若该端口为聚合口,聚合口ID
	BeLongToAgg    string `bson:"be_long_to_agg,omitempty" json:"be_long_to_agg,omitempty"`   //若该端口为成员口,属于哪个聚合口
	Vlan           string `bson:"vlan,omitempty" json:"vlan,omitempty"`
	MacAddress     string `bson:"mac_address,omitempty" json:"mac_address,omitempty"`
	UseTo          string `bson:"use_to,omitempty" json:"use_to,omitempty" ` //用途,srv,hub
	PhysicalStatus string `bson:"physical_status,omitempty" json:"physical_status,omitempty"`
}

mac地址表

type OneCMDRes

type OneCMDRes struct {
	RES    string `bson:"res,omitempty" json:"res,omitempty"`
	Status string `bson:"status,omitempty" json:"status,omitempty"`
}

单个命令的执行情况

type PlatformConfig

type PlatformConfig struct {
	PlatformType                 string         `yaml:"platform_type"`
	BrandCall                    string         `yaml:"brand_call"`
	BrandLabel                   []string       `yaml:"brand_label"`
	Exec                         PrivilegeLevel `yaml:"exec"`
	Configuration                Configuration  `yaml:"configuration"`
	DefaultDesiredPrivilegeLevel string         `yaml:"default_desired_privilege_level"`
	FailedWhenContains           []string       `yaml:"failed_when_contains"`
	NoPageCommand                string         `yaml:"no_page_command"`
}

平台配置

type PrivilegeLevel

type PrivilegeLevel struct {
	Name           string `yaml:"name"`
	Pattern        string `yaml:"pattern"`
	Deescalate     string `yaml:"deescalate"`
	Escalate       string `yaml:"escalate"`
	EscalateAuth   bool   `yaml:"escalate_auth"`
	EscalatePrompt string `yaml:"escalate_prompt"`
}

特权模式下的参数

type SSHOption

type SSHOption struct {
	Ciphers      []string `yaml:"ciphers,omitempty"`
	KeyExchanges []string `yaml:"key_exchanges,omitempty"`
}

ssh交换相关选项

var SSHOptions SSHOption

type SSHSession

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

*

  • 封装的ssh session,包含原生的ssh.Session及其标准的输入输出管道,同时记录最后的使用时间
  • @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) Close

func (s *SSHSession) Close()

*

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

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

func (s *SSHSession) UpdateLastUseTime()

*

  • 更新最后的使用时间

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的索引键值

type TVlan

type TVlan struct {
	IP            string `bson:"ip,omitempty" json:"ip,omitempty"`
	Brand         string `bson:"brand,omitempty" json:"brand,omitempty"`
	DCName        string `bson:"dc_name,omitempty" json:"dc_name,omitempty"`
	PortName      string `bson:"port_name,omitempty" json:"port_name,omitempty"`
	Description   string `bson:"description,omitempty" json:"description,omitempty"`
	PhysicalOrAgg string `bson:"physical_or_agg,omitempty" json:"physical_or_agg,omitempty"` //物理口还是聚合口
	UseTo         string `bson:"use_to,omitempty" json:"use_to,omitempty" `                  //用途,srv,hub
	Pvid          string `bson:"pvid,omitempty" json:"pvid,omitempty"`
	Allow         string `bson:"allow,omitempty" json:"allow,omitempty"`
	NotAllow      string `bson:"not_allow,omitempty" json:"not_allow,omitempty"`
	RightAllow    string `bson:"right_allow,omitempty" json:"right_allow,omitempty"`
	Status        string `bson:"status,omitempty" json:"status,omitempty"`
}

隧道vlan

Jump to

Keyboard shortcuts

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