16

I want to use a shell script to pull a file from another server. I have written a script where i can do this but i want the script to work non-interactively so that it doesn't prompt me for the password and works automatically if i hard-code the same and let the script have what it needs.

I have done this before for FTP however, in out new servers the FTP connections are blocked and we can use only SCP or SFTP. My earlier script was something like this which used to work with FTP and did the job well without asking for the password:

#!/bin/bash -vx
ftp -in 100.XXX.XXX.XX<<END_SCRIPT
quote USER username
quote PASS password
bin
prompt off
cd /directory/in/remote/host
lcd /directory/in/local/machine
mget *
bye
END_SCRIPT

I tried the similar for SFTP in a script and it works for Interactive mode but like FTP i am not able to use the non-interactive options for SFTP. Following is my code to fetch particular files from the remote machine:

#!/bin/bash -vx

path="/tmp/testanks"

sftp username@100.XXX.XX.XXX <<EOT
cd $path
get Bharti*
quit
EOT

How can i change the above code in a way that i could provide this script the username and password to make it non-interactive.

I understand that providing a hard-coded password is a security concern but right now i am concerned more about fetching the files immediately. I would really appreciate if i could get the secured ways of doing it as well along with the answer i desire.

2 Answers2

10

With curl:

curl sftp://example.com/file.zip -u user:password 

Note that for that public-key Authentication is preferred to using a password:

curl sftp://example.com/file.zip -u user

This will attempt to use ~/.ssh/id_rsa as a private key. You can specify another one with --key.

Pere
  • 253
7

the solution is to use scp, you add local's public key to the end of remote authorized_keys file.

then

 scp username@100.X.X.X:Bharthi/* $path

on local host, look in .ssh dir, if you don't have any .pub, do the following first (and only one time)

ssh-keygen (1)
scp-copy-id username@100.X.X.X (2)
  1. accept all default, DO NOT enter a passwd when prompt
  2. enter password for username @ 100.X.X.X
Archemar
  • 31,554
  • +1 as a side note scp has issues on multiple big files or huge number of small files, in which case you could use rsync, however you'll need rsync on both machines where scp only needs to be on machine running the script. – gwillie Jul 31 '15 at 08:11
  • 2
    It seems like the server can block scp and enforces sftp. I tried to use scp and got this message: This service allows sftp connections only.. I remember, that this may be due to a chrooted environment which only works with sftp, not scp. (To be confirmed). – Michael Härtl Feb 04 '17 at 11:05
  • 1
    My server won't let me use scp. is there a solution for using only the sftp client? – waspinator Aug 24 '17 at 17:18
  • 2
    SFTP does not require shell login, scp and rsync do. Keep this in mind. – Christophe De Troyer Jan 30 '18 at 08:41
  • If you're SFTP host has disabled non-interactive access (rendering scp useless), you can get around this by sending the commands from standard input as answered in another question here. – alexkb Oct 31 '19 at 04:02