简体   繁体   English

ssh2_scp_send() 返回“无法打开流:没有这样的文件或目录”

[英]ssh2_scp_send() returning "Failed to open stream: No such file or directory"

Today I'm battling against ssh with huge success if it wasn't for ssh2_scp_send() method.如果不是 ssh2_scp_send() 方法,今天我正在与 ssh 作斗争并取得巨大成功。 This is the first time I work integrating ssh with PHP, so you may find multiple erros in the code that I have yet to discover, since I am currently stuck at that part.这是我第一次将 ssh 与 PHP 集成,因此您可能会在我尚未发现的代码中发现多个错误,因为我目前停留在该部分。

I am using a dummy array imitating the one I must get from a couple of forms and the auth is working fine, although I had a little problem back there given that I had to use 'root' as user for the public key to be recognised as correct when comparing against, instead of my actual user.我正在使用一个虚拟数组来模仿我必须从几个表单中获得的一个,并且身份验证工作正常,尽管我在那里遇到了一个小问题,因为我必须使用“root”作为用户才能识别公钥比较时正确,而不是我的实际用户。 Once I solved that matter, I discovered that the code is not working properly, this time the console says:一旦我解决了那个问题,我发现代码运行不正常,这次控制台说:

ssh2_scp_send(tmp/sshPm/prueba1.csv): failed to open stream: No such file or directory in /home/josemaria/Desktop/API UBcloud/CSV/provisioning.php on line 32 ssh2_scp_send(tmp/sshPm/prueba1.csv):无法打开流:第 32 行的 /home/josemaria/Desktop/API UBcloud/CSV/provisioning.php 中没有这样的文件或目录

Here is my code:这是我的代码:

function arrayToCsv($array, $filename, $delimiter = ';'){

    header('Content-Type: application/csv');


    header('Content-Disposition: attachment; filename="'.$filename.'";');

    $f = fopen('/tmp/sshPm/'.$filename, 'r');


    foreach ($array as $line) {
        fputcsv($f, $line, $delimiter);
    }
} 



function connectSsh($filename){
    $sshConnection = ssh2_connect($host, $port);

    if (!$sshConnection) {
        die('Conexión Fallida');
    }

    ssh2_auth_pubkey_file($sshConnection, $user, $pubKey, $priKey);

    ssh2_scp_send($sshConnection, "/tmp/sshPm/$filename", '/home/scripts/CSV', 0644);

    $stream = ssh2_exec($sshConnection, "/home/scripts/sc_prov_ovpn_firm.sh $filename");

    ssh2_exec($sshConnection, 'exit');

    return $stream;
}



for ($i=0; $i < 10; $i++) { 

    $array[] = array(
        "VAAAAAGH", 
        "THE WAGH IS HERE", 
        200 + $i, 
        564451 +$i, 
        "sip",
        "",
        "",
        "",
        "8.8.8.8",
        "8.8.4.4",
        20048,
        "Modelo Terminal",
        "677shdG3"
    );
}

$filename = 'test.csv';


arrayToCsv($array, $filename);
$stream = connectSsh($filename);
print_r($stream);

As you can see, I intend the CSV's to be created and stored in /tmp.如您所见,我打算在 /tmp 中创建和存储 CSV。 Even though the csv is created and placed in the right directory, whenever I reach scp_send, this method proves incapabe of finding it.即使 csv 被创建并放置在正确的目录中,每当我到达 scp_send 时,这种方法证明无法找到它。 I don't know if this could be related to the fact that I am using root to veify my public key as I've seen that it should be the user you are logged in with.我不知道这是否与我使用 root 验证我的公钥这一事实有关,因为我已经看到它应该是您登录的用户。

I also get the following warning right inmediately, but I guess this is consecuence of the first one... In any case, here it is:我也立即收到以下警告,但我想这是第一个的后果......无论如何,这里是:

PHP Warning: ssh2_scp_send(): Unable to read source file in /home/josemaria/Desktop/API UBcloud/CSV/provisioning.php on line 32. PHP 警告:ssh2_scp_send():无法读取第 32 行 /home/josemaria/Desktop/API UBcloud/CSV/provisioning.php 中的源文件。

I have tried using a wrapper instead of fopen() but with no success.我曾尝试使用包装器代替 fopen() 但没有成功。 As I said, this is the first time I work using ssh and PHP so I would ask you to explain a little bit at least!正如我所说,这是我第一次使用 ssh 和 PHP 工作,所以我会请你至少解释一下! Thank you so much for the help!十分感谢你的帮助!

UPDATE更新

I managed to solve partially the issue by following ArSeN's advice and creating a directory in /Desktop and changing all the routes to that one, instead of /tmp.我设法通过遵循 ArSeN 的建议并在 /Desktop 中创建一个目录并将所有路由更改为该目录而不是 /tmp 来部分解决该问题。 Now the problem I face is that I am not sure where to place the files once created.现在我面临的问题是我不确定一旦创建文件应该放在哪里。 So my next question related to this issue would be:所以我与这个问题相关的下一个问题是:

Where should I store all the CSV's generated locally?我应该在哪里存储本地生成的所有 CSV? As you see, I am doing it in /Documents since I have no restrictions there to read/modify, but I would say the answer lies in /, maybe /var?如您所见,我在 /Documents 中执行此操作,因为我在那里没有读取/修改的限制,但我会说答案在于 /,也许是 /var? I really have no clue about much of this stuff...我真的对这些东西一无所知......

Thank you again for the help provided!再次感谢您提供的帮助!

This is what my code looks like now:这就是我的代码现在的样子:

function arrayToCsv($array, $filename, $delimiter = ','){

    header('Content-Type: application/csv');


    header('Content-Disposition: attachment; filename="'.$filename.'";');

    $f = fopen("/home/josemaria/Documents/sshPm/$filename", 'w');


    foreach ($array as $line) {
        fputcsv($f, $line, $delimiter);
    }

    fclose($f);
} 



function connectSsh($filename){
    $sshConnection = ssh2_connect($host, $port);

    if (!$sshConnection) {
        die('Conexión Fallida');
    }

    ssh2_auth_pubkey_file($sshConnection, $user, $pubKey, $priKey);

    ssh2_scp_send($sshConnection, "/home/josemaria/Documents/sshPm/$filename", "/home/scripts/CSV/$filename", 0644);

    $stream = ssh2_exec($sshConnection, "/home/scripts/sc_prov_ovpn_firm.sh $filename");

    ssh2_exec($sshConnection, 'exit');

    return $stream;
}



for ($i=0; $i < 10; $i++) { 

    $array[] = array(
        "VAAAAAGH", 
        "THE WAGH IS HERE", 
        200 + $i, 
        564451 +$i, 
        "sip",
        "",
        "",
        "",
        "8.8.8.8",
        "8.8.4.4",
        20048,
        "Modelo Terminal",
        "677shdG3"
    );
}

$filename = 'test.csv';


arrayToCsv($array, $filename);
$stream = connectSsh($filename);
print_r($stream);

You should close the file handler so the file does actually get written and is not in some I/O buffer.您应该关闭文件处理程序,以便文件确实被写入并且不在某些 I/O 缓冲区中。

function arrayToCsv($array, $filename, $delimiter = ';'){
    // all your existing code here ...    
    fclose($f);
} 

Also with your copy target it seems like you are putting a file where a folder is before, meaning:同样,对于您的复制目标,您似乎将文件放在之前的文件夹中,这意味着:

ssh2_scp_send($sshConnection, "/tmp/sshPm/$filename", '/home/scripts/CSV', 0644);

should probably be:应该是:

ssh2_scp_send($sshConnection, "/tmp/sshPm/$filename", "/home/scripts/CSV/$filename", 0644);

The answer to my issue had to do with permissions.我的问题的答案与权限有关。 I had some other errors as @ArSeN pointed out, but the warning I got and which prevented the code to work was because I was trying to store and read the files in /tmp withouth the permissions required to do so.正如@ArSeN 指出的那样,我还有一些其他错误,但是我得到的警告阻止了代码工作是因为我试图在 /tmp 中存储和读取文件,而没有这样做所需的权限。 So give yourself full permissions on the dir or change the dir for another.因此,给自己对该目录的完全权限或更改另一个目录。 I still have some problems and issues with this code, but I do feel that they belong to a separate question that I will be linking here: How can I generate a CSV on Win and Linux using PHP and ProcessMaker?这段代码仍然存在一些问题和问题,但我确实觉得它们属于一个单独的问题,我将在此处链接: 如何使用 PHP 和 ProcessMaker 在 Win 和 Linux 上生成 CSV? Which paths are reccommended to store the files locally? 建议使用哪些路径在本地存储文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM