sftp script to get a file from remote machine
Solution 1
SFTP is basically SSH + SFTP on the server side so what you need is some kind of non-interactive authentication such as private and public keys. Depending on OS you are using, you can generate a key using ssh-keygen
and copy a public key to the server using ssh-copy-id
command.
Solution 2
The best simple way:
sshpass -p 'password123' scp -- [email protected]:/dir/file.txt /local/dir
sshpass
must be work and for sftp
too... Don't forget check permissions for script with writed password.
UPD: you need to install sshpass by command (for debian-based distro):
apt-get install sshpass
Solution 3
Another way to automate this process is to use expect script. Here is an example:
for example:
#!/usr/bin/expect -f
spawn sftp [email protected]:/dir/file.txt
expect "[email protected]'s password :"
send "mypassword123\r"
send "exit\r"
You can get more info on expect from here or just google it
Solution 4
You should use key-based authentication so you don't need a password in the first place.
Here's a detailed howto about how to do it: http://www.debian-administration.org/article/530/SSH_with_authentication_key_instead_of_password
Related videos on Youtube

user83570
Updated on September 18, 2022Comments
-
user83570 3 months
I use a script, which contains the line:
sftp [email protected]:/dir/file.txt
When I run the script, it asks for my password:
[email protected]'s password:
However, I don't want to be prompted to type my password. I want my script automate the password entry and download the files
Guys, I don't want to do it. All I want to catch that prompt inside the script and put password. Is it possible?
sshpass belong to bash?
this is not working at all
-
Admin almost 8 yearsSometimes we don't have the option of setting up keys with 3rd party FTP sites. If you don't know, or you can't do it, you should just say so.
-
-
tricasse almost 8 yearsThis can be insecure, as it will show your password to anyone able to have a look at the processes running on your machine. Key-based authentication or at least an expect script are better.
-
Tejaswini Utlapalli over 6 yearsProvided that the administrator of the machine allows subscribers to use key-based authentication.