<?php
/**
* Fritz!Box PHP tools CLI script to check the online status of a network device
*
* Must be called via a command line, shows a help message if called without any or an invalid argument
*
* check, whether a network device is listed as online in the network list
*
* Check the config file fritzbox.conf.php!
*
* @author Gregor Nathanael Meyer <gregor [at] der-meyer.de>
* @license http://creativecommons.org/licenses/by-sa/3.0/de/ Creative Commons cc-by-sa
* @version 0.1.0 2014-10-22
* @package Fritz!Box PHP tools
*/
try
{
// load the fritzbox_api class
require_once('fritzbox_api.class.php');
$fritz = new fritzbox_api();
// handle the CLI arguments or give a help message
if ( !isset($argv[1]) )
{
if ( $fritz->config->getItem('logging') == 'console' )
{
echo '
Check the online status of a network device
Usage on UNIX systems:
/path/to/php ' . $argv[0] . ' deviceString
Usage on Windows systems:
c:\path\to\php.exe ' . $argv[0] . ' deviceString
The deviceString can be any information in the network list that identifies the device to look up (i.e. Name, IP-Address, MAC-Address)
';
}
else
{
$fritz->logMessage('ERROR: Script was called without or with an invalid argument');
}
exit;
}
$deviceString = $argv[1];
// read the current leases list
$formfields = array(
'getpage' => '/net/network_user_devices.lua',
);
$output = $fritz->doGetRequest($formfields);
// look up the given deviceString inside the active connections table
if ( preg_match('/<table id="uiLanActive".*?' . preg_quote($deviceString) . '.*?<\/table>/is', $output, $matches) )
{
$fritz->logMessage('Device "' . $deviceString . '" is listed as active');
}
else
{
$fritz->logMessage('Device "' . $deviceString . '" is not listed as active');
}
}
catch (Exception $e)
{
if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' )
{
$fritz->logMessage($e->getMessage());
}
else
{
echo(date('Y-m-d H:i') . ' ' . $e->getMessage());
}
}
$fritz = null; // destroy the object to log out