Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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" }

share|improve this question
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.