config.php file is not needed anymore
[linksys-wrt3g-tools.git] / scripts / linksys-wrt3g.php
1 #!/usr/bin/env php
2 <?php
3 /**
4 * Control script for Linksys WRT3g routers.
5 *
6 * PHP version 5
7 *
8 * @category Tools
9 * @package  linksys-wrt3g-tools
10 * @author   Christian Weiske <cweiske@cweiske.de>
11 * @license  AGPL v3
12 * @link     http://cweiske.de/linksys-wrt3g-tools.htm
13 */
14 require_once 'Wrt3g.php';
15 require_once 'Console/CommandLine.php';
16
17 $parser = new Console_CommandLine();
18 $parser->description = "Tool to control Linksys WRT3g routers";
19 $parser->version = '0.0.1';//FIXME: dynamic
20 $parser->addOption(
21     'host',
22     array(
23         'short_name'  => '-h',
24         'long_name'   => '--host',
25         'description' => 'IP/Hostname to connect to',
26         'help_name'   => 'HOST',
27         'action'      => 'StoreString',
28         'default'     => null
29     )
30 );
31 $parser->addOption(
32     'user',
33     array(
34         'short_name'  => '-u',
35         'long_name'   => '--user',
36         'description' => 'Admin user name',
37         'help_name'   => 'USER',
38         'action'      => 'StoreString',
39         'default'     => 'admin'
40     )
41 );
42 $parser->addOption(
43     'password',
44     array(
45         'short_name'  => '-p',
46         'long_name'   => '--password',
47         'description' => 'Password for admin user',
48         'help_name'   => 'PASS',
49         'action'      => 'StoreString',
50         'default'     => null
51     )
52 );
53 $parser->addOption(
54     'verbosity',
55     array(
56         'short_name'  => '-v',
57         'long_name'   => '--verbose',
58         'description' => 'Show more details (more to see more details)',
59         'action'      => 'Counter',
60     )
61 );
62 $parser->addOption(
63     'dummy',
64     array(
65         'long_name'   => '--dummy',
66         'description' => "Use dummy router data, not real ones.
67 Dummy responses can be controlled with the host parameter; 3-letter numeric hosts are interpreted as HTTP response code",
68         'action'      => 'StoreTrue',
69     )
70 );
71
72 $stCmd = $parser->addCommand(
73     'status',
74     array(
75         'aliases'     => array('s', 'st'),
76         'description' => 'Show the connection status'
77     )
78 );
79 $stCmd = $parser->addCommand(
80     'card',
81     array(
82         'aliases'     => array('c'),
83         'description' => 'Show the PC card/SIM status'
84     )
85 );
86 $stCmd = $parser->addCommand(
87     'all',
88     array(
89         'aliases'     => array('a'),
90         'description' => 'Show all status details'
91     )
92 );
93
94
95 $parser->addCommand(
96     'reboot',
97     array(
98         'aliases'     => array('r'),
99         'description' => 'Reboot the router'
100     )
101 );
102 $stCmd = $parser->addCommand(
103     'saveConfig',
104     array(
105         'description' => 'Saves the router configuration into the config file'
106     )
107 );
108
109 try {
110     $result = $parser->parse();
111 } catch (Exception $exc) {
112     $parser->displayError($exc->getMessage());
113     exit(1);
114 }
115
116 try {
117     $router = new Wrt3g();
118     $router->verbosity = $result->options['verbosity'];
119     $router->loadConfig($result->options);
120
121     if ($result->options['dummy']) {
122         require_once 'Wrt3g/DummyRequest.php';
123         $router->requestClass = 'Wrt3g_DummyRequest';
124         $router->log('Using dummy data', 1);
125     }
126
127     $router->log('Command: ' . $result->command_name, 2);
128
129     switch ($result->command_name) {
130     case 'reboot':
131         $resp = $router->reboot();
132         echo $resp->getStatus() . ' ' . $resp->getReasonPhrase() . "\n";
133         if (intval($resp->getStatus() / 100) != 2) {
134             exit(3);
135         }
136         break;
137
138     case 'saveConfig':
139         $router->config->save($router->config->getConfigFilePath());
140         break;
141
142     case 'all':
143     case 'card':
144     case 'status':
145     default:
146         if ($result->command_name == 'all') {
147             $arStatus = $router->getFullStatus();
148         } else if ($result->command_name == 'card') {
149             $arStatus = $router->getCardStatus();
150         } else {
151             $arStatus = $router->getConnectionStatus();
152         }
153         foreach ($arStatus as $key => $value) {
154             echo $key . ': ';
155             if (is_array($value)) {
156                 //session usage
157                 echo var_export($value, true) . "\n";
158             } else {
159                 echo $value . "\n";
160             }
161         }
162     }
163 } catch (Exception $e) {
164     echo 'Error: ' . $e->getMessage() . "\n";
165     exit(2);
166 }
167 ?>