Operating System - HP-UX
1834915 Members
2442 Online
110071 Solutions
New Discussion

Re: Running embedded script on remote host

 
Simon Hargrave
Honored Contributor

Running embedded script on remote host

I can run any script as any user on any host with a simple script as follows: -

# runscript.sh
#
host=$1
user=$2
cat somescript.sh | rexec $host -l root "su - $user -c \"sh\""

i can then run runscript.sh with hostname and user as parameters to run that script anywhere.

i want to fold this into one script (ie remove the need for external somescript.sh). something like: -

# runscript.sh
#
host=$1
user=$2
rexec $host -l root "su - $user -c \"sh\"" <{stuff that somescript.sh used to do}
ls
bdf
bla bla
awk '' etc
EOF

However this only works if I extensively escape all the stuff between <
Is there a way to do this tidily? thinking either of a way to make the shell temporarily not process quoting, substitution etc so the <
3 REPLIES 3
Muthukumar_5
Honored Contributor

Re: Running embedded script on remote host

Hai,

We have to give the commands with in "" only. So we have to delimit special chars ",etc to delimit it for action.

Else we have to use remote script file to execute with any problem or, local script file too.

We can some more logic's as,

cat | remsh $host -l $user "cat -|sh"

Else as,

mkdir /usr/hosts
ln -s /usr/bin/remsh /usr/hosts/

/usr/hosts/ "command1;command2;command3"

More it will not show the /etc/copyright messages out there and error messages when you use as like,
rexec $host -l root "su - $user -c \"sh\"" <ls
bdf
EOF

Regards
Muthu
Easy to suggest when don't know about the problem!
Simon Hargrave
Honored Contributor

Re: Running embedded script on remote host

hmm, really wanted to avoid escaping everything. hoped there may be some clever way of having an inline "tar" or "uuencode" or something, but guess not.

interesting tip about the $0 use of remsh though!
Simon Hargrave
Honored Contributor

Re: Running embedded script on remote host

Got a solution (in case anyone ever wants to do similar): -

#!/usr/bin/sh

HOST=$1
USER=$2
F=`grep -n "^#MARKER" $0 | cut -d: -f1`
C=`cat $0 | wc -l`
(( L = C - F ))
tail -$L $0 | rexec $HOST -l root "su - $USER -c \"sh\""
exit 0

#MARKER
rest
of
script
here

Basically it calculates how long the file is after the "#MARKER" comment, then tails itself from here and pipes through the rexec. An exit 0 after the rexec ensures it never runs on local host.

Simple but effective.