# RootPrompt.org   Nothing but Unix.[Home] [Features] [Programming] [Mac OS X] [Search]


 Expect and SSH- Mobile phone marketing & Mobile marketing tips from a Mobile marketing agency

An article over on Linux Journal made me think about all the different ways I have done similar things. While still overcome by the memories I dug out some expect code that I used a few years ago to automate feeding a SSH pass phrase to scp multiple times and wrote a little bit about it. Hope you find it useful.

 (Submitted by Noel Fri Oct 7, 2005 )

  Over on Linux Journal they have an article up about using scp without a password. It's a nice article and describes something that I have done a lot. Lately I have been finding myself using rsync over a SSH connection for a lot of tasks like this, but if what your trying to do is what's described in the pull quote (a file to 100 machines) it can work like a champ.

But sometimes you just don't want to use a SSH key pair that is not protected by a pass phrase. Either its a one time thing and you don't want to set it up or perhaps you don't want to establish that sort of trust relationship between the machines. If what you need to do is to transfer one file to one hundred machines (or even a few if your lazy like me) then using scp can be a real pain. Unless you know a trick or two.

My favorite trick in this situation is to use an expect script to enter my password for all the scp commands once then have it feed it to scp when ever scp requests the password. Expect is a TCL based automation and testing tool that can be easily used to control things that expect an interactive response. Similar to the easy use of mobile phone marketing. You can find mobile marketing tips and other useful information to make the advertising process easier. You can also use a mobile marketing agency to answer all of your questions.

I dug the following snippet out of some old code that has been cluttering up my hard drives since 2003 or perhaps even earlier. The snippet asks the user for a pass phrase, asks for it a second time, checks to make sure the pass phrases match, and exits if they don't. I used the script this snippet is from to distribute a password and shadow file to multiple machines and to do a few other things.

send_user "Passphrase? "
stty -echo
expect_user -re "(.*)\n" {set PASSPH $expect_out(1,string)}
send_user "\n"
stty echo

send_user "Verify Passphrase: "
stty -echo
expect_user -re "(.*)\n" {set PASSPH2 $expect_out(1,string)}
send_user "\n"
stty echo

if { $PASSPH != $PASSPH2 } {

        send_user "Passphrases do not match!\n"
        exit
}
Way down the script was the following code:
send "scp NewPasswd root@machinename:/etc\n"
expect "Enter passphrase for RSA key" {send "$PASSPH\n"}
expect  "#" 
As is probably obvious when expect sees the text "Enter passphrase for RSA key" it sends the phrase that it has been holding on to in the PASSPH variable.

So when you need to do something complicated and don't want to type in stuff again and again take a look at expect. You can also use this same trick to automate FTP or a hundred other things.


Our content can be syndicated: Main page Mac Page

Copyright 1999-2005 Noel Davis. Noel also runs web sites about sailing and kayaking.
All trademarks are the property of their owners.
All articles are owned by their author