2

This is the first time I am writing a shell script. I tried to do as much research as I can to avoid dumb/repetitive question. Please excuse if its repeat/dumb question.

I have a shell script which connects to remote linux machine and runs scripts there. I am using 'expect' to spawn a ssh connection and to issue commands to trigger the job. However, I am having issues while closing the connection after completing the job.

This is my script:

set prompt "(%|#|\\$|%\]) $"


    expect -c 'spawn ssh $UN@$STAGE ;
                    expect password ; send "$PASS \n";
                    expect -regexp "$PROMPT"; send "./settings.$UN.sh > settings_log.txt \n";
                    interact'

This script successfully runs the script file for me ($UN and $STAGE parameters are input to the script. I omitted that here for simplicity). However, this leaves me with an open connection. I tried to close the connection after running the script by using following instead of above

expect -c 'spawn ssh   $UN@$STAGE ;
                expect password ; send "$PASS \n";
                expect -regexp "$PROMPT"; send "./settings.$UN.sh > settings_log.txt \n";
                expect -regexp "$PROMPT"; send "exit \n"'

This does close the connection but I noticed that my script file did not run at all. Also the settings_log.txt is not generated at all.

Does this mean, that exit command is aborting the process before its completion? I tried using 'sleep' before exit but it did not help. Is there a better suggested way to terminate the connection when using expect? Any help is appreciated.

2
  • Can you explain to me what expect -regexp "$PROMPT" does? I'm not familiar with expect. Commented Feb 2, 2011 at 19:52
  • This is the first time with expect. expect -regexp "$PROMPT" waits for the shell to complete its work, which and return for user input. For example I start a ssh connection using . expect -c 'spawn ssh $UN@$STAGE ; Now,I know that ssh will ask for password.Since I know this upfront,I can tell my script to be prepared for it and send the password when this occurs.This is achieved by following statement. expect password ; send "$PASS \n" You can also give regular expression to expect.That is what I tried to do.I defined a variable ($PROMPT) at the top and used it in next statement Commented Feb 2, 2011 at 22:54

3 Answers 3

2

with expect, you terminate your send commands with \r not \n, so

 expect -c 'spawn ssh   $UN@$STAGE
            expect password
            send "$PASS\r"
            expect -regexp "$PROMPT"
            send "./settings.$UN.sh > settings_log.txt\r"
            expect -regexp "$PROMPT"
            send "exit\r"
            expect eof'
Sign up to request clarification or add additional context in comments.

Comments

1

Note you can execute remote shell commands and copy files using ssh and scp, directly, without using expect.

For example,

scp ./settings.$UN.sh $UN@$STAGE:settings_log.txt
ssh $UN@$STAGE whatever-you-need-to-execute

The connection will close as soon as soon as whatever-you-need-to-execute completes.

3 Comments

I tried doing this. But ssh $UN@$STAGE asks me for a password. I need this script to work without user intervention. Say, if I can create HTML form for user where he enters his UN and PWD, I want my system to fire this script. I cannot do it ssh prompts me for PWD. So, I am using except.
You can either identify yourself with a certificate, use expect to provide the password to ssh, or use sshpass to do it for you. Note the first option is by far the most secure, and also works for scp (as does the second, but which is less secure).
Ok great. That sounds like a better way to do it. Thanks for the help
0

Your outer script seems to be written in csh and sets a variable named "prompt", but your expect script is using a variable called "PROMPT". Try making the two variable names match case.

1 Comment

the case mismatch was a typo by me. in my actual scrip both are in same case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.