Tuesday, September 20, 2011

Expect - automate your tasks

Expect is an automation tool which you can use to automatically put text into console, based on the expected output from application (telnet, ftp, ssh, scp...).
This is an example which you can use to type password e.g. for your scp copying.

#!/bin/bash

password="MyPasswd"

expect -c "
spawn scp /path/to/file krisko@somehost:~/copy/here/

expect {
    "yes/no" {
        send "yes\\r"
            expect {
                "Password*" {
                    send $password\r
                    interact
                }
            }
    }
    "Password*" {
        send $password\r
        interact
    }
}
"

If the command will ask you to add the key to known_hosts the auto-answer will be yes and then the password will be filled in, otherwise only the password will be filled.
There are some disadvantages, like password stored in plain-text. Despite of this you can use expect to automate some of your tasks.

No comments:

Post a Comment