3

I want to run sshd restart on Linux and Solaris machines via expect. ( my expect runs in my ksh script )

I created an expect script. When expect sees a prompt "#" it runs sshd restart.

I also run the same script on Solaris with a prompt ">". How to create one expect line that supports both prompts?

on Linux

expect #                {send "/etc/init.d/sshd restart\r"}

on Solaris

expect >                {send "/etc/init.d/sshd restart\r"}
yael
  • 2,563

2 Answers2

4

Use a glob-pattern: expect {[#>]}

or a regexp: expect -re {#|>} -- the regexp pattern can get more elaborate. I recommend you anchor prompt matching to the end of the line. Often prompts end with a space, so you could:

expect -re {[#>] ?$}
0

You could put this in an if statement by checking the output of uname -s, or by checking the output of:

cat /etc/release

cat /etc/redhat-release

cat /etc/lsb-release

And use something like:

if [[ $(uname -s) == "Linux" ]];then
   expect #  {send "/etc/init.d/sshd restart\r"}
else
   expect >  {send "/etc/init.d/sshd restart\r"}
fi

I have not used ksh for some years so sorry if syntax is wrong!

Andy H
  • 392
  • 1
  • 4