Expect script + how to perform case in expect script

expectkshshell-scriptingsolaristcl

the following expect script target is to print the hostname name from linux/solaris machine
and according to hostname results expect script will run linux.ksh or solaris.ksh script

expect_sun_script=`cat << EOF
set timeout -1
spawn   telnet 0 $IP_ADDRESS


expect login:           {send $LOGIN\r}
expect Password:        {send "PASS\r"}

expect -re {#|>}        {send "/bin/hostname\r"}

so – after I get the hostname name I need to run linux.ksh for hostname – machine1

or if I get hostname – machine2 then I need to run the script – solaris.ksh

but how expect can run the script according to the hostname name?

according to my example below:

expect actually wait for machine1 – but it could be machine2
so its stuck ….

my target is to run linux.ksh or solaris.ksh according to the relevant hostname

please advice ?

expect "machine1"            { send "/var/tmp/linux.ksh\r "  } 
expect "machine2"            { send "/var/tmp/solaris.ksh\r" }

.
.
.
.
.
.

Best Answer

I think you can do something like

expect "machine1" {send "/var/tmp/linux.ksh\r" } \
"machine2"  { send /var/tmp/solaris.ksh\r" } \
.
.
.

Having said that it may be easier to use uname which will return Linux and SunOS respectively and should make the management of the script much easier as hosts come and go.

expect -re {#|>}        {send "PATH=/bin:/usr/bin:$PATH uname\r"}

expect "Linux" { send /var/tmp/linux.ksh\r" } \ 
expect SunOS { send /var/tmp/solaris.ksh\r" }

Note that the uname command is in /bin for Linux and /usr/bin for Solaris so we set the PATH before the command to ensure that it is correctly found on either system.