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]