From: Joe Emenaker on
I've come across a problem that's making my head hurt....

A couple of years ago, I wrote two scripts that I used to copy my
public keys in my local $HOME/.ssh to the authorized_hosts/
authorized_hosts2 files on any remote machine (which would allow me to
login to the remote machine without needing a password anymore). The
second script ran on the remote host and actually imported the keys
into the authorized_hosts* files. The first script used scp to copy
the second script and public keys over and then used a ssh call to run
the script on the remote host.

Because the first script used scp and then ssh, it required that the
user enter their password twice. That bothered me. What also bothered
me was that there were two scripts.

I've just fixed the first problem by skipping the scp and just feeding
the second script directly to a single ssh (of course, I have to run
the script through some sed commands to inject the public keys
directly into the script on its way to the remote host), as in:

cat remotescript | sed "s!%%RSAKEYGOESHERE%%!$RSAKEY!" | ssh
$remotehost

*Now*... the problem I want to solve is that I don't want the remote
script to be in a separate file. I'd like to have it in-lined inside
of the first script, so that I can:

echo "$REMOTESCRIPT" | sed "s!.....

but getting the script into a variable is proving to be a chore. I've
tried a variety of "TOHERE"-style of redirection, like:
REMOTESCRIPT="<< ENDOFSCRIPT
or
REMOTESCRIPT=`cat << ENDOFSCRIPT

but they all seem to fail pretty miserably. I'm pretty sure that I
could:
CAT > $tempfile << ENDOFSCRIPT
...
ENDOFSCRIPT
REMOTESCRIPT=`cat $tempfile`
rm "$tempfile"

but using a tempfile isn't elegant. I'm thinking that there's *got* to
be a way to do it elegantly. Also, the second script has a fair number
of single-quotes, double-quotes, back-quotes, and $VARIABLES, so I'm
looking for something that requires a minimum of \escaping.

I know that I *could* uuencode the second script, but I'd like to be
able to edit the second script within the first script... instead of
editing it in a separate file and then "compiling" it, of sorts, with
uuencode and placing it into the first script.

I figure that *somebody* has solved this problem in the past. What's
the trick to it?

From: kruhft on
> but they all seem to fail pretty miserably. I'm pretty sure that I
> could:
> CAT > $tempfile << ENDOFSCRIPT
> ...
> ENDOFSCRIPT
> REMOTESCRIPT=`cat $tempfile`
> rm "$tempfile"

Close, but you should quote your ENDOFSCRIPT marker, like so:

cat <<'ENDOFSCRIPT' | sed "s!%%RSAKEYGOESHERE%%!$RSAKEY!" | ssh
$remotehost
# enter your script here
ENDOFSCRIPT

The quotes around the here document end marker cause the contents of
the here document to be quoted, passing the original directly to the
ssh process.

--
kruhft

From: Joe Emenaker on
On Feb 17, 4:42 am, "kruhft" <kru...(a)gmail.com> wrote:
>
> Close, but you should quote your ENDOFSCRIPT marker, like so:
>
> cat <<'ENDOFSCRIPT' | sed "s!%%RSAKEYGOESHERE%%!$RSAKEY!" | ssh
> $remotehost
> # enter your script here
> ENDOFSCRIPT

Wow! That did it! Thanks a BUNCH!

One more question. For aesthetic reasons, I'd like to avoid placing
the remote script in the middle of the local one. So, I'd like to
avoid
# local script
cat <<'ENDOFSCRIPT' | ssh $remotehost
# remote script
ENDOFSCRIPT
# more local script

I tried assigning to a variable at the beginning of the local script:
####
# Remote Script Start
####
REMOTESCRIPT=`cat <<'ENDOFSCRIPT'
# remote script
ENDOFSCRIPT`
####
# Remote Script End
####
...
echo $REMOTESCRIPT | ssh $remotehost

but the back-quotes in the remote script got interpreted by the first
script and everything failed.

I was able to achieve what I wanted by making a subroutine:
send_remote_script() {
cat << 'ENDOFSCRIPT'
... remote script stuff ...
ENDOFSCRIPT
}
# ... local script...
send_remote_script | ssh $remotehost

So, that achieved the clean separation between the local and remote
scripts that I was after, so now I'm only asking out of follow-up
curiosity: Is there a way to use the <<'TOHERE' notation to assign
directly to an env. variable?

From: Barry Margolin on
In article <1171768959.160141.78200(a)k78g2000cwa.googlegroups.com>,
"Joe Emenaker" <joe.emenaker(a)gmail.com> wrote:

> On Feb 17, 4:42 am, "kruhft" <kru...(a)gmail.com> wrote:
> >
> > Close, but you should quote your ENDOFSCRIPT marker, like so:
> >
> > cat <<'ENDOFSCRIPT' | sed "s!%%RSAKEYGOESHERE%%!$RSAKEY!" | ssh
> > $remotehost
> > # enter your script here
> > ENDOFSCRIPT
>
> Wow! That did it! Thanks a BUNCH!
>
> One more question. For aesthetic reasons, I'd like to avoid placing
> the remote script in the middle of the local one. So, I'd like to
> avoid
> # local script
> cat <<'ENDOFSCRIPT' | ssh $remotehost
> # remote script
> ENDOFSCRIPT
> # more local script
>
> I tried assigning to a variable at the beginning of the local script:
> ####
> # Remote Script Start
> ####
> REMOTESCRIPT=`cat <<'ENDOFSCRIPT'
> # remote script
> ENDOFSCRIPT`
> ####
> # Remote Script End
> ####
> ...
> echo $REMOTESCRIPT | ssh $remotehost
>
> but the back-quotes in the remote script got interpreted by the first
> script and everything failed.

Change it to:

echo "$REMOTESCRIPT" | ssh $remotehost

or maybe

ssh $remotehost "$REMOTESCRIPT"

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Dan Mercer on

"Barry Margolin" <barmar(a)alum.mit.edu> wrote in message news:barmar-4CE957.22281917022007(a)comcast.dca.giganews.com...
: In article <1171768959.160141.78200(a)k78g2000cwa.googlegroups.com>,
: "Joe Emenaker" <joe.emenaker(a)gmail.com> wrote:
:
: > On Feb 17, 4:42 am, "kruhft" <kru...(a)gmail.com> wrote:
: > >
: > > Close, but you should quote your ENDOFSCRIPT marker, like so:
: > >
: > > cat <<'ENDOFSCRIPT' | sed "s!%%RSAKEYGOESHERE%%!$RSAKEY!" | ssh
: > > $remotehost
: > > # enter your script here
: > > ENDOFSCRIPT
: >
: > Wow! That did it! Thanks a BUNCH!
: >
: > One more question. For aesthetic reasons, I'd like to avoid placing
: > the remote script in the middle of the local one. So, I'd like to
: > avoid
: > # local script
: > cat <<'ENDOFSCRIPT' | ssh $remotehost
: > # remote script
: > ENDOFSCRIPT
: > # more local script
: >
: > I tried assigning to a variable at the beginning of the local script:
: > ####
: > # Remote Script Start
: > ####
: > REMOTESCRIPT=`cat <<'ENDOFSCRIPT'
: > # remote script
: > ENDOFSCRIPT`
: > ####
: > # Remote Script End
: > ####
: > ...
: > echo $REMOTESCRIPT | ssh $remotehost
: >
: > but the back-quotes in the remote script got interpreted by the first
: > script and everything failed.
:
: Change it to:
:
: echo "$REMOTESCRIPT" | ssh $remotehost
:
: or maybe
:
: ssh $remotehost "$REMOTESCRIPT"

or

exec 3<<'ENDOFSCRIPT'
...
ENDOFSCRIPT

...


ssh $remotehost <&3

Dan Mercer

:
: --
: Barry Margolin, barmar(a)alum.mit.edu
: Arlington, MA
: *** PLEASE post questions in newsgroups, not directly to me ***
: *** PLEASE don't copy me on replies, I'll read them in the group ***