6

I am trying to copy a file from my remote server to my local. Here's my script to run it, by using 'expect' to automaticlally send in password

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

When I run this, it still prompts for "Password", what is wrong?

Saobi
  • 16,121
  • 29
  • 71
  • 81

4 Answers4

13

This expect 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
}
Eden
  • 1,110
  • 1
  • 12
  • 18
  • 1
    The first working script I find on stackoverflow! Thank you for sharing! I always stumble on advices that I should not use expect in this way... Actually I was developing a program which I should tested on a router whose sshd server doesn't support public key authentification Orz. Using expect is my only salvation. – Allan Ruin Sep 12 '16 at 08:44
11

While I agree with Sam and nik, the answer to you questions is that you didn't "hit enter":

send "mypassword\r"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
4

Please do not even leave such scripts around that might be picked up by someone else and used elsewhere… Try public key authentication instead, it is are very easy to setup.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
nik
  • 13,254
  • 3
  • 41
  • 57
-5

From what you are trying to do it sounds like you might be better off using ssh with a public and private key. You could google for ssh-keygen tutorial to get started.

Sam
  • 14,642
  • 6
  • 27
  • 39