I will make scp be on cronjob so I would rather use to auto enter the password of the other server.

sshpass -p 'your_password' scp user@abc.com:/usr/etc/Output/*.txt /usr/abc/

This script didn't work for me, I am using Solaris.

Please also indicate an explanation on the answers. Thank you.


I used

#!/usr/local/bin/expect -f

spawn bash -c "scp /apps/DASHBOARD/xx/xxx/xxxx/TEST1.*.txt dash@MYSERV:/apps/DASHBOARD/xx/xxx1/"
expect "*Password:*"
send "pass123\r"

spawn bash -c "scp /apps/DASHBOARD/xx/xxx/xxxx/SAMP1.*.txt* dash@MYSERV:/apps/DASHBOARD/xx/samp1/"
expect "*Password:*"
send "pass123\r"

And I put this on my CRONJOB. This script works for me if I ran this manually but when I set this on my CRONJOB, it doesn't proceed on the second scp.

Please help.

  • 2
    Is there a reason you can't use a keypair? – Philip Kendall Aug 27 '19 at 6:00
  • How to use that? Can I also use the expect function? – Ace Aug 27 '19 at 7:30
  • Hello, please help me. – Ace Aug 29 '19 at 2:58
  • If you have a new question/problem, post it separately. – Martin Prikryl Aug 29 '19 at 5:23
  • Even if it's connected on my yesterday's question? – Ace Aug 29 '19 at 7:17

Use public key authentication connection in order to avoid keeping/maintain hardcopy passwords. Copy the content of local users content of id_rsa.pub to the remote users ~/.ssh/authorized_keys file in order to establish public key authentication connection.

If still want to use password then expect script could be made, like the following and change the expect script 2nd line to match your user and server and MY_PASSWORD with your password:

spawn scp "user@abc.com:/usr/etc/Output/*.txt" /usr/abc/
expect "user@abc.com\'s password:" 
send "MY_PASSWORD\r"

Thanks to @pynexj at post Link to StackOverFlow i had to modify the script as the following:

#!/usr/bin/expect -f

spawn scp /TEST1.txt root@
expect "*assword:*"
send "password\r"
expect eof

spawn scp /TEST2.txt root@
expect "*assword:*"
send "password\r"
expect eof

Note that if you have multiple files or patterns that need to be transferred you can also consider to use SFTP with batch mode and expect.

  • 1
    This works for me, I just put the location of expect where it has been installed and boom. Thanks @GiannakopoulosJ – Ace Aug 28 '19 at 8:46
  • 1
    kindly help me on my additional question please. Thanks – Ace Aug 29 '19 at 2:36

If you are allowed to keep a secret on the hard disk, you can use keys in home .ssh folder to skip password authentication. That's the way non-interactive, machine to machine, authentication, is supposed to be.

  • help me. Thanks – Ace Aug 29 '19 at 4:29

Before attempting that you need to place quotations around the use of regular expressions in scp, so most likely that would be your point of failure.

sshpass -p 'your_password' scp "user@abc.com:/usr/etc/Output/*.txt" /usr/abc/
  • 1
    Yes, but I think the main problem is there's no sshpass function installed in Solaris. How can I install or just call the path of that function? – Ace Aug 27 '19 at 6:08
  • You can find informatino on how to install packages on Solaris here: ibm.com/support/knowledgecenter/en/SS6MER_9.5.0/… For you it'll be pkg install sshpass. – ndookie Aug 27 '19 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.