Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

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

I create expect script so when expect see the prompt "#" then he run sshd restart

but the problem is that I run this expect also on solars and the prompt there is ">" so how to create one expect line that support "#" and ">" prompt

on linux

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


   expect >                {send "/etc/init.d/sshd restart\r"}
share|improve this question
up vote 3 down vote accepted

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 {[#>] ?$}
share|improve this answer
what agreat solution thx you very much for your great help -:) – yael Sep 5 '12 at 14:15

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"}
   expect >  {send "/etc/init.d/sshd restart\r"}

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

share|improve this answer
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 '12 at 10:59
expect -re {[$#] } should match both. – Andy H Sep 5 '12 at 11:13

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.