Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running system command from php

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I am trying to put together a page that displays different information about the system. One of the commands I want to run is hdparm so that I can gather the hard drive serial number. My code looks like this:

Code:
$hd = system("hdparm -I /dev/sda");
echo $hd;

But I get nothing when I pull up the page to display.

Any help is greatly appreciated
 
Hi

Are you sure the webserver's user has permission to execute [tt]hdparm[/tt] ?

[tt]system()[/tt] returns only the last line of the output. If the output ends with an empty line, there will be nothing to see.

The serial number is not on the [tt]hdparm[/tt]'s last line. To grab the entire output use the backtick operators ( cheap & easy ) or the [tt]proc_open()[/tt]/[tt]proc_close()[/tt] functions ( better control ).

Feherke.
 
Thanks, come to think of it, yes it is a blank line. I did not know that about system(). I will look into using your suggestions.


Again, thanks!
 
Also noticed that hdparm is owned by root.root so apache does not have permission.
 
ok so it eludes me, tried the backticks, proc_open/close, exec, passthru and even created a script to run hdparm set permissions to 777 +t and still cannot get anything to display on the screen.

Can anoyone else lend a hand?
 
ok I changed my code to be:

Code:
$hdd = `/usr/bin/runhd`;
echo $hdd;

I created the script runhd
Code:
#! /bin/sh
/sbin/hdparm -I /dev/sda


When I run the above runhd, as user other than root, I get an error:

Code:
/dev/sda: Permission Denied
 
you need to be running as root or use sudo to access hdparm via php.

try something like this

Code:
$cwd = realpath('.');
$pwd = $cwd . '/pwd.php'
file_put_contents($pwd, <<<PHP
<?php
echo join('', file('php://stdin'));
?>
PHP
);
$cmd = "/sbin/hdparm -I /dev /sda  | sudo /usr/bin/php -f $pwd");
exec($cmd, $output, $return);
echo '<pre>' . print_r($output, true) . '</pre>';

and edit /etc/sudoers to add the following line. replace the reference to FILENAME with the value of the $pwd variable above (i.e the absolute path to the pwd.php file in the current working directory). and of course if php is not in /usr/bin/php change that reference too

Code:
apache    ALL=(ALL) NOPASSWD: /usr/bin/php -f FILENAME

that should then allow the command to be run with root privileges.
 
Thanks, I will give that a try today and let you know how it goes.
 
jpadie,
I am confused with the code you exampled. is that a seperate php file or do I put that in my current php file?
 
i don't know what your current stuff is. this is a file in itself which will output the results from calling hdparm

prove that the concept works, then adapt it to your circumstances.
 
Basically I have a page that I am getting the results of ifconfig, uname -a, and df. I tried getting hdparm, but it would not display anything.I tried a few options, one being a bash script that I called from php.
 
I tried your idea and am not getting anything. Perhaps I am creating the wrong kind of file. Should this be a PHP file or a regular text file?
 
it's a php file.

the key thing is to make sure that your web service is in the sudoers file. Of course, this is not a safe thing to do...

if your web service is not called apache, substitute as required.
 
it would be prudent to adjust the change in the sudoer's file to only allow apache to run the required commands not all
 
I agree with IPGuru. That is why the example I provided above limits the use of the non-passworded sudoer access to that particular command (hdparm)
 
I guess I was confused as I have never seen a php file constructed that way. I still am not getting something right, and I am frustrated. I am ready to scrap this whole thing! I created a php file with the example you provided. I called it test.php. my $pwd line looks like this $pwd = $cwd . '/var/
I set up that path in sudoers

When I call test.php, it does nothing but it does display this on my screen:
Code:
$cwd = realpath('.'); $pwd = $cwd . '/var/[URL unfurl="true"]www/html/ivrconfig/pwd.php'[/URL] file_put_contents($pwd, <<

 .print_r($output, true) . '

';

I thought this would be a simple procedure, or rather my boss did. Considering he does no PHP coding he knows not what is easy and what isn't.
 
ok. let's simplify this.

1. create a password file

Code:
<?php
echo join('', file('php://stdin'));
?>

2. now create the actual executing script
Code:
$pwd = '/var/[URL unfurl="true"]www/html/ivrconfig/pwd.php';[/URL]
$cmd = "/sbin/hdparm -I /dev/hda  | sudo /usr/bin/php -f $pwd");
exec($cmd, $output, $return);
echo '<pre>' . print_r($output, true) . '</pre>';

note the following:
A. you must make sure that hdparm is in the /sbin directory or else change the command accordingly.
B. you must make sure that the php executable is in the /usr/bin directory or else change the command accordingly.

3. now change the sudoers file

Code:
apache    ALL=(ALL) NOPASSWD: /usr/bin/php -f /var/[URL unfurl="true"]www/html/ivrconfig/pwd.php[/URL]

Notes:
A. substitute 'apache' with whatever user credentials the web process runs as
B. change the reference to /usr/bin/php as necessary for the location of the php executable
 
ok, did what you said and this is what display's

Array
(
)


my sudoers file looks like this:

asterisk ALL = NOPASSWD: /usr/bin/php -f /var/
This is a centos system running trixbox and asterisk. Asterisk is the user that everything runs as.

asterisk ALL = NOPASSWD: /sbin/shutdown
asterisk ALL = NOPASSWD: /usr/bin/nmap
asterisk ALL = NOPASSWD: /usr/bin/yum
asterisk ALL = NOPASSWD: /bin/chown
asterisk ALL = NOPASSWD: /bin/chmod
asterisk ALL = NOPASSWD: /bin/touch
asterisk ALL = NOPASSWD: /sbin/service
asterisk ALL = NOPASSWD: /sbin/init
asterisk ALL = NOPASSWD: /sbin/route
asterisk ALL = NOPASSWD: /bin/hostname
asterisk ALL = NOPASSWD: /bin/ln
asterisk ALL = NOPASSWD: /bin/bash
asterisk ALL = NOPASSWD: /bin/mv
asterisk ALL = NOPASSWD: /usr/sbin/amportal
asterisk ALL = NOPASSWD: /bin/cp
asterisk ALL = NOPASSWD: /bin/tar
asterisk ALL = NOPASSWD: /bin/rm
asterisk ALL = NOPASSWD: /bin/mkdir
asterisk ALL = NOPASSWD: /sbin/fdisk
asterisk ALL = NOPASSWD: /usr/bin/php
asterisk ALL = NOPASSWD: /bin/netstat
asterisk ALL = NOPASSWD: /usr/sbin/postconf
asterisk ALL = NOPASSWD: /usr/sbin/postmap
asterisk ALL = NOPASSWD: /usr/sbin/postfix
asterisk ALL = NOPASSWD: /usr/bin/crontab
asterisk ALL = NOPASSWD: /usr/bin/php -f /var/

There must be something I am doing wrong or this system just doesn't have all it needs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top