Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have script where I put scp command to backup file to other server. But when it runs it asking to type password. Is there way to automate this? I am using CentOS 5.6. I have seen few guides they use spawn command when I use it, its saying -bash: spawn: command not found.

#!/usr/bin/expect -f

# connect via scp
spawn scp "user@example.com:/home/santhosh/file.dmp" /u01/dumps/file.dmp
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "PASSWORD\r"
}
}
interact
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Make sure you have 'Expect' installed. In Linux distributions with 'yum', run (as root): # yum install expect Then make sure the path to expect is correct in your script by typing: $ which expect /usr/bin/expect The path returned is to be put in the beginning of the script at #!/usr/bin/expect -f

share|improve this answer
    
it was already installed (expect-5.43.0-5.1.i386) and path was correct yet still not working, maybe i call script incorrectly it i do "bash <script name>"? –  Hujeplet Dec 9 '11 at 19:49
    
Works when i use chmod +x file.sh and run as ./file.sh –  Hujeplet Dec 9 '11 at 19:55
    
Cool then, yeah #! is a general notation to execute as script under another interpreter/program and it needs the file to be executable unless the file is directly passed to the program as an argument to execute the instructions in the file. –  Nikhil Mulley Dec 9 '11 at 19:57
    
One more question can i use timestamp in filename somehow e.g. "date"; –  Hujeplet Dec 9 '11 at 20:41
    
yes, its possible. declare a variable, assign the date value but in the string format to it and then use the stringified date variable. –  Nikhil Mulley Dec 9 '11 at 20:48
show 1 more comment

Use key based authentication with empty passphrase. This will not ask for password.

Follow these steps.

share|improve this answer
    
cant use key authentication because server i scp to is shared and they dont let me install it –  Hujeplet Dec 9 '11 at 19:42
    
Alright, so you do not have other option than using expect. –  Sachin Divekar Dec 9 '11 at 19:47
    
yes, but i found answer already thanks though –  Hujeplet Dec 9 '11 at 19:57
3  
You don't need to install anything on the destination server - just copy over a file (...using scp itself). Use the ssh-copy-id command to copy the key file over - it simply creates a file ~/.ssh/authorized_keys with your public key. Everything should work automatically from there. –  lutzky Dec 10 '11 at 9:33
add comment

If spawn is not working, you need to check your expect installation. Sending password can be automated like this...

spawn scp "user@example.com:/home/santhosh/file.dmp" /u01/dumps/file.dmp

expect {
"assword:" {
    send "MyPassword\r"
    # you can send remote commands here..
}
"yes/no)?" {
    send "yes\r"
    sleep 2
    exp_continue
}
timeout {
    puts "\nError: timed out.\n"
    exit
}
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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