使用bash SFTP / SCP文件 [英] sftp/scp files with bash

查看:226
本文介绍了使用bash SFTP / SCP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一组文件上传到SFTP帐户​​。

I have the need to upload a set of files to an sftp account.

我首先想到的是使用PHP的ssh2_connect()函数,我能得到这个工作在当地没有问题。但是,一旦我搬到了开发环境我很快意识到,这不是一个很好的解决方案,因为有一些不会存在太多的依赖并安装它们需要太多的审批从人太多了。

My first thought was to use php's ssh2_connect() function and I was able to get this working locally no problem. However, once I moved to the dev environment I quickly realized that this wasn't a good solution because there are too many dependencies that wont exist and installing them would require too many approvals from too many people.

所以,我的下一个念头,就是使用bash,而这正是我需要帮助。我将贯穿的cron每个小时这个bash脚本,所以它需要无人值守。然而,当我运行SFTP / SCP它需要一个密码。

So, my next thought was to use bash, and this is where I need help. I will be running this bash script every hour through cron, so it needs to be unattended. However, when I run sftp/scp it requires a password.

在SFTP帐户​​不允许SSH连接,所以我不能创建授权密钥。我不想依赖于远程计算机上的文件夹的.ssh以及

The sftp account does not allow ssh connections so I cannot create authorized keys. I don't want to rely on the .ssh folder on the remote machine as well.

任何建议将是AP preciated。

Any suggestions would be appreciated.

请我不能安装任何东西,所以没有钥匙扣,sshpass或期望。在<找到的所有其他的答案href=\"http://stackoverflow.com/questions/5386482/how-to-run-the-sftp-command-with-a-password-from-bash-script\">How与来自bash脚本密码运行SFTP命令?都不如我不能在服务器上安装任何可行的。

Keep in mind I cannot install anything, so no keychain, sshpass or expect. All other answers found in How to run the sftp command with a password from Bash script? are not feasible as I cannot install anything on the server.

推荐答案

最初我是想,因为PHP的创建,我需要上传的文件使用PHP的ssh2_connect()。这是更好在我的PHP脚本的原因,但因为它是不工作有这个SFTP交易,我提出抨击。

Initially I was trying to use php's ssh2_connect() because php creates the file that I need to upload. It's better to have this sftp transaction in my php script for that reason but since it wasn't working, I moved on to bash.

我的解决方案实际上是使用PHP和卷曲:

My solution is actually using php and curl:

$ch = curl_init();

$fp = fopen($file, "r");

curl_setopt($ch, CURLOPT_URL, "sftp://USER:PASS@HOST/" . basename($file));
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));

curl_exec($ch);

curl_close($ch);

这篇关于使用bash SFTP / SCP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆