Monday, February 20, 2006

Auto Login - Expect/Autoexpect

I have a Sify Broadband connection at home for which I have to Login everytime
I want to connect to internet.

The Process for connecting was like,

Below I am running the command manually:

#sifyconnect -l

Welcome to Sify BroadBand Service

username :sriramsreedhar
password :

Account Balance : 233.24
Product Code : AXAU
Expiry Date : 2006-03-19 19:30:23
Last Login : 2006-02-20 17:42:13

Mon Feb 20 20:01:38 2006

[root@mybox Download]#

All I wanted to do was run a script like say connectsify.sh which should autologin.

Heres what I did,

I have download expect-8.4.11-1mdk.i586.rpm from rpmfind.net

[root@mybox Download]# rpm -ivh expect-8.4.11-1mdk.i586.rpm
Preparing... ########################################### [100%]
1:expect ########################################### [100%]

The below command autoexpect will create a file called connect_to_sify.exp

[root@mybox Download]# autoexpect -f connect_to_sify.exp /usr/bin/sifyconnect -l
autoexpect started, file is connect_to_sify.exp
Welcome to Sify BroadBand Service

username :sriramsreedhar
password :

Account Balance : 233.24
Product Code : AXAU
Expiry Date : 2006-03-19 19:30:23
Last Login : 2006-02-20 17:42:13

Mon Feb 20 20:01:38 2006

[root@mybox Download]#

This is what connect_to_sify.exp generated :

[root@mybox Download]# cat connect_to_sify.exp
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Mon Feb 20 20:00:57 2006
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script. It
# necessarily has to guess about certain things. Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts. If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character. This
# pacifies every program I know of. The -c flag makes the script do
# this in the first place. The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

#
# 2) differing output - Some programs produce different output each time
# they run. The "date" command is an obvious example. Another is
# ftp, if it produces throughput statistics at the end of a file
# transfer. If this causes a problem, delete these patterns or replace
# them with wildcards. An alternative is to use the -p flag (for
# "prompt") which makes Expect only look for the last line of output
# (i.e., the prompt). The -P flag allows you to define a character to
# toggle this mode off and on.
#
# Read the man page for more info.
#
# -Don


set timeout -1
spawn /usr/bin/sifyconnect -l
match_max 100000
expect -exact "[32mWelcome to [0m[32mSify [0m[32mBroadBand Service[0m\r
\r
username :"
send -- "sriramsreedhar\r"
expect -exact "sriramsreedhar\r
password :"
send -- "password\r"
expect eof

[root@mybox Download]#

[root@mybox Download]# mv connect_to_sify.exp /usr/bin/connectsify.sh

Now I run the below command to connect,

[root@mybox Download]# connectsify.sh
spawn /usr/bin/sifyconnect -l
Welcome to Sify BroadBand Service

username :sriram564
password :

Login Success
Welcome sriram564,


Account Balance : 233.24
Product Code : AXAU
Expiry Date : 2006-03-19 19:30:23
Last Login : 2006-02-20 20:36:35

Mon Feb 20 20:43:58 2006


I will no more require to give my username and password for connecting to sify.