Jump to content

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
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.

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
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...
×
×
  • Create New...