FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » problems with exec()
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
problems with exec() [message #177701] Sun, 15 April 2012 16:37 Go to next message
7segment is currently offline  7segment
Messages: 2
Registered: April 2012
Karma: 0
Junior Member
Hi.

I have an interesting problem and I cannot figure out what the problem
might be.

This does not produce any output:

$response = exec('ifconfig');
echo $response[0];

Although when I try other commands, I get a response on the screen. So I
don't get what's so special about ifconfig.

Could anyone help please?

7segment
Re: problems with exec() [message #177702 is a reply to message #177701] Sun, 15 April 2012 16:47 Go to previous messageGo to next message
7segment is currently offline  7segment
Messages: 2
Registered: April 2012
Karma: 0
Junior Member
Sorry this was the code I wanted to cite:

$response = exec('ifconfig');
echo $response;

On Sun, 15 Apr 2012 11:37:23 -0500, 7segment wrote:

> Hi.
>
> I have an interesting problem and I cannot figure out what the problem
> might be.
>
> This does not produce any output:
>
> $response = exec('ifconfig');
> echo $response[0];
>
> Although when I try other commands, I get a response on the screen. So I
> don't get what's so special about ifconfig.
>
> Could anyone help please?
>
> 7segment
Re: problems with exec() [message #177703 is a reply to message #177702] Sun, 15 April 2012 17:34 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 4/15/2012 12:47 PM, 7segment wrote:
> Sorry this was the code I wanted to cite:
>
> $response = exec('ifconfig');
> echo $response;
>
> On Sun, 15 Apr 2012 11:37:23 -0500, 7segment wrote:
>
>> Hi.
>>
>> I have an interesting problem and I cannot figure out what the problem
>> might be.
>>
>> This does not produce any output:
>>
>> $response = exec('ifconfig');
>> echo $response[0];
>>
>> Although when I try other commands, I get a response on the screen. So I
>> don't get what's so special about ifconfig.
>>
>> Could anyone help please?
>>
>> 7segment
>

Do you have permission to run ifconfig? Normally the web server does
not have nearly enough permissions to reconfigure the network adapters
(which is as it should be).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: problems with exec() [message #177704 is a reply to message #177701] Sun, 15 April 2012 17:43 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
7segment wrote:
> Hi.
>
> I have an interesting problem and I cannot figure out what the problem
> might be.
>
> This does not produce any output:
>
> $response = exec('ifconfig');
> echo $response;
>
> Although when I try other commands, I get a response on the screen. So I
> don't get what's so special about ifconfig.
>
> Could anyone help please?

ifconfig usually residence in /sbin (some GNU/Linux distribution are in the
migration path of moving it to /usr/sbin), this directory is never in a
normal users or a daemon users $PATH, so you need to give the whole path to
this binary to be able to run it.

By default the file is executional by all users, but someone may have changed
permissions or setup ACL rules with for example SELinux.




--

//Aho
Re: problems with exec() [message #177706 is a reply to message #177704] Sun, 15 April 2012 22:16 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 15.04.2012 19:43, schrieb J.O. Aho:
> 7segment wrote:
>> Hi.
>>
>> I have an interesting problem and I cannot figure out what the problem
>> might be.
>>
>> This does not produce any output:
>>
>> $response = exec('ifconfig');
>> echo $response;
>>
>> Although when I try other commands, I get a response on the screen. So I
>> don't get what's so special about ifconfig.
>>
>> Could anyone help please?
>
> ifconfig usually residence in /sbin (some GNU/Linux distribution are in the migration
> path of moving it to /usr/sbin), this directory is never in a normal users or a
> daemon users $PATH, so you need to give the whole path to this binary to be able to
> run it.
>
> By default the file is executional by all users, but someone may have changed
> permissions or setup ACL rules with for example SELinux.
>

I can tell that on opensuse it it not executable by normal users.

/Str.
Re: problems with exec() [message #177707 is a reply to message #177706] Sun, 15 April 2012 22:22 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 16.04.2012 00:16, schrieb M. Strobel:
> Am 15.04.2012 19:43, schrieb J.O. Aho:
>> 7segment wrote:
>>> Hi.
>>>
>>> I have an interesting problem and I cannot figure out what the problem
>>> might be.
>>>
>>> This does not produce any output:
>>>
>>> $response = exec('ifconfig');
>>> echo $response;
>>>
>>> Although when I try other commands, I get a response on the screen. So I
>>> don't get what's so special about ifconfig.
>>>
>>> Could anyone help please?
>>
>> ifconfig usually residence in /sbin (some GNU/Linux distribution are in the migration
>> path of moving it to /usr/sbin), this directory is never in a normal users or a
>> daemon users $PATH, so you need to give the whole path to this binary to be able to
>> run it.
>>
>> By default the file is executional by all users, but someone may have changed
>> permissions or setup ACL rules with for example SELinux.
>>
>
> I can tell that on opensuse it it not executable by normal users.
>

Correct is: on suse it is not found on the path. When called with path /sbin/ifconfig
in a shell it outputs the NIC configuration. When called like that it outputs nothing:

php > $a = exec('/sbin/ifconfig');
php > echo $a;
php > var_dump($a);
string(0) ""


/Str.
Re: problems with exec() [message #177708 is a reply to message #177707] Mon, 16 April 2012 05:17 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
M. Strobel wrote:
> Am 16.04.2012 00:16, schrieb M. Strobel:
>> Am 15.04.2012 19:43, schrieb J.O. Aho:
>>> 7segment wrote:
>>>> Hi.
>>>>
>>>> I have an interesting problem and I cannot figure out what the problem
>>>> might be.
>>>>
>>>> This does not produce any output:
>>>>
>>>> $response = exec('ifconfig');
>>>> echo $response;
>>>>
>>>> Although when I try other commands, I get a response on the screen. So I
>>>> don't get what's so special about ifconfig.
>>>>
>>>> Could anyone help please?
>>>
>>> ifconfig usually residence in /sbin (some GNU/Linux distribution are in the migration
>>> path of moving it to /usr/sbin), this directory is never in a normal users or a
>>> daemon users $PATH, so you need to give the whole path to this binary to be able to
>>> run it.
>>>
>>> By default the file is executional by all users, but someone may have changed
>>> permissions or setup ACL rules with for example SELinux.
>>>
>>
>> I can tell that on opensuse it it not executable by normal users.
>>
>
> Correct is: on suse it is not found on the path. When called with path /sbin/ifconfig
> in a shell it outputs the NIC configuration. When called like that it outputs nothing:
>
> php> $a = exec('/sbin/ifconfig');
> php> echo $a;
> php> var_dump($a);
> string(0) ""

Think the ifconfig makes a bit environment check and wants a shell, use
shell_exec instead and you will see the output.


--

//Aho
Re: problems with exec() [message #177710 is a reply to message #177708] Mon, 16 April 2012 08:07 Go to previous messageGo to next message
Une Bvue is currently offline  Une Bvue
Messages: 30
Registered: March 2012
Karma: 0
Member
Le 16/04/2012 07:17, J.O. Aho a écrit :
>>
>> php> $a = exec('/sbin/ifconfig');
>> php> echo $a;
>> php> var_dump($a);
>> string(0) ""
>
> Think the ifconfig makes a bit environment check and wants a shell, use
> shell_exec instead and you will see the output.
>

On Ubuntu 11.10, this works very well :
$lines=explode("\n", shell_exec('/sbin/ifconfig'));
foreach ($lines as $line) {
echo "$line<br />";
}

may be the prob i had creating an ssh tunnel comes also from the
difference between "exec" and "shell_exec" ?
Re: problems with exec() [message #177712 is a reply to message #177701] Mon, 16 April 2012 11:27 Go to previous messageGo to next message
Kim Andr Aker is currently offline  Kim Andr Aker
Messages: 17
Registered: September 2010
Karma: 0
Junior Member
På Sun, 15 Apr 2012 18:37:23 +0200, skrev 7segment <7segment(at)nowhere(dot)com>:

> Hi.
>
> I have an interesting problem and I cannot figure out what the problem
> might be.
>
> This does not produce any output:
>
> $response = exec('ifconfig');
> echo $response[0];
>
> Although when I try other commands, I get a response on the screen. So I
> don't get what's so special about ifconfig.
>
> Could anyone help please?

Based on your corrected code in your own reply, which lists the second
line as "echo $response;", I can see what you problem would be.

Even if you're allowed to run this particular command from your PHP
script, the return value of exec() is always the LAST line of the command
output. On my system, the last line of the "ifconfig" command is always
blank, which is what $response in your code example would contain. Try
instead to do it like this (and, preferrably, include the full path of
ifconfig):

exec('/sbin/ifconfig', $response); // $response is an array
echo implode('\n', $response);

--
Kim André Akerø
- kimandre(at)NOSPAMbetadome(dot)com
(remove NOSPAM to contact me directly)
Re: problems with exec() [message #177714 is a reply to message #177712] Mon, 16 April 2012 12:43 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 16.04.2012 13:27, schrieb Kim André Akerø:
> På Sun, 15 Apr 2012 18:37:23 +0200, skrev 7segment <7segment(at)nowhere(dot)com>:
>
>> Hi.
>>
>> I have an interesting problem and I cannot figure out what the problem
>> might be.
>>
>> This does not produce any output:
>>
>> $response = exec('ifconfig');
>> echo $response[0];
>>
>> Although when I try other commands, I get a response on the screen. So I
>> don't get what's so special about ifconfig.
>>
>> Could anyone help please?
>
> Based on your corrected code in your own reply, which lists the second line as "echo
> $response;", I can see what you problem would be.
>
> Even if you're allowed to run this particular command from your PHP script, the
> return value of exec() is always the LAST line of the command output. On my system,
> the last line of the "ifconfig" command is always blank, which is what $response in
> your code example would contain. Try instead to do it like this (and, preferrably,
> include the full path of ifconfig):

Very convincing, thank you. I was about to test why ifconfig would need the shell
environment...

/Str.
Re: problems with exec() [message #177717 is a reply to message #177710] Mon, 16 April 2012 17:29 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
Une Bévue wrote:
> Le 16/04/2012 07:17, J.O. Aho a écrit :
>>>
>>> php> $a = exec('/sbin/ifconfig');
>>> php> echo $a;
>>> php> var_dump($a);
>>> string(0) ""
>>
>> Think the ifconfig makes a bit environment check and wants a shell, use
>> shell_exec instead and you will see the output.
>>
>
> On Ubuntu 11.10, this works very well :
> $lines=explode("\n", shell_exec('/sbin/ifconfig'));
> foreach ($lines as $line) {
> echo "$line<br />";
> }
>
> may be the prob i had creating an ssh tunnel comes also from the difference
> between "exec" and "shell_exec" ?

I suspect it could have the same issue, try to use shell_exec and let us know
if it works.

--

//Aho
Re: problems with exec() [message #177729 is a reply to message #177717] Wed, 18 April 2012 13:44 Go to previous messageGo to next message
Une Bvue is currently offline  Une Bvue
Messages: 30
Registered: March 2012
Karma: 0
Member
Le 16/04/2012 19:29, J.O. Aho a écrit :
> I suspect it could have the same issue, try to use shell_exec and let us
> know if it works.

Unfortunately, not.
i suspect because the line :

$cmd = ssh -L 63333:localhost:5432 -f yt@iMac -N

is without password for the user 'yt' not for php being of user
'www_data' on Ubuntu.

Nevertheless i got that pgsql db working remotly after editing pg_hba.conf.
Re: problems with exec() [message #177730 is a reply to message #177729] Wed, 18 April 2012 16:02 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
Une Bévue wrote:
> Le 16/04/2012 19:29, J.O. Aho a écrit :
>> I suspect it could have the same issue, try to use shell_exec and let us
>> know if it works.
>
> Unfortunately, not.
> i suspect because the line :
>
> $cmd = ssh -L 63333:localhost:5432 -f yt@iMac -N
>
> is without password for the user 'yt' not for php being of user 'www_data' on
> Ubuntu.
>
> Nevertheless i got that pgsql db working remotly after editing pg_hba.conf.

Otherwise, you can copy the public key to the www-data users ~/.ssh and the
ssh would have worked. Of course you could have setup a cron job instead which
would recreated the tunnel each time it went down.
Of course those two solutions aren't as good as using the proper method.

--

//Aho
Re: problems with exec() [message #177731 is a reply to message #177730] Wed, 18 April 2012 17:43 Go to previous messageGo to next message
Une Bvue is currently offline  Une Bvue
Messages: 30
Registered: March 2012
Karma: 0
Member
Le 18/04/2012 18:02, J.O. Aho a écrit :
>
> Otherwise, you can copy the public key to the www-data users ~/.ssh and
> the ssh would have worked. Of course you could have setup a cron job
> instead which would recreated the tunnel each time it went down.
> Of course those two solutions aren't as good as using the proper method.

ok, thanks.
In fact i've used the ssh tunnel because it was the simplest :
create the tunnel and use the psql server as if it is local.
Re: problems with exec() [message #177732 is a reply to message #177731] Wed, 18 April 2012 18:37 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
Une Bévue wrote:
> Le 18/04/2012 18:02, J.O. Aho a écrit :
>>
>> Otherwise, you can copy the public key to the www-data users ~/.ssh and
>> the ssh would have worked. Of course you could have setup a cron job
>> instead which would recreated the tunnel each time it went down.
>> Of course those two solutions aren't as good as using the proper method.
>
> ok, thanks.
> In fact i've used the ssh tunnel because it was the simplest :
> create the tunnel and use the psql server as if it is local.

*nods*
In a way it's easy, but at the same time you get something more that can fail
and it can be looking strange in the error messages when the ssh tunnel is
still there while the other end of the tunnel don't work, seen issues with
that at a company I used to work for.

I haven't followed your thread, but you know that user A can setup the tunnel
and user B can use it.



--

//Aho
Re: problems with exec() [message #177736 is a reply to message #177732] Wed, 18 April 2012 21:31 Go to previous message
Une Bvue is currently offline  Une Bvue
Messages: 30
Registered: March 2012
Karma: 0
Member
On 18/04/12 20:37, J.O. Aho wrote:
> I haven't followed your thread, but you know that user A can setup the
> tunnel and user B can use it.

Yes, thanks, in fact i used the tunnel only to start with by the time i
setup pg_hba.conf.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: $this->delimiter in php explode
Next Topic: We Need Speakers For MOSC2012
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 01:11:43 GMT 2024

Total time taken to generate the page: 0.02718 seconds