php远程copy以及执行命令
php远程copy以及执行命令
php远程copy文件以及在远程服务器中执行命令时,所用到的模块是ssh2,以后所有的操作都依据ssh2连接句柄完成。
1. SSH2模块的安装1.1 安装需要的扩展包<?phpecho "是访问到这里来了";$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/>";