Jump to content

Handling exec() output


neovox

Recommended Posts

I have a question about handling output from exec(). 

 

I'm using exec() to run an expect script against a solaris box, with the output ($result) echoing out to the user's screen.  The information that is returned contains about 14 lines of text, however I only want to display one of these lines back to the user.

 

The script I'm using now is

 

<?
$result = null;
$cmd = "/path/script $ENT";
print"<pre>";
print"\n";
exec($cmd, $result);
for($i = 1; $i < (count($result) - 1); $i++) {
  print $result[$i] . "\n";
}
?>

 

This script returns:

 

Retrieving data... Please wait...

 

Enterprise  Name

Parameter1:  valueX

Parameter2:  valueY

<snip>

Parameter14:  valueZ

 

 

What I'd like to do is only display "Parameter2:  valueY"  for instance, instead of the entire output.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/103557-handling-exec-output/
Share on other sites

Thanks for the reply.  Using the sample you provided isn't returning anything, however, it may be due to the formatting of what is being returned. 

 

My code returns:

 

Retrieving data... Please wait...

 

Enterprise  Name

  Call Logging      =  Disabled

  Description        =  Company Name

  Intra-LATA PIC    =  NILCAC

  Inter-LATA PIC    =  NILCAC

  International PIC  =  NILCAC

  Routing profile    =  Hosting

  LCA ID            =  null

  Source ID          =  null

  SourceidForEAPrefix=  null

  Private policies  =  {voiceVPN, ExtDialing}

 

1 entry found.

 

What I'd like it to return is just the entire line "Routing profile    =  Hosting"

 

Could it be the preceding spaces that are causing the foreach not to return anything?  My foreach statement looks like this:

 

foreach($result as $line)  {
    if(substr($line,0,11) == 'Routing profile')
      print "$line\n";
}

 

I also tried it with the preceding spaces, but still got nothing back.  Thanks for the assistance.

Link to comment
https://forums.phpfreaks.com/topic/103557-handling-exec-output/#findComment-530452
Share on other sites

<?php
  $search = "Routing profile"; //Set this to what you are looking for
  $result = null;
  $cmd = "/path/script $ENT";
  print"<pre>\n";
  exec($cmd, $result);
  foreach($result as $line) {
    if(strstr($line,$search) !== FALSE)
      print "$line\n";
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/103557-handling-exec-output/#findComment-530516
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.