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"