使用ssh2在PHP中进行多服务器连接

时间:2019-03-24 12:55:09

标签: php ssh

我有一个通过ssh2_connect连接到服务器的PHP API,我不知道该怎么做才能连接到2个服务器(不仅是1个)并发送相同命令的API

我使用php 7.0和ssh2_connect模块运行ubuntu 16.04

<?php
/////////////////////////////////
//--     API    --//
/////////////////////////////////
ignore_user_abort(true);
set_time_limit(0);
//////////////////////////////////////////
//--    Login    --//
//-- Credentials --//
//////////////////////////////////////////
$server_ip = "host"; //Change "1337" to your servers IP.
$server_pass = "pass"; //Change "pass" to your servers password.
$server_user = "root"; //Only change this if your using a user other than root.

/////////////////////////////////////////
//-- Gets the value of each variable --//
/////////////////////////////////////////
$key = $_GET['key'];
$host = $_GET['host'];
$port = intval($_GET['port']);
$time = intval($_GET['time']);
$method = $_GET['method'];
$action = $_GET['action'];

///////////////////////////////////////////////////
//-- array of implemented method as a variable --//
///////////////////////////////////////////////////
$array = array("method1", "method2", "stop");// Add you're existing methods here, and delete you're none existent methods.
$ray = array("apikey");

////////////////////////////////////////
//-- Checks if the API key is empty --//
////////////////////////////////////////
if (!empty($key)){
}else{
die('Error: API key is empty!');}

//////////////////////////////////////////
//-- Checks if the API key is correct --//
//////////////////////////////////////////
if (in_array($key, $ray)){ //Change "key" to what ever yo want you're API key to be.
}else{
die('Error: Incorrect API key!');}

/////////////////////////////////
//-- Checks if time is empty --//
/////////////////////////////////
if (!empty($time)){
}else{
die('Error: time is empty!');}

/////////////////////////////////
//-- Checks if host is empty --//
/////////////////////////////////
if (!empty($host)){
}else{
die('Error: Host is empty!');}
///////////////////////////////////
//-- Checks if method is empty --//
///////////////////////////////////
if (!empty($method)){
}else{
die('Error: Method is empty!');}

///////////////////////////////////
//-- Checks if method is empty --//
///////////////////////////////////
if (in_array($method, $array)){
}else{
die('Error: The method you requested does not exist!');}
///////////////////////////////////////////////////
//-- Uses regex to see if the Port could exist --//
///////////////////////////////////////////////////
if ($port > 44405){
die('Error: Ports over 44405 do not exist');}

//////////////////////////////////
//-- Sets a Maximum boot time --//
//////////////////////////////////             
if ($time > 10000){
die('Error: Cannot exceed 36000 seconds!');} //Change 10 to the time you used above.

if(ctype_digit($Time)){
die('Error: Time is not in numeric form!');}

if(ctype_digit($Port)){
die('Error: Port is not in numeric form!');}

//////////////////////////////////////////////////////////////////////////////
//--                        Methods                         --//
//-- Make sure the command is formatted correctly for each method you add --//
//////////////////////////////////////////////////////////////////////////////
if ($method == "method1") { $command = "commandtoexecute"; }
if ($method == "method2") { $command = "commandtoexecute"; }
if ($method == "stop") { $command = "pkill $host -f"; }
///////////////////////////////////////////////////
//-- Check to see if the server has SSH2 installed --//
///////////////////////////////////////////////////////
if (!function_exists("ssh2_connect")) die("Error: SSH2 does not exist on you're server");
if(!($con = ssh2_connect($server_ip, 22))){
  echo "Error: Server restarting";
} else {

///////////////////////////////////////////////////
//-- Attempts to login with you're credentials --//
///////////////////////////////////////////////////
    if(!ssh2_auth_password($con, $server_user, $server_pass)) {
        echo "Error: Login failed, one or more of you're server credentials are incorect.";
    } else {

////////////////////////////////////////////////////////////////////////////
//-- Tries to execute the attack with the requested method and settings --//
////////////////////////////////////////////////////////////////////////////   
        if (!($stream = ssh2_exec($con, $command ))) {
            echo "Error: You're server was not able to execute you're methods file and or its dependencies";
        } else {
////////////////////////////////////////////////////////////////////
//-- Executed the attack with the requested method and settings --//
////////////////////////////////////////////////////////////////////      
            stream_set_blocking($stream, false);
            $data = "";
            while ($buf = fread($stream,4096)) {
                $data .= $buf;
            }
                        echo "Sucessfuly sent command!";
            fclose($stream);
        }
    }
}
?>

我测试了类似if(!ssh2_auth_password($ con,$ server_user,$ server_pass))&& if(!ssh2_auth_password($ con2,$ server_user2,$ server_pass2)) 什么都没有。我真的需要帮助,以使其也连接到另一台服务器并发送相同的$ command

0 个答案:

没有答案