Geräteliste des Netzwerkes auslesen/exportieren

1 year ago

Hallo zusammen,


aktuell bastele ich mir eine Übersicht über mein Netzwerk (für die Technikinteressierten: mit Prometheus und Grafana). Alles was mir jetzt noch fehlt, ist eine Liste mit Geräten, die mit meinem Netzwerk verbunden sind, sowie Up- und Downloads,... sprich alles, was man auch unter der Seite /html/content/network/devices.html? im Router findet.


Ich verzweifele gerade langsam daran, die Daten über ein Python Skript auszulesen. Vielleicht hat hier jemand sich schon genau so etwas schon mal gemacht.


Falls noch Infos zum derzeitigen Tech-Stack gesucht werden:

Smartport Smart 4
Python 3.10
Prometheus
Grafana
Docker
PI OS 64-bit
Raspberry Pi 4 B

156

6

    • 1 year ago

      Mit so was bist du im Heise-Forum vermutlich besser aufgehoben als hier Zwinkernd

       

      Viele Grüße

      Thomas

      0

    • 1 year ago

      kottleddo

      sprich alles, was man auch unter der Seite /html/content/network/devices.html? im Router findet.

      sprich alles, was man auch unter der Seite /html/content/network/devices.html? im Router findet.
      kottleddo
      sprich alles, was man auch unter der Seite /html/content/network/devices.html? im Router findet.

      Mal abgesehen davon, dass ich keinen Smart 4 habe und ich auch kein Interesse an den Daten habe, die Du sammeln willst - vielleicht wäre es eine gute Idee

      1. zu schreiben was da an welcher Stelle schiefgeht, welche Fehlermeldungen ggf. kommen
      2. das Skript selbst zu posten

      1

      Answer

      from

      1 year ago

      from prometheus_client import start_http_server, Gauge
      import requests
      from requests.auth import HTTPBasicAuth
      from bs4 import BeautifulSoup

      # Router credentials and URL
      router_url = '
      password = ''

      # Prometheus metrics
      download_speed = Gauge('router_download_speed', 'Download speed (Mbps)', ['device_name', 'ip_address', 'connection_type'])
      upload_speed = Gauge('router_upload_speed', 'Upload speed (Mbps)', ['device_name', 'ip_address', 'connection_type'])
      connection_type_metric = Gauge('router_connection_type', 'Connection type', ['device_name', 'ip_address'])

      # Function to extract metrics from device information page
      def extract_metrics(html_content):
      # Parse HTML using BeautifulSoup
      soup = BeautifulSoup(html_content, 'html.parser', from_encoding="utf-8")

      # Find all devices in the device list
      devices = soup.find_all('div', class_='form-internal-content')

      # Extract metrics for each device
      for device in devices:
      device_name = device.find('div', class_='headlineLabel sort_pcname hasui_no').text.strip()

      # IP Address
      ip_address = device.find('div', class_='var_mdevice_ipv4:').text.strip()

      # Download speed
      download_speed_value = float(device.find('span', id=lambda x: x and x.startswith('down_speed_')).text.strip())
      download_speed.labels(device_name=device_name, ip_address=ip_address, connection_type='').set(download_speed_value)

      # Upload speed
      upload_speed_value = float(device.find('span', id=lambda x: x and x.startswith('up_speed_:')).text.strip())
      upload_speed.labels(device_name=device_name, ip_address=ip_address, connection_type='').set(upload_speed_value)

      # Connection type
      connection_div = device.find('div', class_=lambda c: c and ('cat_wlan5' in c or 'cat_wlan' in c or 'cat_lan' in c))
      if connection_div:
      class_name = connection_div.get('class')
      if 'cat_wlan5' in class_name:
      connection_type = 'wifi 5GHz'
      elif 'cat_wlan' in class_name:
      connection_type = 'wifi 2.4GHz'
      elif 'cat_lan' in class_name:
      connection_type = 'Ethernet'
      else:
      connection_type = 'Unknown'
      else:
      connection_type = 'Unknown'
      connection_type_metric.labels(device_name=device_name, ip_address=ip_address).set(connection_type)

      # Function to fetch device information page and update metrics
      def update_metrics():
      try:
      # Session object to persist Cookies
      session = requests.Session()

      # Login to the router using HTTP Basic Authentication
      login_response = session.get(router_url, auth=HTTPBasicAuth('', password))

      # Check if login was successful
      if login_response.status_code == 200:
      # Access the page containing device information
      device_info_page = session.get(f'{router_url}/html/content/network/devices.html?lang=de')

      # Check if device_info_page was retrieved successfully
      if device_info_page.status_code == 200:
      # Extract metrics from HTML content
      extract_metrics(device_info_page.content)
      else:
      print(f'Failed to retrieve device information page: {device_info_page.status_code}')
      else:
      print(f'Login failed: {login_response.status_code}')

      # Close the session
      session.close()
      except Exception as e:
      print(f'Error occurred: {str(e)}')

      # Start HTTP server to expose metrics
      start_http_server(8000) # Expose metrics on port 8000

      # Periodically update metrics
      while True:
      update_metrics()

      Unlogged in user

      Answer

      from

    • 1 year ago

      kottleddo

      Alles was mir jetzt noch fehlt, ist eine Liste mit Geräten, die mit meinem Netzwerk verbunden sind

      Alles was mir jetzt noch fehlt, ist eine Liste mit Geräten, die mit meinem Netzwerk verbunden sind
      kottleddo
      Alles was mir jetzt noch fehlt, ist eine Liste mit Geräten, die mit meinem Netzwerk verbunden sind

      http://192.168.2.1/data/devicelist.json

       

      Das ist Base64 codiert, musst Du noch aufdröseln.

      2

      Answer

      from

      1 year ago

      @viper.de 

       

      Sehr cool. Danke Fröhlich Kannst du mir da noch einen Tipp geben, wie? Ich bin mir sicher, auch schon dafür hier im Forum eine Lösung gelesen zu haben, aber ich finde sie nicht mehr.  Aktuell erhalte ich folgenden Output


      import base64
      import json
      import requests

      def fetch_data():
      url = 'http://192.168.2.1/data/devicelist.json'
      response = requests.get(url)

      if response.status_code == 200:
      try:
      data = response.text

      # Add missing padding if necessary
      padded_data = data
      missing_padding = len(padded_data) % 4
      if missing_padding:
      padded_data += '=' * (4 - missing_padding)

      # Decode the base64 encoded data
      decoded_data = base64.b64decode(padded_data)

      try:
      # Try to decode with utf-8
      text_data = decoded_data.decode('utf-8')
      except UnicodeDecodeError:
      # If utf-8 fails, try with latin-1
      text_data = decoded_data.decode('latin-1')

      print("Decoded text data:")
      print(text_data)

      # Attempt to load the JSON data
      try:
      # Load the JSON data
      json_data = json.loads(text_data)
      return json.dumps(json_data, indent=4) # Convert JSON data to readable string
      except json.JSONDecodeError as e:
      print(f"Error decoding JSON: {e}")
      return None
      except Exception as e:
      print(f"Error processing data: {e}")
      return None
      else:
      print(f"Failed to fetch data, status code: {response.status_code}")
      return None

      if __name__ == "__main__":
      data = fetch_data()
      if data:
      print(data)





      ¸×~5Ø
      ÷ì¿wÔ.üÓžz@
      ÑBçAvóa<ŸA>ç¾5óΰüØt.4ÔPÃàyÎD=Âëo9ÀÃθÞ@à^?8­Pö¾€Ð]Cç]¶ßN:~×Îy
      ýyçnx DãP´Ð^€]øQ<¯7Aß±Ü]:
      PwÐ@A÷ÄÐ-6-ÄÛPôðMÁ0ìNÅè1ußp8ëpüôP÷Áy÷t
      m´Û½öçoè!×­°{ßO{žƒ8.ºïmAà
      À
      @:=8×ÐwðEÑ@¼×°|ó }ÎC=¾4ìxÀvä>ùØzמAÛ~ù@MúãP„ß{ïN6ïÏA9 z®y×€¯v÷O==Aã9@zÐ.5ëÍì.„Ô=‚ë@C0@èQyï]¸Ó7Nv5×~ÀÓ`èN:à0ýߝ¶ç@7à-ôëmCôë~uÓ­÷ß½¶Ptã.û4ì^º÷ÞÁç=!5±6Ð0õÓaë]@žø÷~už6ï }ç±D^:Þ».|:덂ӏEP6ßq9^Ø
      !}ð|çM5nn{ç¡
      O8¾CM<琺ÜCØ
      ¹×žBOzë°vßp¸ïzÞºçÝ„ÛÎtPµÔ
      AçM:àAãÑBxó6ÛMz½ßŸ;ï~ýàQy¾¼ëÝùŽúäPÐ-B½Äï±AÓÁï~Cô=xߝø9óž7Ó°=ä
      A_vÎ=ØB ëÁAÛ®AÔ]¶ì
      të¾y=A÷]·èO:à?|Ð>
      ÷x}7
      qÛÀ°C¾¼
      }ÓÐëQ|çOyØ
      z`v÷ÀøPü耻ÓÀÅð!tÛMöÔ^xèN=óÞ‚Ón;ç}
      ½5
      ‘z×]=äP
      `xóÐyÔ
      C×ÐMø÷¡9ݹÍÅÏx¿óQ×PD
      ]=Ž
      ìQwà
      Ð 9àº]tߟÝäN×Ñ=ó‘wà0´Ü?6ŽôA8Ûm÷ »Û`¶Ü/ô_wÞ»MÄã¿CmÐtè=BÐ×Ýù<àƒMuאüô
      µ×¾67Ô0|èBŸBA}
      ž¶ÛÁ5]ï|ÍüמN8>ÁQ@×mÍÁ÷Þ<àNzçN4йä;Ó¾ýô7ätÑ<Ø]ÄïBä1AP|p
      p=ß 7èMãN:ëQ9u
      „n:]÷ÑwMÅpÅ
      a7]}ðO4ÝCÐN¹`y×®|
      8Ð-µ×:ä-Ào|Ó5n¸Ó}½ïQu1ðMö÷Àð;ç@7^yë½ý׎½ãOß]õßÀ¾º`uÐ]·è?Eã~ú÷pïÝ5ãÁïýÓ6ó°Aì

      Answer

      from

      1 year ago

      https://gc.de/gc/base64/

      (probier ob es Dich weiterbringt)

      Unlogged in user

      Answer

      from

    Unlogged in user

    Ask

    from