SSH2 需要安装 libssh2 和 ssh2 扩展,扩展安装请自行找我上一篇文章 linux下 php 安装 ssh2扩展_一遇一余的博客-优快云博客。
如果安装不成功,肯定是 ssh2 版本和php版本冲突。php7以下的装 ssh2-0.12 ,php7+ 的 ssh2-1.0+.
通过PHP去访问另一个服务器的文件,根据远程服务器,有的是要密码,有的要密钥。
当前只讨论密码的方式
实现远程访问 服务器 对服务器进行下载或上传操作
一.发送文件到服务器上,ssh2_scp_send(SSH2连接,本地文件路径,服务器上文件路径)
二.从服务器接收文件,路径都必须带上文件名,否则会失败
三种方式
1.scp接收 ssh2_scp_recv(SSH2连接,服务器文件路径,本地文件路径)
2. sftp接收
$resSftp = ssh2_sftp($connection);//SSH2连接,先声明SFTP资源
copy("ssh2.sftp://" . intval($resSftp).$remotefile, $local) 拷贝文件到本地
3. 通过获取文件内容,再在本地生成文件
$content = file_get_contents("ssh2.sftp://" . intval($resSftp).$remotefile); 远程读取到服务器的文件内容, 并 可以用file_put_contents("t.txt", $content); 写入到本地。
$host= '10.0.4.231';//服务器的ip
$user='root';//用户名
$passwd='icefire';//密码
// 链接远程服务器
$connection = ssh2_connect($host, 22);
if (!$connection) die('connection to '.$host.':22 failed');
echo 'connection OK';
// 获取验证方式并打印
$auth_methods = ssh2_auth_none($connection, $user);
//print_r( $auth_methods); echo ' ';
if (in_array('password', $auth_methods ))
{
// 通过password方式登录远程服务器
if (ssh2_auth_password($connection, $user, $passwd))
{
echo '----';
echo $user.' login OK';
$infile = 'D:\web\test.txt';
$remotefile = '/home/wwwroot/sds/admin2/combatlog/test.txt';
//向服务器传文件
// if(ssh2_scp_send($connection, $infile, $remotefile )){
// echo("ssh2_scp_send");
// }else{
// echo("NOT ssh2_scp_send");
// }
// die();
$infile = 'D:\web\log_1000401539__20220607__143614__436602864.csv';
$remotefile = '/root/server/Bin/log/combatlog/log_1000401539__20220607__143614__436602864.csv';
//从服务器下载文件到本地
if(!file_exists($infile)){
if(ssh2_scp_recv($connection, $remotefile, $infile)){
echo("received");
}else{
echo("NOT received");
}
// $revc = ssh2_scp_recv($connection, $remotefile.$filename , $infile ); //类似scp命令
echo ' 接收文件 '.$infile;
}else{
echo ' 文件已存在 ';
}
// die();
//sftp 从服务器拷到本地
$local = 'D:\web\sftpcp.csv';
$resSftp = ssh2_sftp($connection);
if(!file_exists($local)){
if(copy("ssh2.sftp://" . intval($resSftp).$remotefile, $local)){
echo 'sftp copy success! ';
}else{
echo 'sftp copy error! ';
}
}else{
echo ' sftp文件已存在 ';
}
//sftp 从服务器打开读取文件内容,再在本地生成 文件
$content = file_get_contents("ssh2.sftp://" . intval($resSftp).$remotefile);
//echo $content;
include_once 'exportExcel.php';
exportCsv('log_1000401539__20220607__143614__436602864.csv',$content);
die();
// SSH2 执行命令行
// $stream = ssh2_exec($connection, "命令1&&命令2"); // 一条一条地执行linux命令
//获取 目录下文件
$stream = ssh2_exec($connection, "ls /root/server/Bin/log/combatlog"); // 一条一条地执行linux命令
//流处理
stream_set_blocking($stream, true); // 获取执行pwd后的内容
if ($stream === FALSE) die("pwd failed");
echo "\r\n";
echo "文件目录 \r\n";
// echo stream_get_contents($stream);
$mulu = stream_get_contents($stream);
echo $mulu;
//分割成数组
$mulu1 = str_replace(PHP_EOL, "\n",$mulu);
$mulu1 = array_unique(explode("\n",$mulu1));
var_dump($mulu1);
}
else
{
die( $user.' login Failed');
}
}
我是直接生成一个 CSV文件,进行下载到本地
exportCsv($filename, $content);//导出为csv文件
函数方法:
/**
* 导出CVS文件
* @param $filename
* @param $data
*/
function exportCsv($filename, $data){
//定义文档类型为文本的csv
header('Content-Type: application/vnd.ms-excel;charset=utf-8');
// header("Content-type:text/csv");
//文档类型为附件,打开页面就直接下载对应附件,设置附件的名称为$filename
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control: max-age=0');
//设置页面强制不缓存作用和no-cache差不多,但是更强制
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
//过期时间为0
header('Expires:0');
header('Pragma:public');
echo "\xEF\xBB\xBF".$data; //添加BOM, 确保输出$content前没有任何其他东西输出
}