1

I'm trying to use scp, there is no key for the box I need to communicate w/. I tried this code and many variations of it from my searches online, for some reason it doesn't get entered in. If you can help me it would be great. Thanks in advance. BTW, I'm executing the bash script from testing on a mac.

#!/bin/bash

#$1- source file
#$2- destination
#$3- password

/usr/bin/expect << EOF
spawn scp -rp $1  $2
set pass $3
expect -re "password:"
send "password\r" 
expect "\r"
send "\r\n"
EOF

result is ;

spawn scp -rp file1 user@x.com:/path/file2
me@whoknows.com's password: 
return value was : 0
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
vmaxer
  • 31
  • 1
  • 4

1 Answers1

1

You have to wait for the end of file (eof) of the scp command

#!/bin/bash

#$1- source file
#$2- destination
#$3- password

/usr/bin/expect << EOF
spawn scp -rp $1  $2
set pass $3; # Instead, you can also directly use the '$3' for password
puts "pwd = \$pass"
expect -re "password:"
send "\$pass\r" 
expect eof
EOF

Output :

dinesh@dinesh-VirtualBox:~/stackoverflow$ ./vmaxer srcfile dinesh@xxx.xxx.xx.xxx:/home/dinesh welcome!2E
spawn scp -rp srcfile dsivaji@xxx.xxx.xx.xxx:/home/dinesh
pwd = welcome!2E
dinesh@xxx.xxx.xx.xxx's password: 
srcfile                                                        100%  281     0.3KB/s   00:00    
dinesh@dinesh-VirtualBox:~/stackoverflow$ 

Note : If your file is too big and takes more time than Expect's default timeout (which is 10 seconds), you should increase the timeout value.

It can be achieved as,

set timeout 60; # This will make the timeout value to '1 min'
Dinesh
  • 16,014
  • 23
  • 80
  • 122