Shell script function

Asked

Viewed 550 times

3

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

Script is in VM X and I need to run 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 performs correctly and that’s what I need, but I need to execute a function with a business rule.

Similar to this:

    for machine in $1; do

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

done 


function funcao1{

mkdir teste

cd teste
}

Can anyone tell me a similar solution ? Or similar ?

Vlw!!!!

1 answer

2


You can solve this by making two scripts. The first one is responsible for going through the machine list 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 the ssh is done with the option 'bash -s'. This option allows a script to be executed remotely on the machine for which the ssh is done. The script does not need to be locally on the remote host. Therefore, scritp1.sh and script2.sh should both be in the master machine (the ssh making machine).

  • thank you for the reply. Unfortunately it does not suit me, in the previous example I did not put the code that I will actually use in the function but I will use input parameters including the $1 parameter. In the example you passed I cannot use the initial input parameters in the second script2.sh file.

  • I’ll edit the content.

  • @tharleycarvalho, so it’s important to ask a very complete question, with all the relevant details. Here’s the tip. About your problem, I updated the answer with the possibility to pass parameters. Please check.

  • Thanks @Cantoni. It is possible to receive the value of $1 (machine) in the other script2.sh file where the code will be ?

  • Pass the variable name instead of a string: ssh -l root -o Connecttimeout=2 $machine 'bash -s' < script2.sh $1 $machine

  • still here ? Ask me a question, why can’t I pass a string parameter variable ? Example: sshpass -p $PASSWDFTP ssh -l $USERNAME -o Connecttimeout=2 $machine 'bash -s' < teste.sh $USERNAME $HOSTNAME $PASSWD; These variables are not transferred to the test.sh, but if I put $1 $2 $3 they are transferred the values. My problem is: I can’t assign values to these variables .

  • To be more exact, I can only pass parameters like $1, variable itself could not in any way.

  • @tharleycarvalho, if I understand your question correctly, are you trying to reference (within script2.sh) the name of the variables you pass in script1.sh? If you’re trying, within script2.sh, to use $USERNAME, it really won’t work. Within script2.sh, you have $1, $2, $n (where n is the number of parameters). What you can do, therefore, is within script2.sh declare a variable that is called $USERNAME and assign the value of $1, only to be clearer to those who are reading the scritp2.sh.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.