How to make the script automated to take password on its own?
Solution 1
Try this:
#!/usr/bin/expect
spawn sftp [email protected]_address:/path/to/folder
expect "Password:"
send "PASSWORD\n"
expect "sftp>"
send "put file1\n"
expect "sftp>"
send "bye\n"
or if you don't want to dedicate the whole script to expect:
#!/bin/sh
expect << 'EOS'
spawn sftp [email protected]_address:/path/to/folder
expect "Password:"
send "PASSWORD\n"
expect "sftp>"
send "put file1\n"
expect "sftp>"
send "bye\n"
EOS
Solution 2
By design ssh doesn't allow 'embedding' of passwords - that's because it has a mechanism for non-interactive auth using public-private key pairs.
So I would suggest you consider that as your first port of call. Usually it's as simple as:
- run
ssh-keygenon your client. - add the
id_rsa.pubto~/.ssh/authorized_keyson your server.
If that's not an option for some reason, then the fallback option is expect which allows you to do send a password to ssh.
Related videos on Youtube
PriB
Updated on September 18, 2022Comments
-
PriB 4 months
I have a script that is doing
sftpsome files from one server to another, like inside the scriptscript.kshI have the below lines;sftp [email protected]_address << EOF cd path put file1 bye EOFWhen I am running the script like;
./script.kshIt is asking for password like below;
Password:Here I have to pass the password manually as this is not a password less connection.
I want to make this script automated, that when the
Passwordprompt will occur, then it should take the password on its own. Can anyone have any suggestion please.-
Admin over 7 yearssshpass is also an option -
Admin over 7 years@Naitree please can you let me know where and how to use thissshpasscommand in my script? -
Admin over 7 yearsIf security is not a concern, just prependsshpass -p passwordbefore yoursftpcommand. That should do it. -
Admin over 7 years@Naitree Thanks for your help, but I checked in my unix boxsshpassis not installed, I also don't have a privilege to install it. Do you have any other idea other thansshpass? Thanks -
Admin over 7 yearsSorry, you have to install something or use ssh key.
-
-
PriB over 7 yearsIs there any other method? because I can't make the connection password less. I don't have the permission to do that. I want that, while connecting it should ask password, and then automatically the script will take the password to login in.
-
Sobrique over 7 yearsSsh key pairs are better for prod, because they are more secure. You should talk to your sysadmi. -
Sobrique over 7 yearsIf you don't have permission to do password less auth, what makes you think that bypassing that is a good idea? -
ajlowndes over 6 yearsI just noticed this was a question from a year ago...
-
roaima over 5 yearsThat looks suspiciously like FTP. -
roaima over 5 yearsHow is this different to an answer already given? -
SDsolar over 5 yearsThe only answer close is @sobrique but the ssh-copy-id -i command was omitted. It requires doing more research to find out how to add the public key. My answer just plain shows the two commands that need to be run. Short and sweet, and it works. No need for expect.