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