create combined script, do not use global config variables anymore
[linksys-wrt3g-tools.git] / scripts / linksys-wrt3g.php
1 <?php
2 /**
3 * Control script for Linksys WRT3g routers.
4 *
5 * PHP version 5
6 *
7 * @category Tools
8 * @package  linksys-wrt3g-tools
9 * @author   Christian Weiske <cweiske@cweiske.de>
10 * @license  AGPL v3
11 * @link     http://cweiske.de/linksys-wrt3g-tools.htm
12 */
13 require_once 'Wrt3g.php';
14 require_once 'Console/CommandLine.php';
15
16 //default config options
17 $GLOBALS['linksys-wrt3g-tools'] = array(
18     'host' => null,
19     'user' => 'admin',
20     'password' => null,
21 );
22
23 $configFile = dirname(__FILE__) . '/../config.php';
24 if (file_exists($configFile)) {
25     require_once $configFile;
26 }
27
28 $parser = new Console_CommandLine();
29 $parser->description = 'Tool to control Linksys WRT3g routers';
30 $parser->version = '0.0.1';//FIXME: dynamic
31 $parser->addOption(
32     'host',
33     array(
34         'short_name'  => '-h',
35         'long_name'   => '--host',
36         'description' => 'IP/Hostname to connect to',
37         'help_name'   => 'HOST',
38         'action'      => 'StoreString',
39         'default'     => $GLOBALS['linksys-wrt3g-tools']['host']
40     )
41 );
42 $parser->addOption(
43     'user',
44     array(
45         'short_name'  => '-u',
46         'long_name'   => '--user',
47         'description' => 'Admin user name',
48         'help_name'   => 'USER',
49         'action'      => 'StoreString',
50         'default'     => $GLOBALS['linksys-wrt3g-tools']['user']
51     )
52 );
53 $parser->addOption(
54     'password',
55     array(
56         'short_name'  => '-p',
57         'long_name'   => '--password',
58         'description' => 'Password for admin user',
59         'help_name'   => 'PASS',
60         'action'      => 'StoreString',
61         'default'     => $GLOBALS['linksys-wrt3g-tools']['password']
62     )
63 );
64
65 $parser->addCommand(
66     'status',
67     array(
68         'description' => 'Show the router status'
69     )
70 );
71 $parser->addCommand(
72     'reboot',
73     array(
74         'description' => 'Reboot the router'
75     )
76 );
77
78 try {
79     $result = $parser->parse();
80 } catch (Exception $exc) {
81     $parser->displayError($exc->getMessage());
82     exit(1);
83 }
84
85 try {
86     $router = new Wrt3g();
87     $router->host     = $result->options['host'];
88     $router->user     = $result->options['user'];
89     $router->password = $result->options['password'];
90
91     switch ($result->command_name) {
92     case 'reboot':
93         $router->reboot();
94         break;
95
96     case 'status':
97     default:
98         $arStatus = $router->getStatus();
99         foreach ($arStatus as $key => $value) {
100             echo $key . ': ' . $value . "\n";
101         }
102     }
103 } catch (Exception $e) {
104     echo 'Error: ' . $e->getMessage() . "\n";
105     exit(2);
106 }
107 ?>