SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression Help!

    I was wondering if someone here can help me. Here's what a want to do. I'd like to strip all the junk data from a 'netstat' shell command, and store the remaining data into two arrays.

    I want the first array to store the transfer speed (Send-Q), and the second array to store the 'State'. Here's a sample string of what I'll be working with...

    Code:
    Active Internet connections (w/o servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State      
    tcp        0  12960 server1-host.crysis-on:http 58.17.198.62:4079           ESTABLISHED 
    tcp        0  17280 server1-host.crysis-on:http 158.96.189.60.broad.tz:1371 ESTABLISHED 
    tcp        0      0 server1-host.crysis-o:46987 cust9521.nsw01.dataco.:2472 TIME_WAIT   
    tcp        0  12960 server1-host.crysis-on:http 58.17.198.62:4108           ESTABLISHED 
    tcp        0  54020 server1-host.crysis-on:http 58.54.131.45:13434          ESTABLISHED 
    tcp        0  77380 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60101 ESTABLISHED 
    tcp        0  61320 server1-host.crysis-on:http 58.54.131.45:13435          ESTABLISHED 
    tcp        0  65700 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60100 ESTABLISHED 
    tcp        0  14600 server1-host.crysis-on:http 58.54.131.45:13432          ESTABLISHED 
    tcp        0  20440 server1-host.crysis-on:http 218.65.14.223:3182          ESTABLISHED 
    tcp        0  10080 server1-host.crysis-on:http 58.17.198.62:4104           ESTABLISHED 
    tcp        0  20440 server1-host.crysis-on:http 58.54.131.45:13401          ESTABLISHED 
    tcp        0  32120 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60102 ESTABLISHED 
    tcp        0  89060 server1-host.crysis-on:http iphost-72-13-168-164.:63504 ESTABLISHED 
    tcp        0  61320 server1-host.crysis-on:http 66-189-145-156.dhcp.md:2464 ESTABLISHED 
    tcp        0  43800 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60104 ESTABLISHED 
    tcp        0  75920 server1-host.crysis-on:http c-24-10-144-20.hsd1.ut:1218 ESTABLISHED 
    tcp        0  36500 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60107 ESTABLISHED 
    tcp        0  64240 server1-host.crysis-on:http 66-189-145-156.dhcp.md:2466 ESTABLISHED 
    tcp        0  32120 server1-host.crysis-on:http 218.65.14.223:3170          ESTABLISHED 
    tcp        0  24820 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60106 ESTABLISHED 
    tcp        0  45260 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60109 ESTABLISHED 
    tcp        0  18980 server1-host.crysis-on:http 218.65.14.223:3013          ESTABLISHED 
    tcp        0  37960 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60108 ESTABLISHED 
    tcp        0  29200 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60143 ESTABLISHED 
    tcp        0      0 server1-host.crysis-on:http cust9521.nsw01.dataco:18609 ESTABLISHED 
    tcp        0  32120 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60110 ESTABLISHED 
    tcp        0  23360 server1-host.crysis-on:http 58.54.131.45:13871          ESTABLISHED 
    tcp        0  39420 server1-host.crysis-on:http c-24-4-16-95.hsd1.ca.c:1663 ESTABLISHED 
    tcp        0  24820 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60115 ESTABLISHED 
    tcp        1  38880 server1-host.crysis-on:http 58.17.198.62:3740           CLOSE_WAIT  
    tcp        0  36500 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60114 ESTABLISHED 
    tcp        0  21900 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60117 ESTABLISHED 
    tcp        0      0 server1-host.crysis-onl:ftp cust9521.nsw01.dataco.:2443 ESTABLISHED 
    tcp        0  36500 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60116 ESTABLISHED 
    tcp        0  33580 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60119 ESTABLISHED 
    tcp        0  42340 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60118 ESTABLISHED 
    tcp        0  24820 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60121 ESTABLISHED 
    tcp        0  62640 server1-host.crysis-on:http c-98-200-12-48.hsd1.tx:1610 ESTABLISHED 
    tcp        0  18720 server1-host.crysis-on:http 158.96.189.60.broad.tz:2754 ESTABLISHED 
    tcp        0      0 server1-host.crysis-on:http 219.238.129.122:31818       TIME_WAIT   
    tcp        0  29200 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60120 ESTABLISHED 
    tcp        0  10080 server1-host.crysis-on:http 58.17.198.62:4118           ESTABLISHED 
    tcp        0  75504 server1-host.crysis-on:http pool-71-253-19-134.pi:50379 ESTABLISHED 
    tcp        0  27740 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60123 ESTABLISHED 
    tcp        0  27740 server1-host.crysis-on:http 218.65.14.223:3219          ESTABLISHED 
    tcp        0  26280 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60122 ESTABLISHED 
    tcp        0 100224 server1-host.crysis-on:http c-76-110-252-171.hsd1.:2076 ESTABLISHED 
    tcp        0  57280 server1-host.crysis-on:http 202-89-158-108.dyn-dsl:3217 ESTABLISHED 
    tcp        0  33580 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60125 ESTABLISHED 
    tcp        0  21600 server1-host.crysis-on:http 158.96.189.60.broad.tz:2758 ESTABLISHED 
    tcp        0  97820 server1-host.crysis-on:http 71-36-183-1.phnx.qwest:1417 ESTABLISHED 
    tcp        0  36500 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60124 ESTABLISHED 
    tcp        0  84680 server1-host.crysis-on:http 75-129-79-182.dhcp.by:63455 ESTABLISHED 
    tcp        0  90520 server1-host.crysis-on:http 218.65.14.223:2903          ESTABLISHED 
    tcp        0  24820 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60127 ESTABLISHED 
    tcp        0  21600 server1-host.crysis-on:http 158.96.189.60.broad.tz:2756 ESTABLISHED 
    tcp        0  96360 server1-host.crysis-on:http S010600301b39ed59.pk.s:1258 ESTABLISHED 
    tcp        0  36500 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60126 ESTABLISHED 
    tcp        0  14400 server1-host.crysis-on:http 158.96.189.60.broad.tz:2757 ESTABLISHED 
    tcp        0      0 server1-host.crysis-onl:ssh cust9521.nsw01.dataco.:2323 ESTABLISHED 
    Active UNIX domain sockets (w/o servers)
    Proto RefCnt Flags       Type       State         I-Node Path
    unix  2      [ ]         DGRAM                    8206   @/var/run/hal/hotplug_socket
    unix  2      [ ]         DGRAM                    3020   @udevd
    unix  15     [ ]         DGRAM                    5853   /dev/log
    unix  2      [ ]         DGRAM                    1632522 
    unix  2      [ ]         DGRAM                    1632230 
    unix  2      [ ]         DGRAM                    1365553 
    unix  3      [ ]         STREAM     CONNECTED     942938 /var/run/dbus/system_bus_socket
    unix  3      [ ]         STREAM     CONNECTED     942937 
    unix  2      [ ]         DGRAM                    255606 
    unix  3      [ ]         STREAM     CONNECTED     8205   /var/run/dbus/system_bus_socket
    unix  3      [ ]         STREAM     CONNECTED     8204   
    unix  3      [ ]         STREAM     CONNECTED     8182   
    unix  3      [ ]         STREAM     CONNECTED     8181   
    unix  2      [ ]         DGRAM                    7389   
    unix  2      [ ]         DGRAM                    7055   
    unix  3      [ ]         STREAM     CONNECTED     7046   
    unix  3      [ ]         STREAM     CONNECTED     7045   
    unix  3      [ ]         STREAM     CONNECTED     7043   
    unix  3      [ ]         STREAM     CONNECTED     7042   
    unix  2      [ ]         DGRAM                    6878   
    unix  2      [ ]         DGRAM                    6599   
    unix  2      [ ]         DGRAM                    6580   
    unix  2      [ ]         DGRAM                    6559   
    unix  2      [ ]         DGRAM                    6541   
    unix  2      [ ]         DGRAM                    6385   
    unix  3      [ ]         STREAM     CONNECTED     6042   
    unix  3      [ ]         STREAM     CONNECTED     6041   
    unix  2      [ ]         DGRAM                    5933   
    unix  2      [ ]         DGRAM                    5861
    As you can see, it's not pretty. Here's exactly what I want...
    • Exclude all lines which don't start with the string 'tcp' and not' contain the string 'http', this will get rid of all the junk. This should be easy enough. The string should look something like this after we strip all the junk lines...

      Code:
      tcp        0  12960 server1-host.crysis-on:http 58.17.198.62:4079           ESTABLISHED 
      tcp        0  17280 server1-host.crysis-on:http 158.96.189.60.broad.tz:1371 ESTABLISHED  
      tcp        0  12960 server1-host.crysis-on:http 58.17.198.62:4108           ESTABLISHED 
      tcp        0  54020 server1-host.crysis-on:http 58.54.131.45:13434          ESTABLISHED 
      tcp        0  77380 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60101 ESTABLISHED 
      tcp        0  61320 server1-host.crysis-on:http 58.54.131.45:13435          ESTABLISHED 
      tcp        0  65700 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60100 ESTABLISHED 
      tcp        0  14600 server1-host.crysis-on:http 58.54.131.45:13432          ESTABLISHED 
      tcp        0  20440 server1-host.crysis-on:http 218.65.14.223:3182          ESTABLISHED 
      tcp        0  10080 server1-host.crysis-on:http 58.17.198.62:4104           ESTABLISHED 
      tcp        0  20440 server1-host.crysis-on:http 58.54.131.45:13401          ESTABLISHED 
      tcp        0  32120 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60102 ESTABLISHED 
      tcp        0  89060 server1-host.crysis-on:http iphost-72-13-168-164.:63504 ESTABLISHED 
      tcp        0  61320 server1-host.crysis-on:http 66-189-145-156.dhcp.md:2464 ESTABLISHED 
      tcp        0  43800 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60104 ESTABLISHED 
      tcp        0  75920 server1-host.crysis-on:http c-24-10-144-20.hsd1.ut:1218 ESTABLISHED 
      tcp        0  36500 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60107 ESTABLISHED 
      tcp        0  64240 server1-host.crysis-on:http 66-189-145-156.dhcp.md:2466 ESTABLISHED 
      tcp        0  32120 server1-host.crysis-on:http 218.65.14.223:3170          ESTABLISHED 
      tcp        0  24820 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60106 ESTABLISHED 
      tcp        0  45260 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60109 ESTABLISHED 
      tcp        0  18980 server1-host.crysis-on:http 218.65.14.223:3013          ESTABLISHED 
      tcp        0  37960 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60108 ESTABLISHED 
      tcp        0  29200 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60143 ESTABLISHED 
      tcp        0      0 server1-host.crysis-on:http cust9521.nsw01.dataco:18609 ESTABLISHED 
      tcp        0  32120 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60110 ESTABLISHED 
      tcp        0  23360 server1-host.crysis-on:http 58.54.131.45:13871          ESTABLISHED 
      tcp        0  39420 server1-host.crysis-on:http c-24-4-16-95.hsd1.ca.c:1663 ESTABLISHED 
      tcp        0  24820 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60115 ESTABLISHED 
      tcp        1  38880 server1-host.crysis-on:http 58.17.198.62:3740           CLOSE_WAIT  
      tcp        0  36500 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60114 ESTABLISHED 
      tcp        0  21900 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60117 ESTABLISHED 
      tcp        0  36500 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60116 ESTABLISHED 
      tcp        0  33580 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60119 ESTABLISHED 
      tcp        0  42340 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60118 ESTABLISHED 
      tcp        0  24820 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60121 ESTABLISHED 
      tcp        0  62640 server1-host.crysis-on:http c-98-200-12-48.hsd1.tx:1610 ESTABLISHED 
      tcp        0  18720 server1-host.crysis-on:http 158.96.189.60.broad.tz:2754 ESTABLISHED 
      tcp        0      0 server1-host.crysis-on:http 219.238.129.122:31818       TIME_WAIT   
      tcp        0  29200 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60120 ESTABLISHED 
      tcp        0  10080 server1-host.crysis-on:http 58.17.198.62:4118           ESTABLISHED 
      tcp        0  75504 server1-host.crysis-on:http pool-71-253-19-134.pi:50379 ESTABLISHED 
      tcp        0  27740 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60123 ESTABLISHED 
      tcp        0  27740 server1-host.crysis-on:http 218.65.14.223:3219          ESTABLISHED 
      tcp        0  26280 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60122 ESTABLISHED 
      tcp        0 100224 server1-host.crysis-on:http c-76-110-252-171.hsd1.:2076 ESTABLISHED 
      tcp        0  57280 server1-host.crysis-on:http 202-89-158-108.dyn-dsl:3217 ESTABLISHED 
      tcp        0  33580 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60125 ESTABLISHED 
      tcp        0  21600 server1-host.crysis-on:http 158.96.189.60.broad.tz:2758 ESTABLISHED 
      tcp        0  97820 server1-host.crysis-on:http 71-36-183-1.phnx.qwest:1417 ESTABLISHED 
      tcp        0  36500 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60124 ESTABLISHED 
      tcp        0  84680 server1-host.crysis-on:http 75-129-79-182.dhcp.by:63455 ESTABLISHED 
      tcp        0  90520 server1-host.crysis-on:http 218.65.14.223:2903          ESTABLISHED 
      tcp        0  24820 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60127 ESTABLISHED 
      tcp        0  21600 server1-host.crysis-on:http 158.96.189.60.broad.tz:2756 ESTABLISHED 
      tcp        0  96360 server1-host.crysis-on:http S010600301b39ed59.pk.s:1258 ESTABLISHED 
      tcp        0  36500 server1-host.crysis-on:http CPE-65-30-106-204.kc.:60126 ESTABLISHED 
      tcp        0  14400 server1-host.crysis-on:http 158.96.189.60.broad.tz:2757 ESTABLISHED
    • With the remaining lines of text, I want to store the Send-Q data, and the State data into separate arrays. I want the Send-Q array to be named $transfer and the State array to be named $state.
    • The indexes of each need to match, so the data in $transfer[4] needs to have come from the same line as the data in $state[4]. Just so your clear, $transfer[4] will contain the Send-Q data from line 5.
    I've hoped I've made my requirements clear. I will highly appreciate any help given to me as I know a decent amount of work would be involved in doing this (at least I think there'd be). I'm not an absolute noob to php, it's just that I'm not a pro either and I'm yet to learn about and understand regular expressions.

    Cheers!

  2. #2
    SitePoint Member
    Join Date
    Dec 2004
    Location
    canada
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pls Go through Some regaler expression ebooks.You will find a solution

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match_all('/^(tcp.*:http.*)$/im'$subject$result); 
    Last edited by logic_earth; Jul 28, 2007 at 00:38.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's what I came up with in the end. I could have probably used a regular expression in the if statement, but I found my method to be way easier for someone like myself.

    Code PHP:
    <?php
    $netstat = shell_exec('netstat'); // Get stats
    $array = explode("\n", $netstat); // Explode all lines into an array
    $count = count($array); // Count the number of array elements
    $int = 0;
    // While there are still array elements to be processed.
    while($int < $count){
    	// If the array element begins with tcp and contains the string ":http" and "ESTABLISHED".
    	if(strpos($array[$int], "tcp") === 0 && strpos($array[$int], ":http") == true && strpos($array[$int], "ESTABLISHED")){
    		$array[$int] = ereg_replace("tcp[ ]*[0-9]*[ ]*", "", $array[$int]); // Strip all the junk data at the start
    		ereg("[0-9]*", $array[$int], $return); // Get the next occuring number (of a variable length)
    		$rate[] = $return[0]; // Assign the matched data to the $rate array
    		/* Only uncomment this if you remove the: strpos($array[$int], "ESTABLISHED") from the if statement.
    		ereg(" [A-Z_]+ ", $array[$int], $return2); // Gets the connection status
    		$status[] = $return2[0]; // Assign the matched data to the $rate array
    		*/
    		$total++; // Increase the number of the total users connected
    		$rate_total += $rate[$total-1]; // Increase the transfer rate
    	}
    	$int++;
    }
    // Echo the data separating the total connections and the total transfer rate with a colon.
    echo '<pre>'.$total.':'.$rate_total.'</pre>';
    ?>

  5. #5
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This does the same thing as your code but in a smaller package.
    PHP Code:
    $lines shell_exec('netstat | grep "^tcp.*:http.*ESTABLISHED"');
    preg_match_all('/tcp\s+\d+\s+(\d+).*?$/m'$lines$data);
    echo 
    '<pre>'.count($data[0]).':'.array_sum($data[1])."</pre>\n"

  6. #6
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll have to analyse that later on stack


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •