Getting result from remote script into variable

I need to get the result of a remote ksh into a varible, i will use that variable later for being mailed (but thats another issue).

I must note that the result of the remote script takes like 15-30 seconds (it executes a query) to be done.

This is the script, the result is a string of only 20-30 chars.

#!/usr/bin/expect -f

set timeout -1 

spawn ssh -tt user2@machine2 data/ >>$VAR

expect "Password:*"

send -- "argos001\r\n"

send -- "\r"

expect eof

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Your version will not work, obviously.

Best embed the expect script (I'll call it "myexpect") in a bash/ksh script like this:


You can use "grep -v" to filter out unwanted parts of the result:

VAR=$(/path/to/myexpect | grep -v "Password")

# Please note: When working with $VAR later you should enclose it in double quotes to preserve possible line breaks:

echo "$VAR"

Replace "#!/bin/bash" with "#!/bin/ksh" if you don't have bash installed,
or use "#!/usr/bin/ksh" or "#!/usr/bin/bash" if your shell(s) reside(s) under /usr/bin.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Couple of comments on the expect.
- You don't need wildcards at the beginning or end of pattern matches in expect - if the pattern is "Password:*", expect will complete its match on just the "Password:" part - the "*" is not used.  It is commonly assumed that the "*" is needed to match any characters after "Password:" to complete the line, but this isn't how expect works.
- You just need "\r"  at the end of "send" actions in UNIX, not "\r\n".  In contrast, use "\n" at the end of "send_user" actions (where you are giving the user information).
- set timeout -1 is dangerous - you will hang forever if, for example, the password is wrong.  You can set it to, for example, 300 if your script will take no mor than 5 minutes to run.
- The "send -- "\r" will just send a newline immediately after it has sent the password - is this intentional?  Should it be waiting for another prompt?
celticianAuthor Commented:
It shouldn't be waiting for anothoer prompt, i dont know how but when i set \r\n it worked and it didn't previously, probably i made some other changes too.

I have set timeout to 400, its a query and sometimes it takes a while to respond.

now im able to storage the value of the result in a varialbe and mail it to me.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.