Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php 
3
/* $Id$ */
4
/*
5
	status_interfaces.php
6
	part of m0n0wall (http://m0n0.ch/wall)
7
	
8
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10
	
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13
	
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16
	
17
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20
	
21
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32

    
33
require("guiconfig.inc");
34

    
35
function get_interface_info($ifdescr) {
36
	
37
	global $config, $g;
38
	
39
	$ifinfo = array();
40
	
41
	/* find out interface name */
42
	if ($ifdescr == "wan")
43
		$ifinfo['if'] = get_real_wan_interface();
44
	else
45
		$ifinfo['if'] = $config['interfaces'][$ifdescr]['if'];
46
	
47
	/* run netstat to determine link info */
48
	unset($linkinfo);
49
	exec("/usr/bin/netstat -I " . $ifinfo['if'] . " -nWb -f link", $linkinfo);
50
	$linkinfo = preg_split("/\s+/", $linkinfo[1]);
51
	if (preg_match("/\*$/", $linkinfo[0])) {
52
		$ifinfo['status'] = "down";
53
	} else {
54
		$ifinfo['status'] = "up";
55
	}
56
	
57
	if (($ifinfo['if'] != $g['pppoe_interface']) && (!strstr($ifinfo['if'],'tun'))) {
58
		$ifinfo['macaddr'] = $linkinfo[3];
59
		$ifinfo['inpkts'] = $linkinfo[4];
60
		$ifinfo['inerrs'] = $linkinfo[5];
61
		$ifinfo['inbytes'] = $linkinfo[6];
62
		$ifinfo['outpkts'] = $linkinfo[7];
63
		$ifinfo['outerrs'] = $linkinfo[8];
64
		$ifinfo['outbytes'] = $linkinfo[9];
65
		$ifinfo['collisions'] = $linkinfo[10];
66
	} else {
67
		$ifinfo['inpkts'] = $linkinfo[3];
68
		$ifinfo['inbytes'] = $linkinfo[5];
69
		$ifinfo['outpkts'] = $linkinfo[6];
70
		$ifinfo['outbytes'] = $linkinfo[8];
71
	}
72
	
73
	if ($ifinfo['status'] == "up") {
74
		/* run netstat to determine inet info */
75
		unset($inetinfo);
76
		exec("/usr/bin/netstat -I " . $ifinfo['if'] . " -nWb -f inet", $inetinfo);
77
		$inetinfo = preg_split("/\s+/", $inetinfo[1]);
78
		
79
		$ifinfo['ipaddr'] = $inetinfo[3];
80
		
81
		if ($ifdescr == "wan") {
82
			/* run netstat to determine the default gateway */
83
			unset($netstatrninfo);
84
			exec("/usr/bin/netstat -rnf inet", $netstatrninfo);
85
			
86
			foreach ($netstatrninfo as $nsr) {
87
				if (preg_match("/^default\s*(\S+)/", $nsr, $matches)) {
88
					$ifinfo['gateway'] = $matches[1];
89
				}
90
			}
91
		}
92
		
93
		/* try to determine netmask and media with ifconfig */
94
		unset($ifconfiginfo);
95
		exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
96
		
97
		foreach ($ifconfiginfo as $ici) {
98
			if (preg_match("/netmask (\S+)/", $ici, $matches) && !$ifinfo['subnet']) {
99
				if (preg_match("/^0x/", $matches[1])) {
100
					$ifinfo['subnet'] = long2ip(hexdec($matches[1]));
101
				}
102
			}
103
			if (!isset($config['interfaces'][$ifdescr]['wireless'])) {
104
				/* don't list media/speed for wireless cards, as it always
105
				   displays 2 Mbps even though clients can connect at 11 Mbps */
106
				if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
107
					$ifinfo['media'] = $matches[1];
108
				} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
109
					$ifinfo['media'] = $matches[1];
110
				}
111
			}
112
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
113
				if ($matches[1] != "active")
114
					$ifinfo['status'] = $matches[1];
115
			}
116
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
117
				$ifinfo['channel'] = $matches[1];
118
			}
119
			if (preg_match("/ssid (\S*)/", $ici, $matches)) {
120
				$ifinfo['ssid'] = $matches[1];
121
			}
122
		}
123
		
124
		/* PPPoE only: get media from underlying ethernet interface */
125
		if (($ifdescr == "wan") && ($config['interfaces']['wan']['ipaddr'] == "pppoe")) {
126
			unset($ifconfiginfo);
127
			exec("/sbin/ifconfig " . $config['interfaces']['wan']['if'], $ifconfiginfo);
128
			
129
			foreach ($ifconfiginfo as $ici) {
130
				if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
131
					$ifinfo['media'] = $matches[1];
132
				} else if (preg_match("/ether (.*)/", $ici, $matches)) {
133
					$ifinfo['macaddr'] = $matches[1];
134
				}
135
			}
136

    
137
			/* get pppoe link status for dial on demand */
138
			unset($ifconfiginfo);
139
			exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
140

    
141
			$ifinfo['pppoelink'] = "up";
142

    
143
			foreach ($ifconfiginfo as $ici) {
144
				if (strpos($ici, 'LINK0') !== false)
145
					$ifinfo['pppoelink'] = "down";
146
			}
147
		}
148

    
149
		/* get ppptp link status for dial on demand */
150
		if (($ifdescr == "wan") && ($config['interfaces']['wan']['ipaddr'] == "pptp")) {
151
			
152
			unset($ifconfiginfo);
153
			exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
154

    
155
			$ifinfo['pptplink'] = "up";
156

    
157
			foreach ($ifconfiginfo as $ici) {
158
				if (strpos($ici, 'LINK0') !== false)
159
					$ifinfo['pptplink'] = "down";
160
			}
161
		}
162
	}
163
	
164
	return $ifinfo;
165
}
166

    
167
?>
168
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
169
<html>
170
<head>
171
<title><?=gentitle("Status: Interfaces");?></title>
172
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
173
<link href="gui.css" rel="stylesheet" type="text/css">
174
</head>
175

    
176
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
177
<?php include("fbegin.inc"); ?>
178
      <p class="pgtitle">Status: Interfaces</p>
179
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
180
              <?php $i = 0; $ifdescrs = array('wan' => 'WAN', 'lan' => 'LAN');
181
						
182
					for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
183
						$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
184
					}
185
					
186
			      foreach ($ifdescrs as $ifdescr => $ifname): 
187
				  $ifinfo = get_interface_info($ifdescr);
188
				  ?>
189
              <?php if ($i): ?>
190
              <tr>
191
				  <td colspan="8" class="list" height="12"></td>
192
				</tr>
193
				<?php endif; ?>
194
              <tr> 
195
                <td colspan="2" class="listtopic"> 
196
                  <?=htmlspecialchars($ifname);?>
197
                  interface</td>
198
              </tr>
199
              <tr> 
200
                <td width="22%" class="listhdrr">Status</td>
201
                <td width="78%" class="listr"> 
202
                  <?=htmlspecialchars($ifinfo['status']);?>
203
                </td>
204
              </tr><?php if ($ifinfo['pppoelink']): ?>
205
              <tr> 
206
                <td width="22%" class="listhdrr">PPPoE</td>
207
                <td width="78%" class="listr"> 
208
                  <?=htmlspecialchars($ifinfo['pppoelink']);?>
209
                </td>
210
              </tr><?php  endif; if ($ifinfo['pptplink']): ?>
211
              <tr> 
212
                <td width="22%" class="listhdrr">PPTP</td>
213
                <td width="78%" class="listr"> 
214
                  <?=htmlspecialchars($ifinfo['pptplink']);?>
215
                </td>
216
              </tr><?php  endif; if ($ifinfo['macaddr']): ?>
217
              <tr> 
218
                <td width="22%" class="listhdrr">MAC address</td>
219
                <td width="78%" class="listr"> 
220
                  <?=htmlspecialchars($ifinfo['macaddr']);?>
221
                </td>
222
              </tr><?php endif; if ($ifinfo['status'] != "down"): ?>
223
			  <?php if ($ifinfo['ipaddr']): ?>
224
              <tr> 
225
                <td width="22%" class="listhdrr">IP address</td>
226
                <td width="78%" class="listr"> 
227
                  <?=htmlspecialchars($ifinfo['ipaddr']);?>
228
                  &nbsp; </td>
229
              </tr><?php endif; ?><?php if ($ifinfo['subnet']): ?>
230
              <tr> 
231
                <td width="22%" class="listhdrr">Subnet mask</td>
232
                <td width="78%" class="listr"> 
233
                  <?=htmlspecialchars($ifinfo['subnet']);?>
234
                </td>
235
              </tr><?php endif; ?><?php if ($ifinfo['gateway']): ?>
236
              <tr> 
237
                <td width="22%" class="listhdrr">Gateway</td>
238
                <td width="78%" class="listr"> 
239
                  <?=htmlspecialchars($ifinfo['gateway']);?>
240
                </td>
241
              </tr><?php endif; ?><?php if ($ifinfo['media']): ?>
242
              <tr> 
243
                <td width="22%" class="listhdrr">Media</td>
244
                <td width="78%" class="listr"> 
245
                  <?=htmlspecialchars($ifinfo['media']);?>
246
                </td>
247
              </tr><?php endif; ?><?php if ($ifinfo['channel']): ?>
248
              <tr> 
249
                <td width="22%" class="listhdrr">Channel</td>
250
                <td width="78%" class="listr"> 
251
                  <?=htmlspecialchars($ifinfo['channel']);?>
252
                </td>
253
              </tr><?php endif; ?><?php if ($ifinfo['ssid']): ?>
254
              <tr> 
255
                <td width="22%" class="listhdrr">SSID</td>
256
                <td width="78%" class="listr"> 
257
                  <?=htmlspecialchars($ifinfo['ssid']);?>
258
                </td>
259
              </tr><?php endif; ?>
260
              <tr> 
261
                <td width="22%" class="listhdrr">In/out packets</td>
262
                <td width="78%" class="listr"> 
263
                  <?=htmlspecialchars($ifinfo['inpkts'] . "/" . $ifinfo['outpkts'] . " (" . 
264
				  		format_bytes($ifinfo['inbytes']) . "/" . format_bytes($ifinfo['outbytes']) . ")");?>
265
                </td>
266
              </tr><?php if (isset($ifinfo['inerrs'])): ?>
267
              <tr> 
268
                <td width="22%" class="listhdrr">In/out errors</td>
269
                <td width="78%" class="listr"> 
270
                  <?=htmlspecialchars($ifinfo['inerrs'] . "/" . $ifinfo['outerrs']);?>
271
                </td>
272
              </tr><?php endif; ?><?php if (isset($ifinfo['collisions'])): ?>
273
              <tr> 
274
                <td width="22%" class="listhdrr">Collisions</td>
275
                <td width="78%" class="listr"> 
276
                  <?=htmlspecialchars($ifinfo['collisions']);?>
277
                </td>
278
              </tr><?php endif; ?>
279
	      <?php endif; ?>
280
              <?php $i++; endforeach; ?>
281
            </table>
282
<?php include("fend.inc"); ?>
283
</body>
284
</html>
(88-88/111)