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

2 Answers 2

3

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 {[#>] ?$}
1
  • what agreat solution thx you very much for your great help -:)
    – yael
    Sep 5, 2012 at 14:15
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!

2
  • yes but how to implemented the expect solution with Regular expression ( not by if then ..) , for example with expect -re ..... or something like that . .. . .
    – yael
    Sep 5, 2012 at 10:59
  • expect -re {[$#] } should match both.
    – Andy H
    Sep 5, 2012 at 11:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .