create combined script, do not use global config variables anymore
authorChristian Weiske <cweiske@cweiske.de>
Fri, 10 Dec 2010 17:20:34 +0000 (18:20 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 10 Dec 2010 17:20:34 +0000 (18:20 +0100)
Wrt3g.php
scripts/linksys-wrt3g.php [new file with mode: 0644]

index fa4693fe65c06188eb518f9683eedd6f9b4eb60b..249534e919cf3ee4188b0f20b73661bdb02653be 100644 (file)
--- a/Wrt3g.php
+++ b/Wrt3g.php
@@ -15,6 +15,27 @@ require_once 'HTTP/Request2.php';
 
 class Wrt3g
 {
 
 class Wrt3g
 {
+    /**
+     * Router hostname/IP
+     *
+     * @var string
+     */
+    public $host;
+
+    /**
+     * Name of user with administration privileges
+     *
+     * @var string
+     */
+    public $user;
+
+    /**
+     * Password for $user
+     *
+     * @var string
+     */
+    public $password;
+
     protected static $arTranslations = array(
         'GPRS_MSG.WWBEAR'      => 'type',
         'GPRS_MSG.NNAME'       => 'network',
     protected static $arTranslations = array(
         'GPRS_MSG.WWBEAR'      => 'type',
         'GPRS_MSG.NNAME'       => 'network',
@@ -37,9 +58,9 @@ class Wrt3g
         $r->setMethod(HTTP_Request2::METHOD_POST);
         $r->setUrl(
             'http://'
         $r->setMethod(HTTP_Request2::METHOD_POST);
         $r->setUrl(
             'http://'
-            . $GLOBALS['linksys-wrt3g-tools']['user']
-            . ':' . $GLOBALS['linksys-wrt3g-tools']['password']
-            . '@' . $GLOBALS['linksys-wrt3g-tools']['ip']
+            . $this->user
+            . ':' . $this->password
+            . '@' . $this->host
             . '/apply.cgi'
         );
         $r->addPostParameter('action', 'Reboot');
             . '/apply.cgi'
         );
         $r->addPostParameter('action', 'Reboot');
@@ -65,9 +86,9 @@ class Wrt3g
         $arRetval = array();
 
         $strUrlBase = 'http://'
         $arRetval = array();
 
         $strUrlBase = 'http://'
-            . $GLOBALS['linksys-wrt3g-tools']['user']
-            . ':' . $GLOBALS['linksys-wrt3g-tools']['password']
-            . '@' . $GLOBALS['linksys-wrt3g-tools']['ip'];
+            . $this->user
+            . ':' . $this->password
+            . '@' . $this->host;
 
         /**
         * Connection status
 
         /**
         * Connection status
diff --git a/scripts/linksys-wrt3g.php b/scripts/linksys-wrt3g.php
new file mode 100644 (file)
index 0000000..1d1b9d2
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+/**
+* Control script for Linksys WRT3g routers.
+*
+* PHP version 5
+*
+* @category Tools
+* @package  linksys-wrt3g-tools
+* @author   Christian Weiske <cweiske@cweiske.de>
+* @license  AGPL v3
+* @link     http://cweiske.de/linksys-wrt3g-tools.htm
+*/
+require_once 'Wrt3g.php';
+require_once 'Console/CommandLine.php';
+
+//default config options
+$GLOBALS['linksys-wrt3g-tools'] = array(
+    'host' => null,
+    'user' => 'admin',
+    'password' => null,
+);
+
+$configFile = dirname(__FILE__) . '/../config.php';
+if (file_exists($configFile)) {
+    require_once $configFile;
+}
+
+$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'     => $GLOBALS['linksys-wrt3g-tools']['host']
+    )
+);
+$parser->addOption(
+    'user',
+    array(
+        'short_name'  => '-u',
+        'long_name'   => '--user',
+        'description' => 'Admin user name',
+        'help_name'   => 'USER',
+        'action'      => 'StoreString',
+        'default'     => $GLOBALS['linksys-wrt3g-tools']['user']
+    )
+);
+$parser->addOption(
+    'password',
+    array(
+        'short_name'  => '-p',
+        'long_name'   => '--password',
+        'description' => 'Password for admin user',
+        'help_name'   => 'PASS',
+        'action'      => 'StoreString',
+        'default'     => $GLOBALS['linksys-wrt3g-tools']['password']
+    )
+);
+
+$parser->addCommand(
+    'status',
+    array(
+        'description' => 'Show the router status'
+    )
+);
+$parser->addCommand(
+    'reboot',
+    array(
+        'description' => 'Reboot the router'
+    )
+);
+
+try {
+    $result = $parser->parse();
+} catch (Exception $exc) {
+    $parser->displayError($exc->getMessage());
+    exit(1);
+}
+
+try {
+    $router = new Wrt3g();
+    $router->host     = $result->options['host'];
+    $router->user     = $result->options['user'];
+    $router->password = $result->options['password'];
+
+    switch ($result->command_name) {
+    case 'reboot':
+        $router->reboot();
+        break;
+
+    case 'status':
+    default:
+        $arStatus = $router->getStatus();
+        foreach ($arStatus as $key => $value) {
+            echo $key . ': ' . $value . "\n";
+        }
+    }
+} catch (Exception $e) {
+    echo 'Error: ' . $e->getMessage() . "\n";
+    exit(2);
+}
+?>
\ No newline at end of file