Execute commands on a remote computer via PHP

To specify the background in my environment:

I have 3 cars A, B and C

A = web server running on php website which basically acts as an interface for B and C

B = Linux Ubuntu, I have root access, SSH and everything I need to work on the machine through an SSH client (I have a .ppk private key file for this server)

C = MySQL MySql server running on Linux

I can successfully execute queries from A (php) to C (Mysql) and return the results. But now I'm trying to execute linux commands on B from A.

Eg.

I have a script that runs on B and would like to execute a command from A (php) to show the status of the script.

This is easy to do at the command line.

But I want to show the status of this script on a website hosting on server A.

Even just check the running time of server B on server A.

It is anyway. I googled forever, as it seems, but I donโ€™t get anywhere, Im not too phased if the connection is secure or not, because it is a closed network without external access to this network.

Any recommendations would be highly appreciated.

thanks

+6
source share
1 answer

Run SSH commands through PHP on server A to server B.

Here's how to run ssh commands from the command line on linux: http://www.youtube.com/watch?NR=1&feature=fvwp&v=YLqqdQZHzsU

To run commands on Linux using PHP, use the exec () command.

Hope this makes you start looking in the right direction.

Look at these two messages to automate password prompts.

Here is a short example with non-working code, so you think:

<?php $server = "serverB.example.org"; //ip address will work too ie 192.168.254.254 just make sure this is your public ip address not private as is the example //specify your username $username = "root"; //select port to use for SSH $port = "22"; //command that will be run on server B $command = "uptime"; //form full command with ssh and command, you will need to use links above for auto authentication help $cmd_string = "ssh -p ".$port." ".$username."@".$server." ".$command; //this will run the above command on server A (localhost of the php file) exec($cmd_string, $output); //return the output to the browser //This will output the uptime for server B on page on server A echo '<pre>'; print_r($output); echo '</pre>'; ?> 

The recommended thread is to run a command on server A on SSH on server B

+9
source

Source: https://habr.com/ru/post/946976/


All Articles