Sunday, April 29, 2007

Expect

Today I was writing an installer to install a few scripts that will aid build automation and uploading of reports generated from Clear Case to web. I was faced with this particular problem.

Problem: I wanted a remote site to generate a secret key and upload to my machine. I did not want to write a readme describing how to do that since I was sure that the guy on the other side will come back to me asking me to explain it.

This is what I did:

I wrote two expect scripts and wrote a shell script to call them with required arguments. The first script generated the key & the second uploaded it to my server.

---- Script to generate an RSA key. ----
---- Call only if the keys are not present ----
spawn ssh-keygen -t rsa
expect {
-re "Generating*" {exp_continue}
-re "Enter*" {send "\r";exp_continue}
timeout {puts TimeErr}
}
Then I wrote another script to put it where I wanted

--------------------Scp script --------------------------------
set USER [lindex $argv 0]
set RHOST [lindex $argv 1]
set RPASS [lindex $argv 2]
set DESTFILE [lindex $argv 3]
set HOME [lindex $argv 4]
spawn scp $HOME/.ssh/id_rsa.pub \ $USER@$RHOST:$DESTFILE
expect {
-re "^The" {send "yes\r"}
-re "^$USER" {send "$RPASS\r"; exp_continue;}
timeout {puts TimeErr}
}
spawn ssh $USER@$RHOST "if \[ ! -f\ ~/.ssh/authorized_keys \]; then mkdir -p ~/.ssh ; touch \ ~/.ssh/authorized_keys; fi; cat ~/$DESTFILE >>\ ~/.ssh/authorized_keys"
expect {
-re "^The" {send "yes\r"}
-re "^$USER" {send "$RPASS\r"; exp_continue;}
timeout {puts TimeErr}
}

This was my first attempt with expect. Needless to say, I really enjoyed doing this and now I want to learn expect better.

Saturday, April 28, 2007

whoami


I am Suseelan B Sarin, a software engineer from Bangalore, India. For a long time I wanted to start a Linux blog. I don't claim to be a Linux scholar. But I simply like to use Linux. I re-invented this truth very recently when I saw that I could, with more or less minimal effort, setup ClearCase 7.0 server & client on Fc-5 for my office project while our system admin team still struggles with it even after 2 weeks. (I would have helped them. But they pissed me off in the initial stages & now I have completely deserted them so that they can have fun with with their own woes)

Yesterday I wrote a script to upload build logs from CC to a local web server and I really enjoyed it. I wrote it in bash & I plan to put some time into it and convert it into a PERL script. Now, why I am I posting it here? Its simply because these incidents have brought me back to Linux. In past four years, my only acquaintance with Linux was maintaining a forum and discussion board for my office. Now I plan to take one more dip in that vast ocean. Keep watching this space to find if I will drown or will come back with some precious pearls
:)