ae7317bd3a00f50e42af14b743f61c440c50ae69
[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
66 $parser->addCommand(
67     'status',
68     array(
69         'aliases'     => array('s', 'st'),
70         'description' => 'Show the router status'
71     )
72 );
73 $parser->addCommand(
74     'reboot',
75     array(
76         'aliases'     => array('r'),
77         'description' => 'Reboot the router'
78     )
79 );
80
81 try {
82     $result = $parser->parse();
83 } catch (Exception $exc) {
84     $parser->displayError($exc->getMessage());
85     exit(1);
86 }
87
88 try {
89     $router = new Wrt3g();
90     $router->host     = $result->options['host'];
91     $router->user     = $result->options['user'];
92     $router->password = $result->options['password'];
93
94     switch ($result->command_name) {
95     case 'reboot':
96         $resp = $router->reboot();
97         echo $resp->getStatus() . ' ' . $resp->getReasonPhrase() . "\n";
98         if (intval($resp->getStatus() / 100) != 2) {
99             exit(3);
100         }
101         break;
102
103     case 'status':
104     default:
105         $arStatus = $router->getStatus();
106         foreach ($arStatus as $key => $value) {
107             echo $key . ': ' . $value . "\n";
108         }
109     }
110 } catch (Exception $e) {
111     echo 'Error: ' . $e->getMessage() . "\n";
112     exit(2);
113 }
114 ?>