php通过拓展ssh2控制linux
来源:互联网 发布:英文翻译器软件 编辑:程序博客网 时间:2024/05/19 15:43
注意:我们用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
- $host='127.0.0.1';//被控制的linux的ip
- $user='root';//用户名
- $passwd='123456';//密码
- // 链接远程服务器
- $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后的内容
- if ($stream === FALSE) die("pwd failed");
- echo 'pwd: '.stream_get_contents($stream).'<br/>';
- }
- else
- {
- die( $user.' login Failed<br/>');
- }
- }
c.php
<?phpheader("content-type:text/html;charset=utf8");$str="systemctl status mysql.service ";//$str="pwd";$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);if (in_array('password', $auth_methods )){ // 通过password方式登录远程服务器 if (ssh2_auth_password($connection, $user, $passwd)) { /*echo $user.' login OK<br/>';*/ $stream = ssh2_exec($connection, $str); // 执行php $pwd=stream_set_blocking($stream, true);// var_dump($pwd); // 获取执行pwd后的内容 $pwd1=stream_get_contents($stream);// print_r($pwd1); $a=strpos($pwd1,'running');// var_dump($a); if($a) { $b='running'; $st="systemctl stop mysql.service"; }else { $b='dead'; $st="systemctl start mysql.service"; }// $stream = ssh2_exec($connection, $st); // 执行php /* $pwd=stream_set_blocking($stream, true); // 获取执行pwd后的内容 if ($stream === FALSE) die("pwd failed"); echo 'pwd: '.stream_get_contents($stream).'<br/>';*/ } else { die( $user.' login Failed<br/>'); }}?><!doctype html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><body><table border="1"> <tr> <td>主机昵称</td> <td>ip地址</td> <td>mysql服务状态</td> <td>操作</td> </tr> <tr> <td>我的阿里云</td> <td>**********</td> <td><?=$b?></td> <td> <?php if($b=='dead'){?> <a href="d.php?st=<?=$st?>">开启</a> <?php }else{?> <a href="d.php?st=<?=$st?>">关闭</a> <?php }?> </td> </tr></table></body></html>d.php
<?phpheader("content-type:text/html;charset=utf8");$st=$_GET['st'];$str="systemctl status mysql.service ";//$str="pwd";$host='127.0.0.1';//被控制的linux的ip$user='root';//用户名$passwd='Xg10086212!@#';//密码// 链接远程服务器$connection = ssh2_connect($host, 22);$auth_methods = ssh2_auth_none($connection, $user);if (in_array('password', $auth_methods )){ // 通过password方式登录远程服务器 if (ssh2_auth_password($connection, $user, $passwd)) { /*echo $user.' login OK<br/>';*/ $stream = ssh2_exec($connection, $str); // 执行php $pwd=stream_set_blocking($stream, true);// var_dump($pwd); // 获取执行pwd后的内容 $pwd1=stream_get_contents($stream);// print_r($pwd1); $stream = ssh2_exec($connection, $st); // 执行php /* $pwd=stream_set_blocking($stream, true); // 获取执行pwd后的内容 if ($stream === FALSE) die("pwd failed"); echo 'pwd: '.stream_get_contents($stream).'<br/>';*/ } else { die( $user.' login Failed<br/>'); }}$stream = ssh2_exec($connection, $st); // 执行phpheader("Location:c.php");
0 0
- php通过拓展ssh2控制linux
- php通过拓展ssh2控制linux
- php通过拓展ssh2控制linux
- php 通过 ssh2扩展控制Linux 阿里云web控制台
- php通过ssh2扩展控制linux,模拟阿里云web控制台
- php通过ssh2扩展控制linux,模拟阿里云web控制台
- php通过ssh2扩展控制linux,(模拟阿里云web控制台 )
- php通过ssh2扩展控制linux,模拟阿里云web控制台
- php通过ssh2扩展控制linux,模拟阿里云web控制台
- php通过ssh2扩展控制linux,模拟阿里云web控制台
- php通过ssh2扩展控制linux,模拟阿里云web控制台
- php通过ssh2扩展控制linux,模拟阿里云web控制台
- php远程操作linux,ssh2
- php的ssh2扩展实现远程控制
- php安装ssh2扩展控制linux,模拟阿里云web控制台
- ssh2拓展的使用
- linux下php扩展ssh2的安装
- linux下php扩展ssh2的详解
- c 指针算数运算
- 神器Tampermonkey(油猴子)使用+脚本分享
- 静态内部类和非静态内部类
- 服务器外网端口无法访问 解决办法
- POJ2251:Dungeon Master
- php通过拓展ssh2控制linux
- curl_exec(): 2 is not a valid cURL handle resource
- LeetCode Two Point & Array Problem 两点问题汇总
- poj1250
- 八进制到十进制
- 神经网络浅讲:从神经元到深度学习
- Java Anootation
- 01背包问题
- 构建nodejs环境及解决node服务器外网端口无法访问