Solved

Reading Hard Drive Serial Number using PHP

Posted on 2008-06-10
9
4,150 Views
Last Modified: 2013-12-13
I am unable to run the below command in PHP file, by browsing the URL http://localhost/hddinfo.php, the status is 25, output array is empty.

exec ("hdparm -i /dev/hda", $output, $status);

But same will execute from Linux command line and shows output: php hddinfo.php

My requirement is to get the Hard Disk Serial Number on Linux box through my PHP code.

Quick help is much appriciated
0
Comment
Question by:uvals
  • 4
  • 3
9 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 21750335
try using the complete path to hdparam. I guess you run it on command line as root, but vi webserver it runs as wwwrun which has no permissions to access those information. The infromation are only accessable by root.
exec ("/sbin/hdparm -i /dev/hda", $output, $status);
0
 
LVL 24

Expert Comment

by:glcummins
ID: 21750341
When you access the hddinfo.php script via URL, you are running the script with the permission of your web server users. When you run it via the command line, you are running the script with the permissions of the account with which you are logged in. In most cases, your webserver user will not have permissions to execute commands such as hdparm for security reasons. To find out which user your webserver is running as, open your httpd.conf file and look for the line

 User <username>

where <username> is the name that Apache runs as. You can either give that user access to run hdparm (not wise on a public webserver) or find a different method to obtain the data.

Perhaps a secure method is to run a cron job that gets the data you desire and stores it in a database or file. Then your script can simply read that file, and display it as needed.
0
 
LVL 2

Author Comment

by:uvals
ID: 21791480
I tried executing exec ("/sbin/hdparm -i /dev/hda", $output, $status); It still shows me error code 25 when i execute as http://localhost/a1.php.
Also when i try running the command
hdparm -i /dev/hda >> testserialno.txt
through command line, I am unable to write into the file. How can i write the response to a file? How can I find out the serial number using exec command in PHP?

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 48

Accepted Solution

by:
hernst42 earned 250 total points
ID: 21791797
You can't get those information via webserver and using exec
hdparm is only allowed for root and not for normal users like the webserver.
run as root:
hdparm -i /dev/hda >> /tmp/testserialno.txt

and in php
echo file_get_contents('/tmp/testserialno.txt');
0
 
LVL 2

Author Comment

by:uvals
ID: 21792581
i executed hdparm -i /dev/hda >> /tmp/testserialno.txt  in command prompt. It is not writing the content into a file. i gave permission to the file too. But I am unable to write the content of hdparm into the file.
Is there any other way to read hard disk serial number in linux and display the contents using PHP?
Or How can i set a cron job to read hard disk serial number and write it into a file?
Is there a way i to read a unique serial number - may be network card #, harddisk # or processor id,etc..in linux and display the output using PHP?
0
 
LVL 2

Author Comment

by:uvals
ID: 21882610
Any answers to this... even if i read the network card id or mac address of the newtwork card is ok. i hope mac address of each network card is unique. How we do it with PHP?
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21890148
exec ("/sbin/ifconfig", $output, $status);

MAC can be easy changed on linux, it's a part of the ifconfig command
0
 
LVL 2

Author Closing Comment

by:uvals
ID: 31465687
if we modify permission to PHP file, this command may work, but if some one gives that solution it will be a complete solution to this query.

http://find.b121.com
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question