php遠程copy文件以及在遠程服務器中執行命令時,所用到的模塊是ssh2,之後全部的操做都依據ssh2鏈接句柄完成。php
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
wget https://pecl.php.net/get/ssh2-1.1.2.tgz cd ssh2-0.11.3 phpize ./configure —with-ssh2 && make && make install
Ubuntu下能夠直接安裝:html
apt-get install libssh2-1-dev libssh2-php
使用直接安裝方式,不須要修改php配置信息。ubuntu
PS:vim
1.登入遠程主機:bash
localhost$ ssh -l jsmith remotehost服務器
2.已鏈接遠程主機:session
remotehost$app
3.要臨時回到本地主機,輸入退出符號:「~」與「Control-Z」組合。ssh
當你輸入「~」你不會當即在屏幕上看到,當你按下<Control-Z>而且按回車以後才一塊兒顯示。以下,在遠程主機中以此輸入「~<Control-Z>」curl
cd /etc/php5/cli vim php.ini 添加項:extension=/usr/lib/php5/20090626/ssh2.so ssh2.so是編譯ssh2時獲得的模塊,上面是模塊的位置。
1.3 查看是否加載了ssh2
[root@localhost ~]php -m | grep ssh2 ssh2
SSH2鏈接有兩種方式,分別是用戶名密碼,ssh密鑰形式。
$connection = ssh2_connect("192.168.6.222",22); if (ssh2_auth_password($connection,"veno","ubuntu")) { echo "Authentication Successful! "; }else{ die("Authentication Failed..."); }
$connection = ssh2_connect('192.168.6.222', 22, array('hostkey'=>'ssh-rsa')); if (ssh2_auth_pubkey_file($connection, 'root', '/root/.ssh/id_rsa.pub', '/root/.ssh/id_rsa')) { echo "Public Key Authentication Successful\n"; } else { echo ('Public Key Authentication Failed'); }
Ps: 密鑰生成:
在服務器192.168.6.229上面以root登陸
#ssh-keygen
有什麼提問基本上回車便可
後成的公鑰在~/.ssh/id_rsa.pub
若是想在229經過密鑰驗證登陸到222上面,則把229的公鑰id_rsa.pub cp到222上面,先手動登陸root,而後運行:
cat id_rsa.pub >> ~/.ssh/authorized_keys
這樣就能夠在229上面ssh 192.168.6.229不須要輸入密碼便可登陸到222。上面php代碼在229上面也能夠經過驗證。
經過SSH2驗證經過後,獲得的鏈接符爲$connection
遠程服務器文件copy到本地:
bool ssh2_scp_recv ( resource $session, string $remote_file, string $local_file )
Ps: 接收文件時,後面文件名能夠爲空,如:
ssh2_scp_recv($connection, '/home/xiaozl/veno1.exe', '/home/xiaozl/')
本地文件copy到遠程服務器
bool ssh2_scp_send ( resource $session, string $local_file, string $remote_file [, int $create_mode] )
Ps:發送文件時,後面的文件名不能爲空,如:
ssh2_scp_send($connection, '/home/xiaozl/package.xml', '/home/xiaozl/package.xml');
resource ssh2_exec( resource $session, string $command [, string $pty [, array $env [, int $width [, int $height [, int $width_height_type]]]]] )
在229上面執行222上的命令具體實例:
$tcmd="cd /home/veno/gateway/radiusclient;"; $tcmd.="./nastool.sh get-status app=2b1c5364-db39-c76d-842c-11d4a81d555d"; $stream = ssh2_exec($connection, $tcmd); stream_set_blocking($stream,true); echo stream_get_contents($stream); ssh2_exec() 是會有返回值的,而它的返回值是無格式的(無換行),但它是以stream(流的形式返回), 顯示出來,可這樣進行流處理: stream_set_blocking($stream, true); echo :stream_get_contents($stream);
<?php echo "是訪問到這裏來了"; $connection = ssh2_connect('192.168.6.222', 22, array('hostkey'=>'ssh-rsa')); if (ssh2_auth_pubkey_file($connection, 'root', '/root/.ssh/id_rsa.pub', '/root/.ssh/id_rsa')) { echo "Public Key Authentication Successful\n"; //$flag=ssh2_scp_recv($connection, '/home/xiaozl/veno1.exe', '/home/xiaozl/'); //接收時,後面文件名能夠爲空 //$flag=ssh2_scp_send($connection, '/home/xiaozl/package.xml', '/home/xiaozl/package.xml'); //發送時,後面的文件名不能爲空 //echo $flag; $tcmd="cd /home/veno/gateway/radiusclient;"; $tcmd.="./nastool.sh get-status app=2b1c5364-db39-c76d-842c-11d4a81d555d"; $stream = ssh2_exec($connection, $tcmd); stream_set_blocking($stream,true); echo stream_get_contents($stream); } else { echo ('Public Key Authentication Failed'); } echo "<br/>"; $connection = ssh2_connect('192.168.6.222', 22); ssh2_auth_password($connection, 'veno', '*******'); $tcmd="cd /home/veno/gateway/radiusclient;"; $tcmd.="./nastool.sh get-status app=2b1c5364-db39-c76d-842c-11d4a81d555d"; $stream = ssh2_exec($connection, $tcmd); echo "<br/>"; echo "-----------------------------------------------"; stream_set_blocking($stream,true); echo stream_get_contents($stream); echo "-----------------------------------------------"; echo "<br/>";
---------------------------------------------------------------------------------------------------------------------------------------------------------------
<?php
$connection = ssh2_connect('192.168.1.XX', 22);
$res=ssh2_auth_password($connection, 'root', 'XXX');
$re= ssh2_scp_recv($connection, '/home/test1.php', '/var/www/html/test1.php');
var_dump($res);
var_dump($re);
實驗來無數次,ssh2_scp_send成功。recv失敗,折騰了一成天,最後搞個兩臺機器間的密鑰認證就解決了
緣由應該就是recv時,須要本地機器的密碼吧,因此沒密鑰認證前一直recv失敗
兩臺服務器共用session
<?php $connection = ssh2_connect('xxx.xxx.xxx.xxx', 22, array('hostkey'=>'ssh-rsa')); if (ssh2_auth_pubkey_file($connection, 'root', '/root/.ssh/id_rsa.pub', '/root/.ssh/id_rsa')) { echo "Public Key Authentication Successful\n"; } else { exit('Public Key Authentication Failed'); } foreach (glob("/tmp/sess_*") as $filename) { if(filesize($filename)!=0){ ssh2_scp_send($connection, $filename, $filename, 0644); echo "$filename size " . filesize($filename) . "\n"; } } ssh2_exec($connection , "chown www.www /tmp/sess_*");
curl測試是否成功(可導入到文件):
curl "http://127.0.0.1:777/index/business/index.html" -H "Cookie:PHPSESSID=r8aol0jnof9t840s8c81nspp25" -d "param=185&productType=1&num=1&clientType=2"