golang ssh host 連接華為NE5k路由器并支持more分頁符

  • 利用golang/x/crypto/ssh實(shí)現(xiàn)并參考作者: CodyGuo代碼
  • ssh登錄華為路由器執(zhí)行命令,可用作單臺(tái)設(shè)備執(zhí)行命令打印輸出之用
  • 對(duì)終端屏寬產(chǎn)生的分頁符做處理
  • github地址

use case

go run ./testConfig.go --username="aaa" --passwd='aaa' --ip_port="192.168.6.87" --cmd='display version'
-bash-4.3$ go run ./testConfig.go -h
Usage of /tmp/go-build918643740/command-line-arguments/_obj/exe/testConfig:
  -cmd string
        cmdstring (default "display arp statistics all")
  -ip_port string
        ip and port (default "1.1.1.1:22")
  -passwd string
        password (default "aaa")
  -username string
        username (default "aaa")
exit status 2

重點(diǎn)code說明

...
in <- "display version"  //執(zhí)行輸入goroutine
in <- "display arp statistics all"
...
go func() {
        for cmd := range in {
            wg.Add(1)
            w.Write([]byte(cmd + "\n"))
            wg.Wait() //控制每次goroutine執(zhí)行一條命令
        }
    }()
go func() {
        var (
            buf [1024 * 1024]byte
            t   int
        )
        for {
            n, err := r.Read(buf[t:])
            if err != nil {
                fmt.Println(err.Error())
                close(in)
                close(out)
                return
            }
            t += n
            result := string(buf[:t])
            //循環(huán)處理設(shè)備分頁符
            if strings.Contains(string(buf[t-n:t]), "More") {
                w.Write([]byte("\n"))
            }
            //匹配等待符完成一條操作清空goroutine
            if strings.Contains(result, "username:") ||
                strings.Contains(result, "password:") ||
                strings.Contains(result, ">") {
                out <- string(buf[:t])
                t = 0
                wg.Done()
            }
        }
    }()
...

TestUnit

display version
Huawei Versatile Routing Platform Software
VRP (R) software, Version 5.90 (NE40E&80E V600R003C00SPCa00)
Copyright (C) 2000-2012 Huawei Technologies Co., Ltd.
HUAWEI NE80E uptime is 1695 days, 20 hours, 57 minutes
NE80E version information:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

BKP 1 version information:
  PCB         Version : CR52BKPA REV B
  MPU  Slot  Quantity : 2
  SRU  Slot  Quantity : 0
  SFU  Slot  Quantity : 4
  LPU  Slot  Quantity : 16
...
...
...
<HK-HK-CW-1>
display arp statistics all
Dynamic: 345     Static: 0    

<HK-HK-CW-1>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,868評(píng)論 18 139
  • Coro-Telnet和Coro-Telnet+Golang-ssh-proxy性能測(cè)試 利用perl io::s...
    飛天神貓閱讀 1,074評(píng)論 0 2
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,025評(píng)論 25 708
  • 一陣秋風(fēng)打開了 一把大鎖 一個(gè)天空,兩個(gè)天空 苗條呢,像兩棵藍(lán)汪汪的樹 九月或者十月 雨水中帶靜電 滋滋,滋滋 你...
    我是蝎大人閱讀 116評(píng)論 0 2
  • 小時(shí)候,吹噓著要讓父母過上好生活,樹立天大的目標(biāo),長(zhǎng)大后真正實(shí)現(xiàn)的人又有多少呢?人就需要一種破釜沉舟的決心,努力...
    靚兮閱讀 199評(píng)論 0 0