943,740 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 13859
  • PHP RSS
Sep 25th, 2008
-1

Running Linux Commands through PHP

Expand Post »
Hey folks,

I am fairly new in the field of PHP. I have to prepare a module that will show the CPU utilization, current processes, memory utilization and disk quota of a remote system when asked on a PHP page. I am clueless about how to proceed in this matter. My seniors asked me to do scripting in PHP through which I must run Linux Commands and then parse the result I got and display it in a web browser. Please help me out and guide me how to proceed in this matter.

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Sep 25th, 2008
0

Re: Running Linux Commands through PHP

You can execute linux commands within a php script - all you have to do is put the command line in backticks (`).

And also concentrate on exec() , this and shell_exec()..
Last edited by Shanti C; Sep 25th, 2008 at 2:20 am.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Sep 25th, 2008
0

Re: Running Linux Commands through PHP

Thanks Shanti but would you be a bit elaborate about how could I log in to a remote server. I had visited php.net earlier but the first and foremost problem for me is to log into the remote system. Suppose the username is "abcde", password is "wxyz" and IP address is 100.100.100.100. Please tell me how to first log into this system using these parameters. Any sort of help would be much appreciated.

Thanks Again
Gaurav
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Sep 25th, 2008
0

Re: Running Linux Commands through PHP

Sorry...i don't know much on this...
Any other experts will get you the information....
Sorry...
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Sep 25th, 2008
0

Re: Running Linux Commands through PHP

Click to Expand / Collapse  Quote originally posted by gargg321 ...
Thanks Shanti but would you be a bit elaborate about how could I log in to a remote server. I had visited php.net earlier but the first and foremost problem for me is to log into the remote system. Suppose the username is "abcde", password is "wxyz" and IP address is 100.100.100.100. Please tell me how to first log into this system using these parameters. Any sort of help would be much appreciated.

Thanks Again
Gaurav
It would probably be best to create a PHP page on the remote system that will get the "memory utilization and disk quota of a remote system" and display it to HTTP.

eg:

http://remote-sever.com/memory-and-disk-quota.php

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $results = exec('your command');
  4. echo $results;
  5.  
  6. ?>


Then from your local machine query the remote system over HTTP:
php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $results = file_get_contents('
  4. http://remote-sever.com/memory-and-disk-quota.php');
  5.  
  6. echo $results;
  7.  
  8. ?>

You can also build authentication into the remote machine, or use basic auth, etc.

eg: basic auth

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $results = file_get_contents('
  4. http://user:pass@remote-sever.com/memory-and-disk-quota.php');
  5.  
  6. echo $results;
  7.  
  8. ?>


This way, you don't ever send arbitrary code to the remote system, and limit the code execution to that hardcoded into your PHP file.

If you really need to pass arbitrary commands to the remote system for some reason, you may want to implement the SSH protocol. In this way, your PHP script will act as an SSH client, that will log into the SSH server on the remote system, execute commands and read the response.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 13th, 2008
0

Re: Running Linux Commands through PHP

Hi,

Pls tell me how to code this shell command in PHP

( echo "RGBA32 88 31 20 25 0 1" ; cat /var/www/vhosts/lankaclipstv.com/httpdocs/mylogo.rgba ) > /var/www/vhosts/lankaclipstv.com/httpdocs/mylogo.fifo &

Quote ...
<?php

echo shell_exec('( echo "RGBA32 197 32 120 205 0 1" ; cat /var/www/vhosts/lankaclipstv.com/httpdocs/mylogo.rgba ) > /var/www/vhosts/lankaclipstv.com/httpdocs/mylogo.fifo &');
?>
I tried above but it seems not working no output.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
john_robot is offline Offline
4 posts
since Aug 2005
Nov 13th, 2008
0

Re: Running Linux Commands through PHP

Click to Expand / Collapse  Quote originally posted by john_robot ...
Hi,

Pls tell me how to code this shell command in PHP

( echo "RGBA32 88 31 20 25 0 1" ; cat /var/www/vhosts/lankaclipstv.com/httpdocs/mylogo.rgba ) > /var/www/vhosts/lankaclipstv.com/httpdocs/mylogo.fifo &



I tried above but it seems not working no output.

There will be no output from those set of commands. All output from the first and seconds are sent to: /var/www/vhosts/lankaclipstv.com/httpdocs/mylogo.fifo

so if you want to see if it works, view the contents of /var/www/vhosts/lankaclipstv.com/httpdocs/mylogo.fifo

eg:

PHP Syntax (Toggle Plain Text)
  1. cat /var/www/vhosts/lankaclipstv.com/httpdocs/mylogo.fifo
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 13th, 2008
0

Re: Running Linux Commands through PHP

ok Thank you next thing is this SSH command its working fine on the command prompt(file converting). but not working in PHP. ( the file not converting)

mencoder -oac copy -vf bmovl=0:0:mylogo.fifo -ovc raw -o t2.mpg t1.mpg

Quote ...
<?php
echo shell_exec('/usr/bin/mencoder -oac copy -vf bmovl=0:0:/var/www/vhosts/lankaclipstv.com/httpdocs/mylogo.fifo -ovc raw -o /var/www/vhosts/lankaclipstv.com/httpdocs/t2.mpg /var/www/vhosts/lankaclipstv.com/httpdocs/t1.mpg');
?>
what do i have to do to fix this ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
john_robot is offline Offline
4 posts
since Aug 2005
Sep 22nd, 2010
0
Re: Running Linux Commands through PHP
PHP Syntax (Toggle Plain Text)
  1. <pre>
  2. <b>Uptime:</b>
  3. <?php system("uptime"); ?>
  4.  
  5. <b>System Information:</b>
  6. <?php system("uname -a"); ?>
  7.  
  8. <b>Memory Usage (MB):</b>
  9. <?php system("free -m"); ?>
  10.  
  11. <b>Disk Usage:</b>
  12. <?php system("df -h"); ?>
  13.  
  14. <b>CPU Information:</b>
  15. <?php system("cat /proc/cpuinfo | grep \"model name\\|processor\""); ?>
  16. </pre>
Last edited by Ezzaral; Sep 22nd, 2010 at 12:54 pm. Reason: Added code tags. Please use them to format any code that you post.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yogesh2tech is offline Offline
1 posts
since Sep 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: us phone number format
Next Thread in PHP Forum Timeline: google map





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC