Use "wait" to send password automatically

I am trying to copy a file from my remote server to a local one. Here, my script runs it, using "wait" to send the password automatically

scp user@host:/folder/myFile ./
expect "Password: "
send "myPassword"

When I run this, it still asks for a "Password", what's wrong?

+3
source share
4 answers

From what you are trying to do, it looks like you might be better off using ssh with public and private key. To get started, you can google for the ssh-keygen tutorial.

+1
source

As long as I agree with Sam and Nick, the answer to your questions is that you did not press the enter button:

send "mypassword\r"
+10

This wait script does the job (thanks to 'zedwood' )

#!/usr/bin/expect -f
set filename [lindex $argv 0]
set timeout -1
spawn scp $filename myusername@192.168.1.123:/home/myusername/
set pass "mypassword"
expect {
        password: {send "$pass\r" ; exp_continue}
        eof exit
}
+5
source

please do not even leave such scripts around that will be picked up by someone else and used elsewhere .... try public key authentication , it is very easy to configure.

+4
source

Source: https://habr.com/ru/post/1711282/


All Articles