Jump to content

exec remote command and store output in a variable


dmallia

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

Link to comment
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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...