[PHP] Выполнение rcon на нескольких серверах

HooLIGUN

MatchMaking CS:S / RGMIX.ru
Сообщения
1,069
Реакции
269
Здравствуйте.
Есть скрипт, который посылает rcon команду на сервер.
Кто может его подправить, чтобы можно было указывать сразу несколько северов (например 5 серверов, IP разный).
Не хочется под каждый сервер делать отдельно файл и команду в планировщик.
Заранее спасибо
C-подобный:
<?php
    require __DIR__ . '/../SourceQuery/bootstrap.php';

    use xPaw\SourceQuery\SourceQuery;
   
    // For the sake of this example
    Header( 'Content-Type: text/plain' );
    Header( 'X-Content-Type-Options: nosniff' );
   
    // Edit this ->
    define( 'SQ_SERVER_ADDR', '127.0.0.1' );
    define( 'SQ_SERVER_PORT', 27015 );
    define( 'SQ_TIMEOUT',     1 );
    define( 'SQ_ENGINE',      SourceQuery::SOURCE );
    // Edit this <-
   
    $Query = new SourceQuery( );
   
    try
    {
        $Query->Connect( SQ_SERVER_ADDR, SQ_SERVER_PORT, SQ_TIMEOUT, SQ_ENGINE );
       
        $Query->SetRconPassword( 'my_awesome_password' );
       
        var_dump( $Query->Rcon( 'say hello' ) );
    }
    catch( Exception $e )
    {
        echo $e->getMessage( );
    }
   
    $Query->Disconnect( );
 

Kruzya

Участник
Сообщения
12,970
Реакции
10,927
  • Команда форума
  • #2
PHP:
<?php
    require __DIR__ . '/../SourceQuery/bootstrap.php';

    use xPaw\SourceQuery\SourceQuery;
   
    // For the sake of this example
    Header( 'Content-Type: text/plain' );
    Header( 'X-Content-Type-Options: nosniff' );

    $servers = array(
        array(
            'ip'        => '127.0.0.1',
            'port'      => 27015,
            'engine'    => SourceQuery::SOURCE,
            'timeout'   => 1,
            'command'   => 'say hello',
            'password'  => 'rcon'
        ),
        array(
            'ip'        => '127.0.0.1',
            'port'      => 27016,
            'engine'    => SourceQuery::SOURCE,
            'timeout'   => 1,
            'command'   => 'say hello',
            'password'  => 'rcon'
        ),
        array(
            'ip'        => '127.0.0.1',
            'port'      => 27017,
            'engine'    => SourceQuery::SOURCE,
            'timeout'   => 1,
            'command'   => 'say hello',
            'password'  => 'rcon'
        )
        // <...>
    );
   
    $Query  = new SourceQuery( );
    $size   = count( $servers );

    for ($i = 0; $i < $size; $i++) {
        try {
            $Query->Connect( $servers[$i]['ip'], $servers[$i]['port'], $servers[$i]['timeout'], $servers[$i]['engine'] );
            $Query->SetRconPassword( $servers[$i]['password'] );
            var_dump( $Query->Rcon( $servers[$i]['command'] ) );
        } catch( Exception $e ) {
            echo $e->getMessage( );
        }

        $Query->Disconnect( );
    }
 

Grey83

не пишу плагины с весны 2022
Сообщения
8,609
Реакции
5,096
Ну я бы сделал как-то так:
PHP:
<?php
    require __DIR__ . '/../SourceQuery/bootstrap.php';

    use xPaw\SourceQuery\SourceQuery;

    // For the sake of this example
    Header( 'Content-Type: text/plain' );
    Header( 'X-Content-Type-Options: nosniff' );

    define( 'SQ_TIMEOUT',    1 );
    define( 'SQ_ENGINE',    SourceQuery::SOURCE );

    // Edit this ->
    $servers = array('Server1' => array('127.0.0.1', '27015', 'password1'),
                    'Server2' => array('127.0.0.1', '27016', 'password2'),
                    'Server3' => array('127.0.0.1', '27017', 'password3'),
                    'Server4' => array('127.0.0.1', '27018', 'password4'),
                    'Server5' => array('127.0.0.1', '27019', 'password5'));
    // Edit this <-

    $count = count($servers);

    for ($i = 0; $i < $count; $i++) {
        $Query = new SourceQuery( );

        try
        {
            $Query->Connect( servers[i][0], servers[i][1], SQ_TIMEOUT, SQ_ENGINE );

            $Query->SetRconPassword( servers[i][2] );

            var_dump( $Query->Rcon( 'say hello' ) );
        }
        catch( Exception $e )
        {
            echo $e->getMessage( );
        }

        $Query->Disconnect( );
    }
?>
 
Сверху Снизу