cancel
Showing results for 
Search instead for 
Did you mean: 

SFTP in a script

Jeff Rowe
Occasional Visitor

SFTP in a script

Hello, My company is switching from standard FTP to secure FTP. I had a shell script that used the -n switch in FTP to have the user account information be entered via the script. SFPT does not have this option. I tried using ID:Password@server for the connection, but still I am prompted for the password. Is there any way to do this with SFTP? I know that scripting a password isn't the pinnacle of security, but this is a low risk function.
1 REPLY
James R. Ferguson
Acclaimed Contributor

Re: SFTP in a script

Hi Jeff:

You can setup public keys. This thread has a very good discussion of how to setup public keys in ssh:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1254688

The 'known_hosts' file contains the public host keys for all known hosts. The per-user version is maintained automatically. Thus, whenever a user connects from an unknown host, its key is added to the per-user file if you choose to acknowledge the fingerprint. If you don't, the connection can still be made, but you will be prompted the next time as if you had never connected.

See the manpages for 'sshd' and 'ssh' for more information.

As for the script, once you have public keys in place:

# cat ./mysftp
set -u
typeset HOST=$1
typeset FILE=$2
typeset RC
sftp -v -b - <lcd /tmp
cd /tmp
get -P ${FILE} ${FILE}.local
EOF
RC=$?
echo "\nSFTP returned ${RC}"
exit ${RC}

...run as ./mysftp hostname filename

Regards!

...JRF...