aboutsummaryrefslogtreecommitdiff
path: root/scripts/linksys-wrt3g.php
blob: d9d72884fbd34098e8780f32a0decb05c0e23500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env php
<?php
/**
* Control script for Linksys WRT3g routers.
*
* PHP version 5
*
* @category Tools
* @package  linksys-wrt3g-tools
* @author   Christian Weiske <cweiske@cweiske.de>
* @license  AGPL v3
* @link     http://cweiske.de/linksys-wrt3g-tools.htm
*/
require_once 'Wrt3g.php';
require_once 'Console/CommandLine.php';

$parser = new Console_CommandLine();
$parser->description = "Tool to control Linksys WRT3g routers";
$parser->version = '0.0.1';//FIXME: dynamic
$parser->addOption(
    'host',
    array(
        'short_name'  => '-h',
        'long_name'   => '--host',
        'description' => 'IP/Hostname to connect to',
        'help_name'   => 'HOST',
        'action'      => 'StoreString',
        'default'     => null
    )
);
$parser->addOption(
    'user',
    array(
        'short_name'  => '-u',
        'long_name'   => '--user',
        'description' => 'Admin user name',
        'help_name'   => 'USER',
        'action'      => 'StoreString',
        'default'     => 'admin'
    )
);
$parser->addOption(
    'password',
    array(
        'short_name'  => '-p',
        'long_name'   => '--password',
        'description' => 'Password for admin user',
        'help_name'   => 'PASS',
        'action'      => 'StoreString',
        'default'     => null
    )
);
$parser->addOption(
    'verbosity',
    array(
        'short_name'  => '-v',
        'long_name'   => '--verbose',
        'description' => 'Show more details (more to see more details)',
        'action'      => 'Counter',
    )
);
$parser->addOption(
    'dummy',
    array(
        'long_name'   => '--dummy',
        'description' => "Use dummy router data, not real ones.
Dummy responses can be controlled with the host parameter; 3-letter numeric hosts are interpreted as HTTP response code",
        'action'      => 'StoreTrue',
    )
);

$stCmd = $parser->addCommand(
    'status',
    array(
        'aliases'     => array('s', 'st'),
        'description' => 'Show the connection status'
    )
);
$stCmd = $parser->addCommand(
    'card',
    array(
        'aliases'     => array('c'),
        'description' => 'Show the PC card/SIM status'
    )
);
$stCmd = $parser->addCommand(
    'all',
    array(
        'aliases'     => array('a'),
        'description' => 'Show all status details'
    )
);
$stCmd = $parser->addCommand(
    'authstatus',
    array(
        'description' => 'Show the connection status the old way (authenticated)'
    )
);


$parser->addCommand(
    'reboot',
    array(
        'aliases'     => array('r'),
        'description' => 'Reboot the router'
    )
);
$stCmd = $parser->addCommand(
    'saveConfig',
    array(
        'description' => 'Saves the router configuration into the config file'
    )
);

try {
    $result = $parser->parse();
} catch (Exception $exc) {
    $parser->displayError($exc->getMessage());
    exit(1);
}

try {
    $router = new Wrt3g();
    $router->verbosity = $result->options['verbosity'];
    $router->loadConfig($result->options);

    if ($result->options['dummy']) {
        require_once 'Wrt3g/DummyRequest.php';
        $router->requestClass = 'Wrt3g_DummyRequest';
        $router->log('Using dummy data', 1);
    }

    $router->log('Command: ' . $result->command_name, 2);

    switch ($result->command_name) {
    case 'reboot':
        $resp = $router->reboot();
        echo $resp->getStatus() . ' ' . $resp->getReasonPhrase() . "\n";
        if (intval($resp->getStatus() / 100) != 2) {
            exit(3);
        }
        break;

    case 'saveConfig':
        $router->config->save($router->config->getConfigFilePath());
        break;

    case 'all':
    case 'card':
    case 'status':
    default:
        if ($result->command_name == 'all') {
            $arStatus = $router->getFullStatus();
        } else if ($result->command_name == 'card') {
            $arStatus = $router->getCardStatus();
        } else if ($result->command_name == 'authstatus') {
            $arStatus = $router->getConnectionStatusAuth();
        } else {
            $arStatus = $router->getConnectionStatus();
        }

        foreach ($arStatus as $key => $value) {
            echo $key . ': ';
            if (is_array($value)) {
                //session usage
                echo var_export($value, true) . "\n";
            } else {
                echo $value . "\n";
            }
        }
    }
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage() . "\n";
    exit(2);
}
?>