Jump to content
Sign in to follow this  
dmallia

exec remote command and store output in a variable

Recommended Posts

So i am building an admin page as part of a project for the course I am doing. I want to execute the remote command "top" (it doesn't matter if i execute it by php ssh connection) on linux and than I want to store the output of the command in a variable called $data. Is that possible? I tried doing it with shell_exec() and exec() but couldn't get it to work. Thanks before hand.

Edited by dmallia

Share this post


Link to post
Share on other sites

What OS are you using?For FreeBSD use "top -d 1"For Linux use "top -n 1"cuz"top" needs a input for exit so its not a good idea to use just "top"but its will work perfect with "ps" command

Share this post


Link to post
Share on other sites

shell_exec is the function to use to execute a shell command and store the output. Note you can only run that command on the server that PHP is running on, if you want to execute it on a remote server then you need to use something like SSH to connect, log in, and then run commands.

Share this post


Link to post
Share on other sites
$data = shell_exec('top -d 1');echo $data;

i tried this code but nothing is loading. than just to test if the code is good i tried this

$data = shell_exec('free -m');echo $data;

and it worked correctly

Edited by dmallia

Share this post


Link to post
Share on other sites

If you're trying to capture output from top then you should run it in batch mode with a set number of iterations, probably only 1.

Share this post


Link to post
Share on other sites
$output = null;exec('top -n 1', $output);print_r($output);

i modified the script but the array is empty.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...