$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的遠程賦權