How to make the script automated to take password on its own?

109,207

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-keygen on your client.
  • add the id_rsa.pub to ~/.ssh/authorized_keys on 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.

Share:
109,207

Related videos on Youtube

Author by

PriB

Updated on September 18, 2022

Comments

  • PriB 4 months

    I have a script that is doing sftp some files from one server to another, like inside the script script.ksh I have the below lines;

    sftp [email protected]_address << EOF
    cd path
    put file1
    bye
    EOF
    

    When I am running the script like;

    ./script.ksh
    

    It 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 Password prompt will occur, then it should take the password on its own. Can anyone have any suggestion please.

    • Admin
      Admin over 7 years
      sshpass is also an option
    • Admin
      Admin over 7 years
      @Naitree please can you let me know where and how to use this sshpass command in my script?
    • Admin
      Admin over 7 years
      If security is not a concern, just prepend sshpass -p password before your sftp command. That should do it.
    • Admin
      Admin over 7 years
      @Naitree Thanks for your help, but I checked in my unix box sshpass is not installed, I also don't have a privilege to install it. Do you have any other idea other than sshpass? Thanks
    • Admin
      Admin over 7 years
      Sorry, you have to install something or use ssh key.
  • PriB over 7 years
    Is 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
    Sobrique over 7 years
    Ssh key pairs are better for prod, because they are more secure. You should talk to your sysadmi.
  • Sobrique
    Sobrique over 7 years
    If you don't have permission to do password less auth, what makes you think that bypassing that is a good idea?
  • ajlowndes over 6 years
    I just noticed this was a question from a year ago...
  • roaima
    roaima over 5 years
    That looks suspiciously like FTP.
  • roaima
    roaima over 5 years
    How is this different to an answer already given?
  • SDsolar
    SDsolar over 5 years
    The 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.