sftp/scp files with bash

社会主义新天地 提交于 2019-12-11 05:55:19

问题


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

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.

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.

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.

Any suggestions would be appreciated.

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.


回答1:


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.

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);



回答2:


check lftp, it's very powerful tool in file transfer. for example:

lftp -u "$username","$password" -e "cd /desc/path; put $FILE; bye" sftp://remote.example.com

check also mput, mirror in lftp manual.

BTW. I don't think you need to install anything on the remote server if you use expect. anyway, I am using lftp instead of expect in most similar situations at work.



来源:https://stackoverflow.com/questions/24457328/sftp-scp-files-with-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!