1

I've been trying to get a PHP script to SFTP upload a file, but failed. I tried asking my question on stackoverflow.com but haven't received a working solution:

https://stackoverflow.com/questions/1767117/how-to-sftp-upload-files-from-php

So now I want to try a different approach. Is there a way to write a shell script that can issue several commands to sftp upload a file to another server? If I were to issue commands one by one from terminal, it would look like:

>sftp [email protected] >password: mypassword >put file.csv 

I tried putting these three lines into a ./automaticupload file. When I executed the file, but the terminal shows me:

[email protected]'s password: 

even though I typed in the password line in automaticupload.

I am unable to create a passwordless ssh/scp/sftp account. The remote server.com does not give me ssh or scp access.

What should I do?

3 Answers 3

3

If you want to use sftp from a script you need to setup passwordless SSH keys with ssh-keygen(1) or use sshpass

My vote would nearly always be for the former, but it can depend on the situation which is more appropiate.

4
  • I don't have privileges to set up either of those options. The remote server belongs to a company that deals with financial transactions, so they're limiting my access to only sftp uploads. Commented Nov 19, 2009 at 23:54
  • and forcing me to supply username and password each time Commented Nov 19, 2009 at 23:55
  • so you've tried uploading your public key in the appropriate place with the correct permissions and it ignored them? Commented Nov 20, 2009 at 1:37
  • yeah, it says permission denied when i try to enter the .ssh directory Commented Nov 25, 2009 at 6:14
1

Install and use expect, it's designed for exactly this kind of situation.

Use the autoexpect script to generate an expect script from a recorded live session. Depending on the distribution, autoexpect is either included in the main expect package, or (strangely) in expect-dev on Ubuntu.

1
  • does expect stores the password encrypted? Commented Apr 20, 2017 at 11:20
1

Use socat: http://www.dest-unreach.org/socat/

For example:

(sleep 2; echo password) | socat - EXEC: "sftp -o batchmode=no -o PubkeyAuthentication=no -b batchfile user@host",pty,setsid,ctty

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.