doumei1772 2019-05-19 12:35
浏览 48

进入lang + snmp大声笑(错误连接处理)

I do a server on golnag and I need to do a SNMP command to many devices, some of this devices could not have connections( or lost it temporary) And I want message " no connection" or "Error" and next continue the runing of programm

Ubuntu 18.04 // go 1.8 // go SNMP from https://github.com/soniah/gosnmp

package main

import (
    "fmt"
    "log"
    "time"
    g "github.com/soniah/gosnmp"
)

func main() {
    g.Default.Target = string("192.168.10.1")
    err := g.Default.Connect()
    if err != nil {
        log.Fatalf("Connect() err: %v", err)
        fmt.Println("Fail connection") // want this message in this case  !!!!!
    }
    defer g.Default.Conn.Close()

    oids := []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"} 
    result, err2 := g.Default.Get(oids)  
    if err2 != nil {
        log.Fatalf("Get() err: %v", err2)
        fmt.Println("Error read OID") // want this message in this case !!!!!
    }

    for i, variable := range result.Variables {
        fmt.Printf("%d: oid: %s ", i, variable.Name)

        switch variable.Type {
        case g.OctetString:
            fmt.Printf("string: %s
", string(variable.Value.([]byte)))
        default:
            fmt.Printf("number: %d
", g.ToBigInt(variable.Value))
        }
    }
}

I receive this result if no connection, and break the runing of pogramm

2019/05/19 15:11:33 Get() err: Request timeout (after 3 retries)
exit status 1

I want something like this

"Fail connection"   or  "Error read OID"

And next, I want to continue the runing of programm

  • 写回答

2条回答 默认 最新

  • doudou3716 2019-05-19 20:06
    关注

    You have unreachable code in lines where you've left comments.

    Just use correct string value for log.Fatalf:

    if err != nil {
      log.Fatalf("Fail connection, error: %v", err)
    }
    

    or if you don't want to exit:

    if err != nil {
      log.Printf("Fail connection, error: %v", err)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛