centos7 安裝ssh擴充套件控制linux

hwk_yellow發表於2016-11-16

安裝ssh2

PHP的一個擴充套件ssh2. 下載ssh2擴充套件,從http://pecl.php.NET/package/ssh2

如果是php5安裝ssh2-0.13或0.12擴充套件,php7安裝1.0

wget http://pecl.php.Net/get/ssh2-0.13.tgz


tar -zxvf ssh2-0.13.tgz

cd ssh2-0.13/

/usr/bin/phpize 

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

(如果失敗,請安裝 yum install libssh2-devel)

make

make install

測試

  1. <?php  
  2.   
  3. $host='127.0.0.1';//被控制的linux的ip  
  4.   
  5. $user='root';//使用者名稱  
  6.   
  7. $passwd='123456';//密碼  
  8.   
  9. // 連結遠端伺服器  
  10.   
  11. $connection = ssh2_connect($host, 22);  
  12.   
  13. if (!$connectiondie('connection to '.$host.':22 failed');  
  14.   
  15. echo 'connection OK<br/>';  
  16.   
  17. // 獲取驗證方式並列印  
  18.   
  19. $auth_methods = ssh2_auth_none($connection$user);  
  20.   
  21. print_r( $auth_methods.'<br/>');  
  22.   
  23. if (in_array('password'$auth_methods ))  
  24. {  
  25.   
  26.     // 通過password方式登入遠端伺服器  
  27.   
  28.     if (ssh2_auth_password($connection$user$passwd))  
  29.   
  30.     {  
  31.   
  32.         echo $user.' login OK<br/>';  
  33.   
  34.         $stream = ssh2_exec($connection"pwd"); // 執行php  
  35.   
  36.         stream_set_blocking($stream, true); // 獲取執行pwd後的內容  
  37.   
  38.          if ($stream === FALSE) die("pwd failed");  
  39.   
  40.         echo 'pwd: '.stream_get_contents($stream).'<br/>';  
  41.   
  42.     }  
  43.   
  44.     else  
  45.   
  46.     {  
  47.   
  48.         die$user.' login Failed<br/>');  
  49.   
  50.     }  
  51.   
  52. }  

相關文章