Shell script function

3

I have a VM and need to run a script remotely in another VM, for example:

Script is in VM X and must be executed by X in VM Y.

#!/bin/bash
IP=$1

if [ $# -ne 1 ]; then
echo "informe o servidor $1: "
exit

fi

for machine in $1; do

    ssh -l root -o ConnectTimeout=2 $machine ifconfig

done

This command runs correctly and that's what I need, but I need to run a function with a business rule.

Looks like this:

    for machine in $1; do

    ssh -l root -o ConnectTimeout=2 $machine funcao1

done 


function funcao1{

mkdir teste

cd teste
}

Does anyone know of a similar solution? Or similar?

Vlw !!!!

    
asked by anonymous 08.02.2016 / 17:05

1 answer

2

You can solve this by doing two scripts. The first is responsible for traversing the list of machines and running ssh. The second contains the code of funcao1 .

Shell script 1 (script1.sh):

for machine in $1; do
  ssh -l root -o ConnectTimeout=2 $machine 'bash -s' < script2.sh "teste" "parametro"
done 

Shell script 2 (script2.sh):

mkdir teste
cd teste
echo $1 $2

Note that ssh is done with the 'bash -s' option. This option allows a script to be executed remotely on the machine for which ssh is done. The script does not need to be locally on the remote machine. Therefore, scritp1.sh and script2.sh must be both on the master machine (the machine that does ssh).

    
11.02.2016 / 14:34