php ssh2實現遠程上傳、下載、執行命令

來源:互聯網
上載者:User

php遠程copy以及執行命令

php遠程copy檔案以及在遠程伺服器中執行命令時,所用到的模組是ssh2,以後所有的操作都依據ssh2串連控制代碼完成。

1. SSH2模組的安裝 1.1 安裝需要的擴充包

[plain]  view plain copy print ? wget  http://www.libssh2.org/download/libssh2-1.4.2.tar.gz   tar zxf libssh2-1.4.2.tar.gz   cd libssh2-1.4.2   ./configure && make && make install  

[plain]  view plain copy print ? wget  http://pecl.php.net/get/ssh2-0.11.3.tgz   cd ssh2-0.11.3   phpize   (如果報錯命令沒有找到,apt-get install php5-dev)   ./configure —with-ssh2 && make && make install  

Ubuntu下可以直接安裝[plain] view plain copy print ? apt-get install libssh2-1-dev libssh2-php  
使用直接安裝方式,不需要修改php配置資訊。
PS:

1.登入遠程主機:

localhost$ ssh -l jsmith remotehost

2.已串連遠程主機:

remotehost$

3.要臨時回到本地主機,輸入退出符號:“~”與“Control-Z”組合。

當你輸入“~”你不會立即在螢幕上看到,當你按下<Control-Z>並且按斷行符號之後才一起顯示。如下,在遠程主機中以此輸入“~<Control-Z>”
1.2 修改php配置資訊

[plain]  view plain copy print ? cd  /etc/php5/cgi   vim  php.ini   添加項:extension=/usr/lib/php5/20090626/ssh2.so        ssh2.so是編譯ssh2時得到的模組,上面是模組的位置。  

[plain]  view plain copy print ? cd  /etc/php5/cli   vim  php.ini   添加項:extension=/usr/lib/php5/20090626/ssh2.so        ssh2.so是編譯ssh2時得到的模組,上面是模組的位置。   1.3 重啟web伺服器

[plain]  view plain copy print ? /etc/init.d/lighttpd restart   1.4 查看是否載入了ssh2

[plain]  view plain copy print ? [root@localhost ~]php -m | grep ssh2   ssh2   2. SSH2模組的串連應用

SSH2串連有兩種方式,分別是使用者名稱密碼,ssh密鑰形式。 2.1 使用者名稱與密碼 [php]  view plain copy print ? $connection = ssh2_connect("192.168.6.222",22);   if (ssh2_auth_password($connection,"veno","ubuntu"))   {            echo "Authentication Successful! ";   }else{            die("Authentication Failed...");   }   2.2 ssh密鑰

[php] 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.