dqxafj6830 2010-02-23 11:45
浏览 184
已采纳

使用SFTP和PHP或shell / terminal脚本传输文件

I need to write a script that is run as a cron job every night which transfers some report files via sftp to another server.

The report files are created every night using another cron in the format 'support_[date].csv' & 'download_[date].csv'.

I'm wondering if you had any pointers on how to do the following:

  1. Find the 2 files created on latest [date]
  2. Copy these files to another server using SFTP

I've tried several PHP scripts utilising the ssh2 extension, but to no avail. Is there a way to do it using a shell script? It's not something I am hugely familiar with to be honest (hence going down the PHP route initially)

This was one of my PHP scripts which didn't work:

$src = 'test.csv';

$filename = 'test.csv';
$dest = '/destination_directory_on_server/'.$filename;

$connection = ssh2_connect('example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

// Create SFTP session
$sftp = ssh2_sftp($connection);

$sftpStream = fopen('ssh2.sftp://'.$sftp.$dest, 'w');

try {

            if (!$sftpStream) {
                throw new Exception("Could not open remote file: $dest<br>");
            }

            $data_to_send = file_get_contents($src);

            if ($data_to_send === false) {
                throw new Exception("Could not open local file: $src.<br>");
            }

            if (fwrite($sftpStream, $data_to_send) === false) {
                throw new Exception("Could not send data from file: $src.<br>");
            } else {
                //Upload was successful, post-upload actions go here...
            }

            fclose($sftpStream);

        } catch (Exception $e) {

            //error_log('Exception: ' . $e->getMessage());
           echo 'Exception: ' . $e->getMessage();
            if($sftpStream) {fclose($sftpStream);}

        }

This were the error messages I got:

Warning: fopen() [function.fopen]: URL file-access is disabled in the server configuration in /path_to_script/sftp-test.php on line 17

Warning: fopen(ssh2.sftp://Resource id

3/destination_directory_on_server/test.csv)

[function.fopen]: failed to open stream: no suitable wrapper could be found in /path_to_script/sftp-test.php on line 17 Exception: Could not open remote file: /destination_directory_on_server/test.csv

  • 写回答

6条回答 默认 最新

  • donglu7816 2010-02-24 12:43
    关注

    Muchos kudos to ghostdog74! Managed to get this working, but with sftp.

    First I managed to set up key authentication, then partly using ghostdog74's script I did this and it worked perfectly!

    cd /directorywithfilesin
    latest_download=$(ls -1tr download* | tail -1)
    latest_support=$(ls -1tr support* | tail -1)
    sftp username@example.com <<EOF
    cd /dir_to_copy_to
    put $latest_download
    put $latest_support
    EOF
    

    Thanks!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵