Operating System - HP-UX
1830229 Members
2543 Online
109999 Solutions
New Discussion

executing a script in remote servers

 
istartedin2009
Occasional Advisor

executing a script in remote servers

hi dears,
i am working on a script tat will provide all the LVM data of a HP UX server, it's called LVM_Inventory.sh, and now i am working on a script that will execute LVM_Inventory.sh in remote servers,
so first i have to check if the script existe and then execute it, afteer i will add how to download the result file, if it does not exist the script will copy LVM_Inventory.sh in the remote server an then run it

i tried to fix the script all the day and i just don't know how to do it, so here is my script and i will be happy of your feedbacks
#! /bin/ksh

############################################

### this script needs a list of servers names
### each line is a server name
############################################

liste=$1

DIR_SCRIPT_LVM_INVENTORY="/home/edsadm/scripts/"
NAME_SCRIPT_LVM_INVENTORY="LVM_Inventory.sh"
SCRIPT_LVM_INVENTORY="$DIR_SCRIPT_LVM_INVENTORY$NAME_SCRIPT_LVM_INVENTORY"



for nomServeur in $(cat "$liste")
do

#RET=`remsh $nomServeur -l root 'test -f $SCRIPT_LVM_INVENTORY ; echo $?'`

if [ $RET -eq 0 ]
then
echo "then $nomServeur"
remsh $nomServeur $SCRIPT_LVM_INVENTORY $nomServeur
else
echo "else $nomServeur"
# on copie le fichier sur le serveur
host=`hostname`
remsh "$nomServeur" -l root "mkdir -p /home/edsadm/scripts 2>/dev/null"
rcp -p "root@$host:$SCRIPT_LVM_INVENTORY" "root@$nomServeur:$SCRIPT_LVM_INVENTORY"
remsh $nomServeur $SCRIPT_LVM_INVENTORY $nomServeur
fi

done


the test of existance of the file does not give the true value, it gives the non existence of the file even if i just copy it my self.
6 REPLIES 6
Steven Schweda
Honored Contributor

Re: executing a script in remote servers

The apostrophes in:
'test -f $SCRIPT_LVM_INVENTORY ; echo $?'
mean that it's looking for a file named:
$SCRIPT_LVM_INVENTORY
not:
/home/edsadm/scripts/LVM_Inventory.sh
.

Try quotation marks:
"test -f $SCRIPT_LVM_INVENTORY ; echo $?"

You know, like the other remsh command.
James R. Ferguson
Acclaimed Contributor

Re: executing a script in remote servers

Hi:

First, I assume that you didn't mean to comment out the establishment of the 'RET' variable with:

#RET=`remsh $nomServeur -l root 'test -f $SCRIPT_LVM_INVENTORY ; echo $?'`

Then, instead of :

RET=`remsh $nomServeur -l root 'test -f $SCRIPT_LVM_INVENTORY ; echo $?'`

I would do:

RET=$(ssh $nonServeur -n -l root [ -f ${FILE} ] && echo 0)

Notice that I didn't quote the command argument allowing the shell to interpret the metacharacters on the _local_ host first.

Lastly, notice that I have used 'ssh' instead of the less-secure 'remsh'. The syntax is virtually identical and I find it more reliable.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: executing a script in remote servers

>for nomServeur in $(cat "$liste")

If you are going to use $(), you should also remove cat. And probably no need to use "" around $liste, unless you have spaces:
for nomServeur in $(< $liste); do
istartedin2009
Occasional Advisor

Re: executing a script in remote servers

hi dears,
let me thank all of you for your answers.

Steven, i try you proposition but it doesn't work with double quotes "" also,
James R. i can't use ssh so i used remsh without -n (as you said that's the same), but it did not work.
Dennis thank you for you help also.

so before making the boucle and so on, i write another script to find the solution of the probleme and then i will write it in my first script, here is the new script with the results.
i test in two servers, frls does not have the script on it so i must have 1 in the return of the test, and frlg which has the script and must have 0 in the return of the test.

#! /bin/ksh


DIR_SCRIPT_LVM_INVENTORY="/home/edsadm/scripts/"
NAME_SCRIPT_LVM_INVENTORY="LVM_Inventory.sh"
SCRIPT_LVM_INVENTORY="$DIR_SCRIPT_LVM_INVENTORY$NAME_SCRIPT_LVM_INVENTORY"

r=$(remsh frls -l root 'test -f /home/edsadm/scripts/LVM_Inventory.sh ; echo $?')
s=$(remsh frlg -l root 'test -f /home/edsadm/scripts/LVM_Inventory.sh ; echo $?')

RET=1
RET=$(remsh frls -l root [ -f ${SCRIPT_LVM_INVENTORY} ] && echo 0)
ET=$(remsh frlg -l root [ -f ${SCRIPT_LVM_INVENTORY} ] && echo 0)

rp=$(remsh frls -l root "test -f $SCRIPT_LVM_INVENTORY ; echo $?")
sp=$(remsh frlg -l root "test -f $SCRIPT_LVM_INVENTORY ; echo $?")

echo "Forum example"
echo "$RET $ET"

echo "correct"
echo "$r $s"

echo "with parameter"
echo "$rp $sp"


and here is the result
frd7:/root/home/root (root) ./LVM_Payasage_Inventory.sh
Forum example
0 0
correct
1 0
with parameter
0 0
James R. Ferguson
Acclaimed Contributor

Re: executing a script in remote servers

Hi (again):

> James R. i can't use ssh so i used remsh without -n (as you said that's the same), but it did not work.

The '-n' works for 'ssh' or 'remsh'. It is generally wise to include it: see the manpages for an explanation.

For the snippet of code I suggested, 'remsh' does not consistently work while 'ssh' does.

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: executing a script in remote servers

> Steven, i try you proposition but it
> doesn't work with double quotes "" also,

Some of it does. You want
$file_name
inside quotation marks, because you want
_this_ variable evaluated on the _local_
host, where the variable is defined, but you
need to have
$?
inside apostrophes, because you don't want
_it_ evaluated here, you want _it_ evaluated
on the _remote_ host, where it gets the
status value from the remote "test" command.
For example:

dy # echo $file_yes
/root/.profile

dy # echo $file_no
/root/.profileXX

dy # remsh dy -l root "test -f $file_yes ; "'echo $?'
0
dy # remsh dy -l root "test -f $file_no ; "'echo $?'
1

And if your file name includes problem
characters, then you need to work a little
harder:

dy # echo $file_odd
a b c

dy # ls -l 'a b c'
-rw-r--r-- 1 root sys 5 Sep 30 10:42 a b c

dy # remsh dy -l root 'test -f "'"$file_yes"'" ; echo $?'
0

dy # remsh dy -l root 'test -f "'"$file_no"'" ; echo $?'
1

dy # remsh dy -l root 'test -f "'"$file_odd"'" ; echo $?'
0

You need to think about which shell where
sees which quotation marks.

Everything's complicated.