php 操作linux下的mysql开启关闭

来源:互联网 发布:2016年济南市酒驾数据 编辑:程序博客网 时间:2024/04/28 03:20
<?php  
header('content-type:text/html;charset=utf8');
$status = $_GET['status'];
$host=“写自己的”';//被控制的linux的ip  
 
$user='写自己的';//用户名  
 
$passwd='写自己的';//密码  
 
// 链接远程服务器  
 
$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))  
 
    {  
 
       //echo $user.' login OK<br/>';  
 
        $stream = ssh2_exec($connection, "pwd"); // 执行php  
 
        stream_set_blocking($stream, true); // 获取执行pwd后的内容  
         /*$stream = ssh2_exec($connection, "ls");
           echo 'pwd: '.stream_get_contents($stream).'<br/>';*/
          //$stream = ssh2_exec($connection, "ls");
          //echo 'pwd: '.stream_get_contents($stream).'<br/>';
          if($status == 0){
                   $stream = ssh2_exec($connection, "service mysqld stop"); // 执行php  
          }else if($status == 1){
                  $stream = ssh2_exec($connection, "service mysqld start"); // 执行php  
          }
          $c = @mysql_connect($host,'root','root');
          //var_dump(@mysql_connect($host,'root','root'));die;
          if($c){
              $a = 1;
          }else{
              $a = 0;
          }
          //$stream  =  ssh2_exec ( $connection ,  'shutdown -h -now' );
        //if ($stream === FALSE) die("pwd failed");  
         
 
    }  
 
    else  
 
    {  
 
        die( $user.' login Failed<br/>');  
 
    }
 }

 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 <head>
     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
     <title>Document</title>
 </head>
 <body>
     <table>
         <tr>
             <td>主机昵称</td>
             <td>IP地址</td>
             <td>mysql服务状态</td>
             <td>操作</td>
         </tr>
         <tr>
             <td><?php echo $user;?></td>
             <td><?php echo $host;?></td>
             <td><?php
                 if($a == 1){
                     echo "开启";
                 }else{
                     echo "关闭";
                 }
             ?> </td>
             <td>
                 <?php
                 if($a == 1){
                     echo "<a href='yuekao.php?status=0'>关闭</a>";
                 }else{
                     echo "<a href='yuekao.php?status=1'>开启</a>";
                 }
             ?>
             </td>
         </tr>
     </table>
 </body>

 </html>

注意事项:

1. linux为centos6.4(如果为centos7的话只需更改启动和关闭的语法)

2.记得为linux下mysql的远程赋权

0 0
原创粉丝点击