• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

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/remote_script.sh >>$VAR

expect "Password:*"

send -- "argos001\r\n"

send -- "\r"

expect eof

Open in new window

2 Solutions
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.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now