#!/usr/bin/env php * @license AGPL v3 * @link http://cweiske.de/linksys-wrt3g-tools.htm */ require_once 'Wrt3g.php'; require_once 'Console/CommandLine.php'; $parser = new Console_CommandLine(); $parser->description = "Tool to control Linksys WRT3g routers"; $parser->version = '0.0.1';//FIXME: dynamic $parser->addOption( 'host', array( 'short_name' => '-h', 'long_name' => '--host', 'description' => 'IP/Hostname to connect to', 'help_name' => 'HOST', 'action' => 'StoreString', 'default' => null ) ); $parser->addOption( 'user', array( 'short_name' => '-u', 'long_name' => '--user', 'description' => 'Admin user name', 'help_name' => 'USER', 'action' => 'StoreString', 'default' => 'admin' ) ); $parser->addOption( 'password', array( 'short_name' => '-p', 'long_name' => '--password', 'description' => 'Password for admin user', 'help_name' => 'PASS', 'action' => 'StoreString', 'default' => null ) ); $parser->addOption( 'verbosity', array( 'short_name' => '-v', 'long_name' => '--verbose', 'description' => 'Show more details (more to see more details)', 'action' => 'Counter', ) ); $parser->addOption( 'dummy', array( 'long_name' => '--dummy', 'description' => "Use dummy router data, not real ones. Dummy responses can be controlled with the host parameter; 3-letter numeric hosts are interpreted as HTTP response code", 'action' => 'StoreTrue', ) ); $stCmd = $parser->addCommand( 'status', array( 'aliases' => array('s', 'st'), 'description' => 'Show the connection status' ) ); $stCmd = $parser->addCommand( 'card', array( 'aliases' => array('c'), 'description' => 'Show the PC card/SIM status' ) ); $stCmd = $parser->addCommand( 'all', array( 'aliases' => array('a'), 'description' => 'Show all status details' ) ); $parser->addCommand( 'reboot', array( 'aliases' => array('r'), 'description' => 'Reboot the router' ) ); $stCmd = $parser->addCommand( 'saveConfig', array( 'description' => 'Saves the router configuration into the config file' ) ); try { $result = $parser->parse(); } catch (Exception $exc) { $parser->displayError($exc->getMessage()); exit(1); } try { $router = new Wrt3g(); $router->verbosity = $result->options['verbosity']; $router->loadConfig($result->options); if ($result->options['dummy']) { require_once 'Wrt3g/DummyRequest.php'; $router->requestClass = 'Wrt3g_DummyRequest'; $router->log('Using dummy data', 1); } $router->log('Command: ' . $result->command_name, 2); switch ($result->command_name) { case 'reboot': $resp = $router->reboot(); echo $resp->getStatus() . ' ' . $resp->getReasonPhrase() . "\n"; if (intval($resp->getStatus() / 100) != 2) { exit(3); } break; case 'saveConfig': $router->config->save($router->config->getConfigFilePath()); break; case 'all': case 'card': case 'status': default: if ($result->command_name == 'all') { $arStatus = $router->getFullStatus(); } else if ($result->command_name == 'card') { $arStatus = $router->getCardStatus(); } else { $arStatus = $router->getConnectionStatus(); } foreach ($arStatus as $key => $value) { echo $key . ': '; if (is_array($value)) { //session usage echo var_export($value, true) . "\n"; } else { echo $value . "\n"; } } } } catch (Exception $e) { echo 'Error: ' . $e->getMessage() . "\n"; exit(2); } ?>