Fun with “expect”

» Posted by on Mar 27, 2012 in Blog, Coding, Systems Administration, Technology thoughts, Utilities | 0 comments

Automate everything!

PERL makes great golems.

A few years ago, I had call to automate some file transfers from one system to another. However, for security purposes, FTP was not an option. (It probably shouldn’t ever be on production systems–throw up a VM for that so when A Bad Thing happens, you can easily reconstitute that point of entry).It was at that point that I discovered the handy Tcl-based *nix utility expect.

expect is nothing more than a simple call/response program, but oh, the places you can go. For instance, here’s a simple script that grabs backup files from one server and transfers them to the backup_archives directory on the local server. This example uses password authentication, but if you use keys, just add the -i /path/to/key.pem line in the scp spawn command.


#!/usr/bin/expect -f
# catch the date passed on the command line and assign it to a variable
set thedate [lindex $argv 0]
# connect to remote server
spawn scp "user@server:/backups/*$thedate*" /backups_archives
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "your_passwordr\r"
}
}
interact

Read more about expect here: http://linux.die.net/man/1/expect

Submit a Comment

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>