219e081c1c87be924830d8e756b32e1c4a622dac
[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 //default config options
18 $GLOBALS['linksys-wrt3g-tools'] = array(
19     'host' => null,
20     'user' => 'admin',
21     'password' => null,
22 );
23
24 $configFile = dirname(__FILE__) . '/../config.php';
25 if (file_exists($configFile)) {
26     require_once $configFile;
27 }
28
29 $parser = new Console_CommandLine();
30 $parser->description = "Tool to control Linksys WRT3g routers";
31 $parser->version = '0.0.1';//FIXME: dynamic
32 $parser->addOption(
33     'host',
34     array(
35         'short_name'  => '-h',
36         'long_name'   => '--host',
37         'description' => 'IP/Hostname to connect to',
38         'help_name'   => 'HOST',
39         'action'      => 'StoreString',
40         'default'     => $GLOBALS['linksys-wrt3g-tools']['host']
41     )
42 );
43 $parser->addOption(
44     'user',
45     array(
46         'short_name'  => '-u',
47         'long_name'   => '--user',
48         'description' => 'Admin user name',
49         'help_name'   => 'USER',
50         'action'      => 'StoreString',
51         'default'     => $GLOBALS['linksys-wrt3g-tools']['user']
52     )
53 );
54 $parser->addOption(
55     'password',
56     array(
57         'short_name'  => '-p',
58         'long_name'   => '--password',
59         'description' => 'Password for admin user',
60         'help_name'   => 'PASS',
61         'action'      => 'StoreString',
62         'default'     => $GLOBALS['linksys-wrt3g-tools']['password']
63     )
64 );
65 $parser->addOption(
66     'verbosity',
67     array(
68         'short_name'  => '-v',
69         'long_name'   => '--verbose',
70         'description' => 'Show more details (more to see more details)',
71         'action'      => 'Counter',
72     )
73 );
74 $parser->addOption(
75     'dummy',
76     array(
77         'long_name'   => '--dummy',
78         'description' => "Use dummy router data, not real ones.
79 Dummy responses can be controlled with the host parameter; 3-letter numeric hosts are interpreted as HTTP response code",
80         'action'      => 'StoreTrue',
81     )
82 );
83
84 $stCmd = $parser->addCommand(
85     'status',
86     array(
87         'aliases'     => array('s', 'st'),
88         'description' => 'Show the connection status'
89     )
90 );
91 $stCmd = $parser->addCommand(
92     'card',
93     array(
94         'aliases'     => array('c'),
95         'description' => 'Show the PC card/SIM status'
96     )
97 );
98 $stCmd = $parser->addCommand(
99     'all',
100     array(
101         'aliases'     => array('a'),
102         'description' => 'Show all status details'
103     )
104 );
105
106
107 $parser->addCommand(
108     'reboot',
109     array(
110         'aliases'     => array('r'),
111         'description' => 'Reboot the router'
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->host      = $result->options['host'];
126     $router->user      = $result->options['user'];
127     $router->password  = $result->options['password'];
128
129     if ($result->options['dummy']) {
130         require_once 'Wrt3g/DummyRequest.php';
131         $router->requestClass = 'Wrt3g_DummyRequest';
132         $router->log('Using dummy data', 1);
133     }
134
135     $router->log('Command: ' . $result->command_name, 2);
136
137     switch ($result->command_name) {
138     case 'reboot':
139         $resp = $router->reboot();
140         echo $resp->getStatus() . ' ' . $resp->getReasonPhrase() . "\n";
141         if (intval($resp->getStatus() / 100) != 2) {
142             exit(3);
143         }
144         break;
145
146     case 'all':
147     case 'card':
148     case 'status':
149     default:
150         if ($result->command_name == 'all') {
151             $arStatus = $router->getFullStatus();
152         } else if ($result->command_name == 'card') {
153             $arStatus = $router->getCardStatus();
154         } else {
155             $arStatus = $router->getConnectionStatus();
156         }
157         foreach ($arStatus as $key => $value) {
158             echo $key . ': ';
159             if (is_array($value)) {
160                 //session usage
161                 echo var_export($value, true) . "\n";
162             } else {
163                 echo $value . "\n";
164             }
165         }
166     }
167 } catch (Exception $e) {
168     echo 'Error: ' . $e->getMessage() . "\n";
169     exit(2);
170 }
171 ?>