php 操作linux下的mysql开启关闭
2016-08-26 19:16
399 查看
<?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的远程赋权
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的远程赋权
相关文章推荐
- linux,Centos 6.3 yum安装Nginx+php+Mysql,配置防火墙,开启,关闭端口命令
- linux_ubuntu_mysql服务关闭开启
- Linux C程序操作Mysql 调用PHP采集淘宝商品
- PHP : MySQL操作数据库 【 连接、建库、建表、增、删、改、查、关闭】
- Linux mysql 相关操作命令-配置日志时间,开启日志
- MySQL笔记1 - 终端与服务器的链接建立,服务器的开启与关闭,基本数句操作(增删改查)
- Linux下开启/关闭MySql Server命令
- Linux下开启/关闭MySql Server
- php 使用ssh2 操作linux、mysql(模拟阿里云控制台)
- linux下PHP错误警报开启与关闭
- Linux下mysql的远程连接与开启关闭MySQL服务
- Linux ubuntu下安装配置apache,php,mysql并实现操作
- Linux下开启/关闭MySql Server命令
- linux下的nginx/mysql/php启动、停止、重启命令操作
- Linux下开启/关闭MySql Server
- 在Linux操作下如何编译安装php&mysql&apache
- Linux下的MySQL简单操作(服务启动与关闭、启动与关闭、查看版本)
- Linux下的MySQL简单操作(服务启动与关闭、启动与关闭、查看版本)
- linux下开启、关闭、重启mysql服务
- mysql 事件开启与关闭操作