1

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.

Community
  • 1
  • 1
Scott Joudry
  • 883
  • 10
  • 25
  • 1
    @anubhava this is not a duplicate because the post you have provided does not answer my question – Scott Joudry Jun 27 '14 at 17:12
  • Actually it does. Please spend some time reading answers. It allows you to run sftp/scp from shell *unattended* using various ways suggested. – anubhava Jun 27 '14 at 17:14
  • 1
    @anubhava I also mentioned in my post that the answers from that post are not feasible for my situation since I can't use keys or install anything on the server. – Scott Joudry Jun 27 '14 at 17:15
  • 2
    Lack of shell access does not necessarily mean you can't set up keys. If you can write to `~/.ssh/authorized_keys`, then you can generate a keypair on another system, prepare an appropriate `authorized_keys` file, and upload it to the server with sftp - as long as the server is configured to allow key authentication. – nobody Jun 27 '14 at 17:17
  • @ScottJoudry: There are several ways suggested in the linked Q&A. Not all of them would require you to install some software. – anubhava Jun 27 '14 at 17:18
  • I have no control over the destination sftp account so creating a folder on that account and requiring it for this to work is also not something I want to do. – Scott Joudry Jun 27 '14 at 17:19
  • @anubhava, I scanned that post and all of the answers are using something that my server does not have. – Scott Joudry Jun 27 '14 at 17:19
  • 1
    ok I reopened the question but it is unlikely you will get some easy answers here. – anubhava Jun 27 '14 at 17:24
  • 1
    Thanks, never hurts to try – Scott Joudry Jun 27 '14 at 17:25

2 Answers2

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);
Scott Joudry
  • 883
  • 10
  • 25
0

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.

lihao
  • 583
  • 3
  • 6
  • lftp is not installed on the server. See previous comment in post – Scott Joudry Jun 27 '14 at 17:56
  • @ScottJoudry I might be wrong about this, but I'm pretty sure that `lftp` is a client side program and you just need an SFTP server running on the server end of things. – Piccolo Jun 27 '14 at 17:58
  • I am uploading files from one server to another and the source server does not have lftp installed. – Scott Joudry Jun 27 '14 at 18:00