def get_netgear_devices(): netgear_key = os.environ['netgear_key'] netgear = Netgear(password=netgear_key) res_dict = [] for i in netgear.get_attached_devices(): temp_dict = dict(zip(i._fields, list(i))) temp_dict['date'] = datetime.isoformat(datetime.now()) res_dict.append(temp_dict) return pd.DataFrame.from_dict(res_dict)
def main(): """Scan for devices and print results.""" netgear = Netgear(*sys.argv[1:]) devices = netgear.get_attached_devices() if devices is None: print("Error communicating with the Netgear router") else: for i in devices: print(i)
class NetgearDeviceScanner(DeviceScanner): """Queries a Netgear wireless router using the SOAP-API.""" def __init__( self, host, ssl, username, password, port, devices, excluded_devices, accesspoints, ): """Initialize the scanner.""" self.tracked_devices = devices self.excluded_devices = excluded_devices self.tracked_accesspoints = accesspoints self.last_results = [] self._api = Netgear(password, host, username, port, ssl) _LOGGER.info("Logging in") results = self.get_attached_devices() self.success_init = results is not None if self.success_init: self.last_results = results else: _LOGGER.error("Failed to Login") def scan_devices(self): """Scan for new devices and return a list with found device IDs.""" self._update_info() devices = [] for dev in self.last_results: tracked = (not self.tracked_devices or dev.mac in self.tracked_devices or dev.name in self.tracked_devices) tracked = tracked and (not self.excluded_devices or not (dev.mac in self.excluded_devices or dev.name in self.excluded_devices)) # when link_rate is None this means the router still knows about # the device, but it is not in range. if tracked and dev.link_rate is not None: devices.append(dev.mac) if (self.tracked_accesspoints and dev.conn_ap_mac in self.tracked_accesspoints): devices.append(f"{dev.mac}_{dev.conn_ap_mac}") return devices def get_device_name(self, device): """Return the name of the given device or the MAC if we don't know.""" parts = device.split("_") mac = parts[0] ap_mac = None if len(parts) > 1: ap_mac = parts[1] name = None for dev in self.last_results: if dev.mac == mac: name = dev.name break if not name or name == "--": name = mac if ap_mac: ap_name = "Router" for dev in self.last_results: if dev.mac == ap_mac: ap_name = dev.name break return f"{name} on {ap_name}" return name def _update_info(self): """Retrieve latest information from the Netgear router. Returns boolean if scanning successful. """ if not self.success_init: return _LOGGER.info("Scanning") results = self.get_attached_devices() if results is None: _LOGGER.warning("Error scanning devices") self.last_results = results or [] def get_attached_devices(self): """ List attached devices with pynetgear. The v2 method takes more time and is more heavy on the router so we only use it if we need connected AP info. """ if self.tracked_accesspoints: return self._api.get_attached_devices_2() return self._api.get_attached_devices()
import config import pandas as pd from pandas.io import sql import MySQLdb from pynetgear import Netgear import os from datetime import datetime seb_mysql_key = os.environ['seb_mysql_key'] con = MySQLdb.connect('localhost', 'seb', seb_mysql_key) netgear_key = os.environ['netgear_key'] netgear = Netgear(password=netgear_key) res_dict = [] for i in netgear.get_attached_devices(): temp_dict = dict(zip(i._fields, list(i))) temp_dict['date'] = datetime.isoformat(datetime.now()) res_dict.append(temp_dict) A = pd.DataFrame.from_dict(res_dict) # A.to_sql(con=con, name='table_name_for_df', if_exists='replace', flavor='mysql')
print("Writing to logs...") write_logs(devices, traffic) print("Done.") print("Waiting for 1 hour before starting again.") threading.Timer(3600, logs_start).start() print("Logging into Netgear.") netgear = Netgear(password=data['router-password'], host=data['host-ip'], port=data['port']) # print(netgear.get_traffic_meter()) print("Logged in...") print("Getting attached devices...") devices = netgear.get_attached_devices() print("Done.") print("Getting traffic meter...") traffic = netgear.get_traffic_meter() print("Done.") print("Writing to logs...") print('Setting up logs...') with open('data.csv', 'a+') as csvfile: writer = csv.writer(csvfile) row = [ 'Date', 'Time', 'Usage (MB)', 'Today Upload (MB)', 'Today Download (MB)' ] for device in devices: row.append(device.name)
def setLights(lights, setOn): for light in lights: light.on = setOn #if any of these are connected, keep lights on presentDevices = ['MAC_1', 'MAC_2'] hueMac = 'MAC_HUE' netgear = Netgear('password') hueIp = -1 print 'Connecting to router. Devices found:' for device in netgear.get_attached_devices(): print device if device.mac == hueMac: hueIp = device.ip if hueIp == -1: print 'Hue not found, aborting..' exit else: print 'Hue found at ' + hueIp print 'Connecting to Hue' b = Bridge(hueIp) # If the app is not registered and the button is not pressed, press the button and call connect() (this only needs to be run a single time)
import sys from pynetgear import Netgear netgear = Netgear(*sys.argv[1:]) devices = netgear.get_attached_devices() if devices is None: print("Error communicating with the Netgear router") else: for i in devices: print(i)