arkssh

package
v0.0.0-...-27a3170 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

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 IfSomeStrInLongString

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

*

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

func InitLogger

func InitLogger(enableDebugLogging bool, logFileName string)

func NewLogger

func NewLogger(enableDebugLogging bool, logFileName string) (*zap.Logger, error)

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 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"`
	EscalatePassword string `bson:"escalate_password,omitempty" json:"escalate_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"`
	Protocol         string `bson:"protocol,omitempty" json:"protocol,omitempty"`     // icmp|tcp|udp
	Reachability     bool   `bson:"reachability,string" json:"reachability,string"`   // 连通性
	NotPong          int    `bson:"not_pong,omitempty" json:"not_pong,omitempty"`     //探测主机22端口连续失败次数
	AgentName        string `bson:"agent_name,omitempty" json:"agent_name,omitempty"` //该设备属于哪个agent
	// 采集字段,长期保存
	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"` //是堆叠则为yes,否则没有该字段
	Mlag           string       `bson:"mlag,omitempty" json:"mlag,omitempty"`   //是m-lag则为yes,否则没有该字段
	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"`
	// 推送命令相关
	DebugMode                  bool                     `bson:"debug_mode,omitempty" json:"debug_mode,omitempty"`
	Cmds                       []string                 `bson:"cmds,omitempty" json:"cmds,omitempty"`
	CmdLevel                   string                   `bson:"cmd_level,omitempty" json:"cmd_level,omitempty"` // exec | configuration
	Timeout                    int                      `bson:"timeout,omitempty" json:"timeout,omitempty"`
	WriteMode                  string                   `bson:"write_mode,omitempty" json:"write_mode,omitempty"`                         //命令推送模式 dont_wait
	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结果
	TextFsmAttributeName       string                   `bson:"textfsm_attribute_name,omitempty" json:"textfsm_attribute_name,omitempty"` //如果不解压则使用该名称作为最外层map的key,通常用于写入数据库字段
	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"`
	UnknowTextFsmAttributeName []map[string]interface{} `bson:"unknow_text_fsm_attribute_name,omitempty" json:"unknow_text_fsm_attribute_name,omitempty"`
	// 登录验证相关
	LoginSuccessTimes       int `bson:"login_success_times,omitempty" json:"login_success_times,omitempty"`     //登录成功总次数
	LoginFailTimes          int `bson:"login_fail_times,omitempty" json:"login_fail_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) BeMQMsg

func (d *Device) BeMQMsg()

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

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 {
	ID         string `bson:"id,omitempty" json:"id,omitempty" unique:"true"` //设备id
	DCName     string `bson:"dc_name,omitempty" json:"dc_name,omitempty"`
	IP         string `bson:"ip,omitempty" json:"ip,omitempty"`
	Hostname   string `bson:"hostname,omitempty" json:"hostname,omitempty"`
	LastUpdate string `bson:"last_update,omitempty" json:"last_update,omitempty"`

	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"`
	AllowWithoutTo string   `bson:"allow_without_to,omitempty" json:"allow_without_to,omitempty"` //将to去除
	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"`
	PrivilegeExec                PrivilegeLevel `yaml:"privilege_exec"`
	Configuration                PrivilegeLevel `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"`
	PreviousLevel  string `yaml:"previous_level"`
	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) ReadChannelTimingWithBrand

func (s *SSHSession) ReadChannelTimingWithBrand(timeout int) (string, bool)

*

  • 从输出管道中读取设备返回的执行结果,若匹配到prompt则直接返回,否则输出流间隔超过timeout便会返回
  • @param timeout,超时时间
  • @return string,拼接的通道回显,bool若为false则表示超时退出
  • @author gulilin 2023/7/31 10:27

func (*SSHSession) ReadChannelTimingWithoutBrand

func (s *SSHSession) ReadChannelTimingWithoutBrand(timeout int) (string, bool)

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缓存中未使用超过6分钟的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