Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Below is re-usable code named as "ssh_init.php" for remote management.

Code Block
languagephp
titlessh_init.php
<?php
// Programmed by Chun Kang (kurapa@kurapa.com) on Nov 2018

$m_config->session = null;

function get_line_count($r)
{
        $i=0;
        $len = strlen($r);
        $cnt=1;
        while($i<$len)
        {
                if (ord($r[$i])==10) $cnt++;
                $i++;
        }
        if ($cnt>40) $cnt=40;
        return $cnt;
}

function ssh2_run_cmd($cmd, $include_text_area=0)
{
        global $m_config;

        if ($m_config->session==NULL)
        {
                $m_config->session = ssh2_connect( $m_config->server, $m_config->port);

                if (!$m_config->session)
                {
                        die( "{$m_config->id}@{$m_config->server}:{$m_config->port}...connection failure!!<br>\n");
                }
                else
                {
                        if ($include_text_area) echo "{$m_config->id}@{$m_config->server}:{$m_config->port}...connection ok<br>\n";
                }

                if (ssh2_auth_password( $m_config->session, $m_config->id, $m_config->password))
                {
                        if ($include_text_area) echo "Authentication successful!<br>\n";
                }
                else
                {
                        die( "Authentication failure...");
                }
        }

        if ($include_text_area) echo "<pre>{$cmd}</pre>\n";
        $stream=ssh2_exec( $m_config->session, $cmd);

        stream_set_blocking($stream, true);
        $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
        $r=stream_get_contents( $stream_out);
        if ($include_text_area)
        {
                $rows_cnt = get_line_count( $r);
                return "<textarea cols=120 rows={$rows_cnt} wrap=off style='width:100%'>{$r}</textarea>\n";
        }
        else
        {
                return $r;
        }
}

function kssh2_disconnect() {
        ssh2_disconnect( $m_config->session);
        $m_config->session=NULL;
}

?>

...

Below code actually performs remote process and reboot services. Actually I am checking httpd, mysqld, two confluences and one Jira instance on below code.

Code Block
languagephp
titlebot-reboot-instances.php
<?

include_once "ssh_init.php";

$m_config->id = "<your user id>";
$m_config->password = "<user password>";
$m_config->server = "<server address>";
$m_config->port = 22;

function service_status($service_name, $message, $prc) {
        if (ereg( $message, $prc)) {
                echo "{$service_name} ok!\n";
                return 1;
        }
        else {
                echo "{$service_name} does not work!!\n";
                return 0;
        }
}

$prc=ssh2_run_cmd( "ps aux --sort=-pcpu");

if (strlen($prc)>0)
{
        echo "Process Status<br>\n";
        if (!service_status("httpd", "/usr/sbin/httpd -DFOREGROUND", $prc)) {
                ssh2_run_cmd( "/etc/init.d/httpd restart");
                echo  "httpd restared!\n";
        }
        if (!service_status("mysqld", "/usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql", $prc)) {
                ssh2_run_cmd( "/etc/init.d/mysqld retartrestart");
                echo  "mysqld restaredrestarted!\n";
        }
        if (!service_status("qsok.com - confluence server", "/pub/qsok.com-confluence/jre//bin/java", $prc)) {
                ssh2_run_cmd( "/etc/init.d/confluence restart");
                echo  "qsok.com restaredrestarted!\n";
        }
        if (!service_status("enewtown.com - confluence server", "/pub/enewtown.com-confluence/jre//bin/java", $prc)) {
                ssh2_run_cmd( "/etc/init.d/confluence1 restart");
                echo  "enewtown.com restaredrestarted!\n";
        }
        if (!service_status("qsok.com - jira server", "/pub/qsok.com-jira/jre//bin/java", $prc)) {
                ssh2_run_cmd( "/etc/init.d/jira restart");
                echo  "qsok.com restaredrestarted!\n";
        }
        echo "<hr>";
}
?>


In my case, I made a clone job to run above bot code to restart server instances automatically.