Created
July 7, 2014 03:46
-
-
Save davidkryzaniak/02c71bb21fb52cea584b to your computer and use it in GitHub Desktop.
Poll router (Tomato) for active devices on the network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Returns an array of devices currently on the network | |
* | |
* Created by PhpStorm. | |
* User: davidkryzaniak | |
* Date: 06/07/2014 | |
* Time: 22:05 | |
*/ | |
$username = 'admin'; | |
$password = 'PASSWORD!'; | |
$routerIp = '192.168.1.1' | |
$context = stream_context_create(array( | |
'http' => array( | |
'header' => "Authorization: Basic " . base64_encode("$username:$password") | |
) | |
) | |
); | |
$data = file_get_contents("http://$routerIp/status-devices.asp", false, $context); | |
//dhcpd_lease = [ ['Name-Of-Device','192.168.1.100','**:**:**:**:**:52','0 days, 19:37:18'],[ ... ],[ ... ]] | |
//our poll is based off the dhcp_lease time. This might be an issue if you're using static IPs or some other magic | |
preg_match("/dhcpd_lease =(.+);\n/",$data,$list); | |
//start converting that string to an array | |
$found = substr(str_replace(array(' ',"'"),'',$list[1]),2,-2); | |
$results = array(); | |
foreach(explode('],[',$found) as $single){ | |
$device = explode(',',$single); | |
$singleDevice = array(); | |
$singleDevice['name'] = $device[0]; | |
$singleDevice['ip'] = $device[1]; | |
//key is the MAC address | |
$results[$device[2]] = $singleDevice; | |
} | |
var_dump($results); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment