Tuesday, July 15, 2008

SCP without interactive password input

If you want to make your scripts working with interactive programs without your participation use expect tool. Here is an expect script for downloading file from remote host to local directory via scp

1 #!/usr/bin/expect -f
2
3 set timeout 1
4
5 set username [lindex $argv 0]
6 set password [lindex $argv 1]
7 set host_filename [lindex $argv 2]
8 set new_dir [lindex $argv 3]
9
10 eval spawn scp $username@$host_filename $new_dir
11
12 expect "password: $"
13 send "$password\n"
14
15 expect "$ $"

It is very useful application while supporting many hosts, nobody really wants to make the same thing for all the hundreds machines. Let your script do that.

No comments: