implement non-authenticated default status
[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 $stCmd = $parser->addCommand(
94     'authstatus',
95     array(
96         'description' => 'Show the connection status the old way (authenticated)'
97     )
98 );
99
100
101 $parser->addCommand(
102     'reboot',
103     array(
104         'aliases'     => array('r'),
105         'description' => 'Reboot the router'
106     )
107 );
108 $stCmd = $parser->addCommand(
109     'saveConfig',
110     array(
111         'description' => 'Saves the router configuration into the config file'
112     )
113 );
114
115 try {
116     $result = $parser->parse();
117 } catch (Exception $exc) {
118     $parser->displayError($exc->getMessage());
119     exit(1);
120 }
121
122 try {
123     $router = new Wrt3g();
124     $router->verbosity = $result->options['verbosity'];
125     $router->loadConfig($result->options);
126
127     if ($result->options['dummy']) {
128         require_once 'Wrt3g/DummyRequest.php';
129         $router->requestClass = 'Wrt3g_DummyRequest';
130         $router->log('Using dummy data', 1);
131     }
132
133     $router->log('Command: ' . $result->command_name, 2);
134
135     switch ($result->command_name) {
136     case 'reboot':
137         $resp = $router->reboot();
138         echo $resp->getStatus() . ' ' . $resp->getReasonPhrase() . "\n";
139         if (intval($resp->getStatus() / 100) != 2) {
140             exit(3);
141         }
142         break;
143
144     case 'saveConfig':
145         $router->config->save($router->config->getConfigFilePath());
146         break;
147
148     case 'all':
149     case 'card':
150     case 'status':
151     default:
152         if ($result->command_name == 'all') {
153             $arStatus = $router->getFullStatus();
154         } else if ($result->command_name == 'card') {
155             $arStatus = $router->getCardStatus();
156         } else if ($result->command_name == 'authstatus') {
157             $arStatus = $router->getConnectionStatusAuth();
158         } else {
159             $arStatus = $router->getConnectionStatus();
160         }
161
162         foreach ($arStatus as $key => $value) {
163             echo $key . ': ';
164             if (is_array($value)) {
165                 //session usage
166                 echo var_export($value, true) . "\n";
167             } else {
168                 echo $value . "\n";
169             }
170         }
171     }
172 } catch (Exception $e) {
173     echo 'Error: ' . $e->getMessage() . "\n";
174     exit(2);
175 }
176 ?>