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!!!!
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.
– tharley carvalho
I’ll edit the content.
– tharley carvalho
@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.
– cantoni
Thanks @Cantoni. It is possible to receive the value of $1 (machine) in the other script2.sh file where the code will be ?
– tharley carvalho
Pass the variable name instead of a string: ssh -l root -o Connecttimeout=2 $machine 'bash -s' < script2.sh $1 $machine
– cantoni
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 .
– tharley carvalho
To be more exact, I can only pass parameters like $1, variable itself could not in any way.
– tharley carvalho
@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.
– cantoni