Help with exec.
am 03.03.2010 14:01:35 von Paul Halliday
I need to pipe some data to an external application.
I have this:
while ($row = mysql_fetch_array($theData[0])) {
$src_ip[] = $row[0];
$dst_ip[] = $row[1];
$sig_desc[] = $row[2];
$rec ++;
if ( $rec == $recCount ) {
break;
}
}
for ($i = 0; $i < sizeof($src_ip); $i++) {
$tmpResult[] = "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
}
The external program is called like:
cat results.csv | theprogram outputfilename
Is there a way mimic this w/o outputting $tmpResult to a file first?
Thanks.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help with exec.
am 03.03.2010 14:04:55 von Richard Quadling
On 3 March 2010 13:01, Paul Halliday wrote:
> I need to pipe some data to an external application.
>
> I have this:
>
> while ($row =3D mysql_fetch_array($theData[0])) {
> Â Â $src_ip[] =3D $row[0];
> Â Â $dst_ip[] =3D $row[1];
> Â Â $sig_desc[] =3D $row[2];
>
> Â Â $rec ++;
> Â Â if ( $rec == $recCount ) {
> Â Â Â Â Â Â break;
> Â Â }
> }
>
> for ($i =3D 0; $i < sizeof($src_ip); $i++) {
> Â Â $tmpResult[] =3D "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
> }
>
>
> The external program is called like:
>
> cat results.csv | theprogram outputfilename
>
> Is there a way mimic this w/o outputting $tmpResult to a file first?
>
> Thanks.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
I believe you can use popen() to open "theprogram" and pipe to it the
content and read back the results. All without writing to any files.
--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help with exec.
am 03.03.2010 14:27:42 von Teus Benschop
On Wed, 2010-03-03 at 13:04 +0000, Richard Quadling wrote:
> On 3 March 2010 13:01, Paul Halliday wrote:
> > I need to pipe some data to an external application.
> >
> > I have this:
> >
> > while ($row =3D mysql_fetch_array($theData[0])) {
> > $src_ip[] =3D $row[0];
> > $dst_ip[] =3D $row[1];
> > $sig_desc[] =3D $row[2];
> >
> > $rec ++;
> > if ( $rec == $recCount ) {
> > break;
> > }
> > }
> >
> > for ($i =3D 0; $i < sizeof($src_ip); $i++) {
> > $tmpResult[] =3D "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
> > }
> >
> >
> > The external program is called like:
> >
> > cat results.csv | theprogram outputfilename
> >
> > Is there a way mimic this w/o outputting $tmpResult to a file first?
> >
> > Thanks.
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>=20
> I believe you can use popen() to open "theprogram" and pipe to it the
> content and read back the results. All without writing to any files.
>=20
popen() either reads from a pipe or writes to a pipe, if I remember
well, not both. Teus.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help with exec.
am 03.03.2010 14:29:23 von Paul Halliday
I work by example :)
I can't find enough of an example to get me going with this.
I have this:
$glow =3D popen('afterglow.pl -c color.properties -s -e 3 -p 1 -l 2000 |
neato -Tpng -o /usr/local/www/test.png','r');
how do I feed my array to that?
Thanks.
On Wed, Mar 3, 2010 at 9:04 AM, Richard Quadling
wrote:
> On 3 March 2010 13:01, Paul Halliday wrote:
>> I need to pipe some data to an external application.
>>
>> I have this:
>>
>> while ($row =3D mysql_fetch_array($theData[0])) {
>> =A0 =A0$src_ip[] =3D $row[0];
>> =A0 =A0$dst_ip[] =3D $row[1];
>> =A0 =A0$sig_desc[] =3D $row[2];
>>
>> =A0 =A0$rec ++;
>> =A0 =A0if ( $rec == $recCount ) {
>> =A0 =A0 =A0 =A0 =A0 =A0break;
>> =A0 =A0}
>> }
>>
>> for ($i =3D 0; $i < sizeof($src_ip); $i++) {
>> =A0 =A0$tmpResult[] =3D "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
>> }
>>
>>
>> The external program is called like:
>>
>> cat results.csv | theprogram outputfilename
>>
>> Is there a way mimic this w/o outputting $tmpResult to a file first?
>>
>> Thanks.
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> I believe you can use popen() to open "theprogram" and pipe to it the
> content and read back the results. All without writing to any files.
>
>
>
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
> Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D2134=
74731
> ZOPA : http://uk.zopa.com/member/RQuadling
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Help with exec.
am 03.03.2010 15:08:23 von Ian
On 03/03/2010 13:01, Paul Halliday wrote:
> I need to pipe some data to an external application.
>
> I have this:
>
> while ($row = mysql_fetch_array($theData[0])) {
> $src_ip[] = $row[0];
> $dst_ip[] = $row[1];
> $sig_desc[] = $row[2];
>
> $rec ++;
> if ( $rec == $recCount ) {
> break;
> }
> }
>
> for ($i = 0; $i < sizeof($src_ip); $i++) {
> $tmpResult[] = "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
> }
>
>
> The external program is called like:
>
> cat results.csv | theprogram outputfilename
>
> Is there a way mimic this w/o outputting $tmpResult to a file first?
>
> Thanks.
>
Hi,
I have used this code to feed data to gpg and read back the encrypted
result, Im sure you can adapt it to your needs.
function Encrypt($data){
# http://www.theoslogic.com/scripts/php-gpg/
$gpg_command="/usr/bin/gpg $parameters";
$errLog = "/tmp/errors.log";
$dspecs = array(
0=>array("pipe", "r"),
1=>array("pipe", "w"),
2=>array("file", $errLog, "a")
);
$encrypted="";
$procdata="";
$gpgproc = proc_open($gpg_command, $dspecs, $pipes);
if (is_resource($gpgproc)) {
fwrite($pipes[0], $data);
fclose($pipes[0]);
while($procdata = fgets($pipes[1], 1024)) {
$encrypted .= $procdata;
}
fclose($pipes[1]);
}
return $encrypted;
}
It works really well.
Regards
Ian
--
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: Help with exec.
am 03.03.2010 16:40:50 von Paul Halliday
and its that easy!
it took me a minute to figure out; but all I had to do was:
if (is_resource($process)) {
for ($i =3D 0; $i < sizeof($src_ip); $i++) {
fwrite($pipes[0], "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n");
}
fclose($pipes[0]);
fclose($pipes[1]);
proc_close($process);
}
thanks.
On Wed, Mar 3, 2010 at 10:08 AM, Ian wrote:
> On 03/03/2010 13:01, Paul Halliday wrote:
>> I need to pipe some data to an external application.
>>
>> I have this:
>>
>> while ($row =3D mysql_fetch_array($theData[0])) {
>> =A0 =A0 $src_ip[] =3D $row[0];
>> =A0 =A0 $dst_ip[] =3D $row[1];
>> =A0 =A0 $sig_desc[] =3D $row[2];
>>
>> =A0 =A0 $rec ++;
>> =A0 =A0 if ( $rec == $recCount ) {
>> =A0 =A0 =A0 =A0 =A0 =A0 break;
>> =A0 =A0 }
>> }
>>
>> for ($i =3D 0; $i < sizeof($src_ip); $i++) {
>> =A0 =A0 $tmpResult[] =3D "$sig_desc[$i],$src_ip[$i],$dst_ip[$i]\n";
>> }
>>
>>
>> The external program is called like:
>>
>> cat results.csv | theprogram outputfilename
>>
>> Is there a way mimic this w/o outputting $tmpResult to a file first?
>>
>> Thanks.
>>
>
> Hi,
>
> I have used this code to feed data to gpg and read back the encrypted
> result, Im sure you can adapt it to your needs.
>
> function Encrypt($data){
>
> =A0 =A0 =A0 =A0# http://www.theoslogic.com/scripts/php-gpg/
>
> =A0 =A0 =A0 =A0$gpg_command=3D"/usr/bin/gpg $parameters";
>
> =A0 =A0 =A0 =A0$errLog =3D "/tmp/errors.log";
>
> =A0 =A0 =A0 =A0$dspecs =3D array(
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A00=3D>array("pipe", "r"),
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A01=3D>array("pipe", "w"),
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A02=3D>array("file", $errLog, "a")
> =A0 =A0 =A0 =A0);
>
> =A0 =A0 =A0 =A0$encrypted=3D"";
> =A0 =A0 =A0 =A0$procdata=3D"";
>
> =A0 =A0 =A0 =A0$gpgproc =3D proc_open($gpg_command, $dspecs, $pipes);
>
> =A0 =A0 =A0 =A0if (is_resource($gpgproc)) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0fwrite($pipes[0], $data);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0fclose($pipes[0]);
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0while($procdata =3D fgets($pipes[1], 1024)=
) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$encrypted .=3D $procdata;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0fclose($pipes[1]);
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0return $encrypted;
> }
>
> It works really well.
>
> Regards
>
> Ian
> --
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php