从SNMP PDU中获取八位字节字符串的gosnmp命令。

huangapple go评论145阅读模式
英文:

gosnmp get octetstring from SNMP PDU

问题

我正在尝试从gosnmp包返回的SNMP PDU中获取OctetString值。即使只有字节也可以。

以下是我的代码:

  1. package snmp_abstract
  2. import (
  3. "github.com/soniah/gosnmp"
  4. "time"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. type Switch struct {
  10. Hostname string
  11. Connection gosnmp.GoSNMP
  12. }
  13. var ConnectionParams = &gosnmp.GoSNMP{
  14. Target: "",
  15. Port: 161,
  16. Community: "community",
  17. Version: gosnmp.Version2c,
  18. Timeout: time.Duration(5) * time.Second,
  19. Logger: log.New(os.Stdout, "", 0),
  20. }
  21. type Mibs struct {
  22. VtpVlanState,
  23. Dot1dBasePortIfIndex string
  24. }
  25. var Default = &Mibs{
  26. VtpVlanState: "1.3.6.1.4.1.9.9.46.1.3.1.1.2",
  27. Dot1dBasePortIfIndex: "1.3.6.1.2.1.17.1.4.1.2",
  28. }
  29. func SNMPGet(conn *gosnmp.GoSNMP, host string, mib []string) {
  30. conn.Target = host
  31. log.Println(conn)
  32. err := conn.Connect()
  33. if err != nil {
  34. log.Printf("Unable to connect to %s\n", host)
  35. }
  36. defer conn.Conn.Close()
  37. res, err := conn.Get(mib)
  38. if err != nil {
  39. log.Println("GET error")
  40. log.Print(err)
  41. }
  42. log.Println(res)
  43. }
  44. func SNMPWalk(conn *gosnmp.GoSNMP, host string, mib string) []gosnmp.SnmpPDU {
  45. conn.Target = host
  46. log.Println(conn)
  47. err := conn.Connect()
  48. if err != nil {
  49. log.Printf("Unable to connect to %s\n", host)
  50. }
  51. defer conn.Conn.Close()
  52. res, err := conn.BulkWalkAll(mib)
  53. if err != nil {
  54. log.Println("GET error")
  55. log.Print(err)
  56. }
  57. return res
  58. }
  59. func (sw *Switch) Vlans() []string {
  60. res := SNMPWalk(&sw.Connection, sw.Hostname, Default.VtpVlanState)
  61. var vlans = make([]string, len(res))
  62. for i, vlan := range res {
  63. oidSlice := strings.Split(vlan.Name, ".")
  64. v := oidSlice[len(oidSlice)-1]
  65. vlans[i] = v
  66. }
  67. return vlans
  68. }
  69. func (sw *Switch) MapBPIIfindex(vlan string) {
  70. log.Println(vlan)
  71. s := *sw
  72. s.Connection.Community += "@" + vlan
  73. log.Println(s.Connection.Community)
  74. res := SNMPWalk(&s.Connection, s.Hostname, Default.Dot1dBasePortIfIndex)
  75. for _, p := range res {
  76. log.Println(p.Name)
  77. log.Println(p.Value)
  78. }
  79. }

当我使用MapBPIIfindex方法时,我得到以下输出:

  1. OID: [.1.3.6.1.2.1.31.1.1.1.1.10001]
  2. [decodeValue: type is OctetString]
  3. decodeValue: value is []interface {}{[]uint8{0x46, 0x61, 0x30, 0x2f, 0x31}}

现在,这应该包含一个OctetString。uint8字节应该解码为Fa0/1,但我无法做到这一点。

当我将log.Println(p.Value)更改为log.Println(p.Value.([]uint8))时,我得到以下错误:

  1. 2017/05/29 12:54:59 .1.3.6.1.2.1.17.1.4.1.2
  2. panic: interface conversion: interface {} is nil, not []uint8

我该如何获取这个值?文档对此并不是很清楚。

英文:

I am trying to get the OctetString value from an SNMP PDU returned by the gosnmp package. Even the bytes would suffice.

Here is my code:

  1. package snmp_abstract
  2. import (
  3. "github.com/soniah/gosnmp"
  4. "time"
  5. "log"
  6. "os"
  7. "strings"
  8. )
  9. type Switch struct {
  10. Hostname string
  11. Connection gosnmp.GoSNMP
  12. }
  13. var ConnectionParams = &gosnmp.GoSNMP{
  14. Target: "",
  15. Port: 161,
  16. Community: "community",
  17. Version: gosnmp.Version2c,
  18. Timeout: time.Duration(5) * time.Second,
  19. Logger: log.New(os.Stdout, "", 0),
  20. }
  21. type Mibs struct {
  22. VtpVlanState,
  23. Dot1dBasePortIfIndex string
  24. }
  25. var Default = &Mibs{
  26. VtpVlanState: "1.3.6.1.4.1.9.9.46.1.3.1.1.2",
  27. Dot1dBasePortIfIndex: "1.3.6.1.2.1.17.1.4.1.2",
  28. }
  29. func SNMPGet(conn *gosnmp.GoSNMP, host string, mib []string) {
  30. conn.Target = host
  31. log.Println(conn)
  32. err := conn.Connect()
  33. if err != nil {
  34. log.Printf("Unable to connect to %s\n", host)
  35. }
  36. defer conn.Conn.Close()
  37. res, err := conn.Get(mib)
  38. if err != nil {
  39. log.Println("GET error")
  40. log.Print(err)
  41. }
  42. log.Println(res)
  43. }
  44. func SNMPWalk(conn *gosnmp.GoSNMP, host string, mib string) []gosnmp.SnmpPDU {
  45. conn.Target = host
  46. log.Println(conn)
  47. err := conn.Connect()
  48. if err != nil {
  49. log.Printf("Unable to connect to %s\n", host)
  50. }
  51. defer conn.Conn.Close()
  52. res, err := conn.BulkWalkAll(mib)
  53. if err != nil {
  54. log.Println("GET error")
  55. log.Print(err)
  56. }
  57. return res
  58. }
  59. func (sw *Switch) Vlans() []string {
  60. res := SNMPWalk(&sw.Connection, sw.Hostname, Default.VtpVlanState)
  61. var vlans = make([]string, len(res))
  62. for i, vlan := range res {
  63. oidSlice := strings.Split(vlan.Name, ".")
  64. v := oidSlice[len(oidSlice)-1]
  65. vlans[i] = v
  66. }
  67. return vlans
  68. }
  69. func (sw *Switch) MapBPIIfindex(vlan string) {
  70. log.Println(vlan)
  71. s := *sw
  72. s.Connection.Community += "@" + vlan
  73. log.Println(s.Connection.Community)
  74. res := SNMPWalk(&s.Connection, s.Hostname, Default.Dot1dBasePortIfIndex)
  75. for _, p := range res {
  76. log.Println(p.Name)
  77. log.Println(p.Value)
  78. }
  79. }

When I use the MapBPIIfindex method I get the following output:

  1. OID: [.1.3.6.1.2.1.31.1.1.1.1.10001]
  2. [decodeValue: type is OctetString]
  3. decodeValue: value is []interface {}{[]uint8{0x46, 0x61, 0x30, 0x2f, 0x31}}

Now, this should contain an OctetString. The uint8 bytes should decode to Fa0/1, but I am not able to do this.

When I change log.Println(p.Value) to log.Println(p.Value.([]uint8)), I get the following error:

  1. 2017/05/29 12:54:59 .1.3.6.1.2.1.17.1.4.1.2
  2. panic: interface conversion: interface {} is nil, not []uint8

How can I get this value? The documentation is not so clear on this.

答案1

得分: 1

这是ASCII码。你需要将你的十六进制输出转换一下。
我对GO语言没有任何了解。
https://en.wikipedia.org/wiki/ASCII

英文:

this is ASCII. You need to convert your HEX output.
I don't have any knowledge of GO.
https://en.wikipedia.org/wiki/ASCII

huangapple
  • 本文由 发表于 2017年5月29日 18:56:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/44240806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定