简体   繁体   中英

sftp/scp files with bash

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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