Array Question
| From: | Ian Barnes | Date: | Mon, 26 Apr 2004 13:21:02 +0000 |
| Subject: | Array Question | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-184632@lists.php.net to get a copy of this message | ||
Hi,
Here is what I have done so far:
<?php
exec('netstat -na', $sts); //Runs the Netstat Command
unset ($sts[0]); //Removes the first line (Headings)
unset ($sts[1]); //Removes the second line (Arb Info)
$sts=preg_replace('/ +/', ',', $sts); //Removes white spaces and replaces
with commas
?>
This is a typical output of a netstat -na: (Note the top two lines of this
where removed using unset).
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp4 0 0 192.168.5.1.3128 192.168.5.222.1052 TIME_WAIT
tcp4 0 0 192.168.5.1.3128 192.168.5.222.1050 TIME_WAIT
tcp4 0 0 192.168.5.1.3128 192.168.5.222.1044 TIME_WAIT
tcp4 0 0 192.168.5.1.3128 192.168.5.222.1043 TIME_WAIT
tcp4 0 0 192.168.5.1.3128 192.168.5.222.1035 TIME_WAIT
tcp4 0 0 192.168.5.1.3128 192.168.5.222.1034 TIME_WAIT
tcp4 0 0 192.168.5.1.3128 192.168.5.211.1548 TIME_WAIT
tcp4 0 0 192.168.5.1.3128 192.168.5.8.4341
ESTABLISHED
tcp4 0 0 192.168.5.1.3128 192.168.5.211.1547 TIME_WAIT
tcp4 0 0 192.168.5.1.3128 192.168.5.8.4335
ESTABLISHED
tcp4 0 315 192.168.5.1.3128 192.168.5.8.4334
ESTABLISHED
tcp4 0 0 192.168.5.1.3128 192.168.5.8.4331
ESTABLISHED
tcp4 0 0 192.168.5.1.3128 192.168.5.8.4319 TIME_WAIT
tcp4 0 0 192.168.5.1.3128 192.168.5.211.1503
ESTABLISHED
tcp4 0 0 *.* *.* CLOSED
tcp4 0 0 127.0.0.1.6667 127.0.0.1.1457
ESTABLISHED
tcp4 0 0 127.0.0.1.1457 127.0.0.1.6667
ESTABLISHED
tcp4 0 0 127.0.0.1.6667 127.0.0.1.1362
ESTABLISHED
tcp4 0 0 127.0.0.1.1362 127.0.0.1.6667
ESTABLISHED
tcp4 0 0 *.3128 *.* LISTEN
tcp4 0 0 192.168.5.1.3128 *.* LISTEN
tcp4 0 0 *.110 *.* LISTEN
tcp4 0 0 127.0.0.1.6667 *.* LISTEN
tcp4 0 0 *.22 *.* LISTEN
tcp4 0 0 *.3306 *.* LISTEN
tcp4 0 0 *.21 *.* LISTEN
tcp4 0 0 *.80 *.* LISTEN
tcp4 0 0 *.25 *.* LISTEN
tcp4 0 0 *.* *.* CLOSED
tcp4 0 0 127.0.0.1.53 *.* LISTEN
tcp4 0 0 192.168.6.1.53 *.* LISTEN
tcp4 0 0 192.168.5.1.53 *.* LISTEN
udp4 0 0 *.1042 *.*
udp4 0 0 *.10000 *.*
udp4 0 0 *.161 *.*
udp4 0 0 *.53 *.*
udp4 0 0 127.0.0.1.53 *.*
udp4 0 0 192.168.6.1.53 *.*
udp4 0 0 192.168.5.1.53 *.*
udp4 0 0 196.2.33.211.53 *.*
udp4 0 0 *.514 *.*
Active UNIX domain sockets
Address Type Recv-Q Send-Q Inode Conn Refs Nextref Addr
c83a6640 stream 0 0 c83935c0 0 0 0
/tmp/screens/S-root/887.ttyp4.comrite-mdb
c83a66e0 stream 0 0 c8393c80 0 0 0
/tmp/screens/S-creati0n/877.ttyp2.comrite-mdb
c83a6820 stream 0 0 c8388cc0 0 0 0
/tmp/screens/S-rush/716.ttyp0.comrite-mdb
c83a6d20 stream 0 0 c841ad80 0 0 0
/tmp/mysql.sock
c83a6e60 stream 0 0 c83a3dc0 0 0 0
/var/run/ndc
c83a6a00 dgram 0 0 0 c8382f00 0 c83a6aa0
c83a6aa0 dgram 0 0 0 c8382f00 0 c83a6b40
c83a6b40 dgram 0 0 0 c8382f00 0 c83a6be0
c83a6be0 dgram 0 0 0 c8382f00 0 c83a6c80
c83a6c80 dgram 0 0 0 c8382f00 0 c83a6dc0
c83a6dc0 dgram 0 0 0 c8382f00 0 c83a6f00
c83a6f00 dgram 0 0 0 c8382f00 0 0
c8382f00 dgram 0 0 c8379ec0 0 c83a6a00 0
/var/run/log
Now what i need todo is split this further, I want to have two arrays
initially, one for the top half, and one for the second half. The second
half will be this lot of output:
Active UNIX domain sockets
Address Type Recv-Q Send-Q Inode Conn Refs Nextref Addr
c83a6640 stream 0 0 c83935c0 0 0 0
/tmp/screens/S-root/887.ttyp4.comrite-mdb
c83a66e0 stream 0 0 c8393c80 0 0 0
/tmp/screens/S-creati0n/877.ttyp2.comrite-mdb
c83a6820 stream 0 0 c8388cc0 0 0 0
/tmp/screens/S-rush/716.ttyp0.comrite-mdb
c83a6d20 stream 0 0 c841ad80 0 0 0
/tmp/mysql.sock
c83a6e60 stream 0 0 c83a3dc0 0 0 0
/var/run/ndc
c83a6a00 dgram 0 0 0 c8382f00 0 c83a6aa0
c83a6aa0 dgram 0 0 0 c8382f00 0 c83a6b40
c83a6b40 dgram 0 0 0 c8382f00 0 c83a6be0
c83a6be0 dgram 0 0 0 c8382f00 0 c83a6c80
c83a6c80 dgram 0 0 0 c8382f00 0 c83a6dc0
c83a6dc0 dgram 0 0 0 c8382f00 0 c83a6f00
c83a6f00 dgram 0 0 0 c8382f00 0 0
c8382f00 dgram 0 0 c8379ec0 0 c83a6a00 0
/var/run/log
This isnt a constant, so I cant use unset. Now what I have tried todo is:
<?php
exec('netstat -na', $sts); //Runs the Netstat Command
$sts=explode('Active', $sts); //Tries to split initial array into two
print_r ($sts);
unset ($sts[0]); //Removes the first line (Headings)
unset ($sts[1]); //Removes the second line (Arb Info)
$sts=preg_replace('/ +/', ',', $sts); //Removes white spaces and replaces
with commas
?>
If I can get this right, then it will be easy to put it into a table under
specfic columns so that the formatting is neat.
Anyone know of a way to do this? Or an easier way ?
I hope you are not to confused by this stage.
Ciao
Ian