php通过ssh2扩展控制linux,模拟阿里云web控制台

注意:我们用php来控制linux,php环境可以在windows也可以在linux,但是我们要控制的机器是一台linux(被控制的linux关闭selinux和firewalld)。

如果php在linux,不会安装没关系,可以参考安装lamp教程地址:  http://blog.csdn.net/zph1234/article/details/51248124

然后我们的php环境要想实现控制linux,必须安装php的一个扩展ssh2

0.下载ssh2扩展,从http://pecl.php.net/package/ssh2,有linux扩展包,也有windows的dll,如果是php5安装ssh2-0.13或0.12扩展,php7安装1.0

1.windows中的php安装扩展,需要下载dll文件,在php.ini中引入即可

2.这里是centos中的php安装ssh2扩展,记得关闭selinux和firewalld

yum install libssh2 libssh2-devel php-devel gcc-c++

3.如果是php5安装ssh2-0.13扩展,php7安装1.0

cd ssh2-0.13/

/usr/bin/phpize 
./configure --with-php-config=/usr/bin/php-config LIBS=-ldl
make
make install

4. vi /etc/php.ini

加入extension=ssh2.so

5.重启apache

6.vi test.php

<?php

    //header('Content-type:textml;charset=utf8');
    $host='192.168.1.115';//被控制的linux的ip

    $user='root';//用户名

    $passwd='root';//密码

    // 链接远程服务器

    $connection = ssh2_connect($host, 22);

    if (!$connection) die('connection to '.$host.':22 failed');

    echo 'connection OK<br/>';

    // 获取验证方式并打印

    $auth_methods = ssh2_auth_none($connection, $user);

   // print_r( $auth_methods.'<br/>');

    if (in_array('password', $auth_methods ))
    {

        // 通过password方式登录远程服务器

        if (ssh2_auth_password($connection, $user, $passwd))

        {
            if(isset($_GET['caozuo']))
            {
                $stream = ssh2_exec($connection, "systemctl ".$_GET['caozuo']." mariadb"); // 执行php

                stream_set_blocking($stream, true); // 获取执行pwd后的内容

                if ($stream === FALSE) die("pwd failed");
            }
            $caozuo=isset($_GET['caozuo'])?$_GET['caozuo']:"";
            $stream = ssh2_exec($connection, "systemctl ".$caozuo." mariadb");

            echo $user.' login OK<br/>';
            //服务器名称
            $stream = ssh2_exec($connection, "hostname"); // 执行php 

            stream_set_blocking($stream, true); // 获取执行pwd后的内容

            if ($stream === FALSE) die("pwd failed");
            
            $name=stream_get_contents($stream).'<br/>';

            //ip
            $stream = ssh2_exec($connection, "ifconfig eno16777736 | grep \"inet \" | awk '{ print $2}'"); // 执行php

            stream_set_blocking($stream, true); // 获取执行pwd后的内容

            if ($stream === FALSE) die("pwd failed");

            $ip= stream_get_contents($stream).'<br/>';
            //运行状态
            $stream = ssh2_exec($connection, "systemctl status mariadb"); // 执行php

            stream_set_blocking($stream, true); // 获取执行pwd后的内容

            if ($stream === FALSE) die("pwd failed");

            $status=strpos(stream_get_contents($stream),'running')?'running':'dead';

        }

        else

        {

            die( $user.' login Failed<br/>');

        }

    }

?>




    <table border="1" cellpadding="" width="">
    <tr>
        <th>主机昵称</th>
        <th>ip地址</th>
        <th>mysql的运行状态</th>
        <th>操作</th>
    </tr>
    <tr>
        <td><?php echo $name?></td>
        <td><?php echo $ip?></td>
        <td><?php echo $status?></td>
        <td><a href="?caozuo=<?php if($status=='running') {echo "stop";} else {echo "start";}?>">
                <?php
                if($status=='running')
                {
                    echo "停止";
                }
                else
                {
                    echo "启动";
                }
                ?>
            </a></td>
    </tr>
</table>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值