Skip to content

Instantly share code, notes, and snippets.

@rootVIII
Last active May 30, 2022 16:18
Show Gist options
  • Save rootVIII/497fafebc0e6b31956aac90bbc43ddf1 to your computer and use it in GitHub Desktop.
Save rootVIII/497fafebc0e6b31956aac90bbc43ddf1 to your computer and use it in GitHub Desktop.
Golang Scrape a Netgear Router's web UI, show devices on your network
package main
// rootVIII
// Netgear router scraper
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"time"
)
type router struct {
ip string
login string
password string
}
func scrapeUI(response string) [][]string {
result := regexp.MustCompile(`Info\W+\".*\)`)
matches := result.FindAllString(response, -1)
devices := make([][]string, 0)
for _, line := range matches {
device := strings.Replace(line[6:len(line)-1], "\"", "", -1)
devices = append(devices, strings.Split(device, ",")[:3])
}
return devices
}
func makeRequest(netgear router) (string, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", netgear.ip, nil)
req.SetBasicAuth(netgear.login, netgear.password)
resp, err := client.Do(req)
if err != nil {
return _, error
}
rText, err := ioutil.ReadAll(resp.Body)
return string(rText), _
}
// set environment variables prior to using
func main() {
netgear := router{
ip: os.Getenv("ROUTER_IP"),
login: os.Getenv("ROUTER_LOGIN"),
password: os.Getenv("ROUTER_PASSWD"),
}
fmt.Println(time.Now().Format(time.RFC850) + "\n")
makeRequest(netgear)
netgear.ip += "/RgAttachedDevices.asp"
var page, err = makeRequest(netgear)
if error != nil {
panic(err)
}
for _, deviceDetail := range scrapeUI(page) {
for _, info := range deviceDetail {
fmt.Println(info)
}
fmt.Println()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment