Skip to content

Instantly share code, notes, and snippets.

@simonszu
Created June 16, 2015 17:39
Show Gist options
  • Save simonszu/51957ff0ee68aa6bc5c7 to your computer and use it in GitHub Desktop.
Save simonszu/51957ff0ee68aa6bc5c7 to your computer and use it in GitHub Desktop.
Playing around with Go and SNMP
package main
import "fmt"
import "os"
import "regexp"
import "strconv"
import gosnmp "github.com/alouca/gosnmp"
func interfacecheck(ip string) {
fmt.Println("Checking " + ip)
//Create a new snmp client
snmp, _ := gosnmp.NewGoSNMP(ip, "EcofisVC", gosnmp.Version2c, 5)
//Lets get the hostname first
//172.30.40.73
resp, err := snmp.Get(".1.3.6.1.2.1.1.5.0")
if err == nil {
for _, v := range resp.Variables {
fmt.Println( v.Type)
fmt.Println(string(v.Value))
}
}
}
func main() {
// First, declare which IP may be valid
var validIP = regexp.MustCompile(`^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$`)
// Get the IP from the command line
ip := os.Args[1]
//If the IP matches the regexp
if validIP.MatchString(ip){
//Check each octet
matches := validIP.FindStringSubmatch(ip)
for i:=1; i<len(matches); i++{
if octet, _ := strconv.Atoi(matches[i]); octet<0 || octet>255{
//If the octet is too big, exit
fmt.Println(ip + " is not a valid IP")
os.Exit(2)
}
}
// Else do the check
interfacecheck(ip)
// If the IP does not match the regexp
} else {
fmt.Println(ip + " is not a valid IP")
os.Exit(2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment