0

I wrote a shell script and ı will set it into crontab but ı have a problem. When crontab run my script, running scp command inside script and get some informations from different server so ı have to enter password of other server inside my script. How to enter password inside script ?

my script:

scp username@host:/Products/data/bridge/control.txt .

????? ( have to automatically enter password)

b=$(more control.txt | wc -l)

if [$b = 1]; then

echo " OK "

fi 
mathB
  • 634
  • 8
  • 21
getaffe
  • 41
  • 1
  • 7
  • 1
    You can use `sshpass` to complete this – mathB Oct 16 '17 at 11:12
  • `sshpass -p "password" scp username@host:/Products/data/bridge/control.txt .` to exact. – PoVa Oct 16 '17 at 11:14
  • Try sshpass. And your system must connect to remote server before so you have ~/.ssh/known_hosts inside your system. However this method is not suggested because you have to leave your password unprotected into crontab or filesystem. I'll suggest to use ssh-keygen to make a key, and put ~/.ssh/id_rsa.pub to remote ~/.ssh/authorized_keys . Then your system will not require to provide a password during scp. – HZS Oct 16 '17 at 11:19
  • actually this is not our server, we can connect this server with limited authorization so sshpass command not working and they dont install this cmd. Also ı tried ssh key method but it is not working ı think firewall prevent to ssh with key. I just want to send a txt file from a server to other. – getaffe Oct 16 '17 at 11:44
  • does this - https://stackoverflow.com/questions/1346509/automate-scp-file-transfer-using-a-shell-script help? – mathB Oct 16 '17 at 11:52
  • Any option to set up [trust](https://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/) with a public key instead? – Paul Hodges Oct 16 '17 at 13:44
  • thanks your answers. İt has been solved. – getaffe Oct 25 '17 at 07:19

1 Answers1

0

You could use expect, for example:

my_password="123456"
expect -c "spawn scp username@host:/Products/data/bridge/control.txt .; expect \"password\" {send -- \"${my_password}\r\"; expect eof;};"
Buyduck
  • 11
  • 2