January 6, 2009

scp with automatic password input

I know I should use certificate to automate this, but sometimes we do need to do scp with a password and it would be nice to automate it. Here Document does work. We need "expect" utility to do it. Here is an example:

#!/usr/bin/expect -f

# trick to pass in command-line args to spawn
#eval spawn scp $argv
eval spawn scp hcw root@10.0.0.1:.

expect "password: $"
send "yourpassword\n"

# wait for regular shell prompt before quitting
# probably a better way using 'wait'
expect "$ $"

2 comments:

  1. Great solution I needed!!! Thanx, and keep up the good work :-)

    Regards,
    Marcel.

    ReplyDelete
  2. Thanks for the script.

    but i am using the below script
    #!/usr/bin/expect -f

    # trick to pass in command-line args to spawn
    #eval spawn scp $argv
    eval spawn scp /home/bkpadm/* root@server1:/backup/agent

    expect "root@server1's password: $"
    send "password\n"

    # wait for regular shell prompt before quitting
    # probably a better way using 'wait'
    expect "$ $"

    In the script i am using * to move all the files but while executing the script it showing me the file doesnot exist error.

    My files are in the name format of below `date "+%d-%m-%y"`.tar.gz

    hence i need to move the files on daily basis thats why i am using * to move all the files.

    Thanks & Regards,
    Nareen T

    ReplyDelete