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

$VAR
set timeout -1 

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

expect "Password:*"

send -- "argos001\r\n"

send -- "\r"


expect eof

Open in new window

LVL 1
celticianAsked:
Who is Participating?

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

x
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.

woolmilkporcCommented:
Your version will not work, obviously.

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

#!/bin/bash
VAR=$(/path/to/myexpect)

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.
0

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
simon3270Commented:
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?
0
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.

thanks!
0
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.