项目作者: shenbowei

项目描述 :
A packaged SSH library for switches (huawei,h3c,cisco)
高级语言: Go
项目地址: git://github.com/shenbowei/switch-ssh-go.git
创建时间: 2017-11-05T10:25:08Z
项目社区:https://github.com/shenbowei/switch-ssh-go

开源协议:MIT License

下载


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

  1. $ 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).

  1. //get the switch brand(vendor), include h3c,huawei and cisco
  2. brand, err := ssh.GetSSHBrand(user, password, ipPort)
  3. //run the cmds in the switch, and get the execution results
  4. result, err := ssh.RunCommands(user, password, ipPort, cmds...)
  5. //run the cmds in the switch with the device brand(the first connection will be faster), and get the execution results
  6. result, err := ssh.RunCommandsWithBrand(user, password, ipPort, ssh.CISCO, cmds...)

example

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/shenbowei/switch-ssh-go"
  5. )
  6. func main() {
  7. user := "your device ssh name"
  8. password := "your device ssh password"
  9. ipPort := "ip:22"
  10. //get the switch brand(vendor), include h3c,huawei and cisco
  11. brand, err := ssh.GetSSHBrand(user, password, ipPort)
  12. if err != nil {
  13. fmt.Println("GetSSHBrand err:\n", err.Error())
  14. }
  15. fmt.Println("Device brand is:\n", brand)
  16. //run the cmds in the switch, and get the execution results
  17. cmds := make([]string, 0)
  18. cmds = append(cmds, "dis clock")
  19. cmds = append(cmds, "dis vlan")
  20. result, err := ssh.RunCommands(user, password, ipPort, cmds...)
  21. if err != nil {
  22. fmt.Println("RunCommands err:\n", err.Error())
  23. }
  24. fmt.Println("RunCommands result:\n", result)
  25. }

Licenses

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