创建使用php连接外部服务器的接口(并执行shell命令)

创建使用php连接外部服务器的接口(并执行shell命令)

问题描述:

I have 4 servers for web, dns, mail and database. I wanna to create an interface to remotely connect those servers using server's IP (may be port) and username and password(and later, in next step execute linux/winsows commands such as shutdown/reset server, stop/start IIS, stop/start mail server, ...)

Is it possible or any link to help me?

我有4台服务器用于web,dns,邮件和数据库。 我想创建一个接口,使用服务器的IP(可能是端口)和用户名和密码远程连接这些服务器(稍后,在下一步执行linux / winsows命令,如关机/重置服务器,停止/启动IIS,停止/启动 邮件服务器,...) p>

是否有可能或任何链接帮助我? p> div>

You can use ssh module as said by GeoPhoenix. You can try another method using curl. I assume you're using php, so consider following example,

function check_dserver_process(){
    if(!$ch)
       $ch = curl_init();


        $options=array( CURLOPT_URL => "http://122.165.212.22:81/mysite/init.php",
                        CURLOPT_FRESH_CONNECT => 1,
                        CURLOPT_RETURNTRANSFER => 1, 
                        CURLOPT_FORBID_REUSE =>1,
                        CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01;Windows NT 5.0)"
                     );

        curl_setopt_array($ch,$options);
        $result=curl_exec($ch);  
        $op=($result===false ? curl_error($ch) : $result);
        curl_close($ch);
        return $op;
}  

If you call above function it will fetches the output from init.php from the given url. so you can run your own commands from init.php using exec() function. Before you start the process in init.php make sure the request came from one of your ip.

sockets are a way to do that using a server-client model but i wouldn't suggest it.
SSH is another way to connect securely to the server and execute commands, you can create a CGI script at local computer and manage one server at a time, using forms/buttons etc.. note that you must have openssh-server package if server-side is using linux distribution.