Need solution for my bash script executed localy for ssh remote machine

Dear members,

I'm having some difficulties to find a solution to my problem. I have passed over 2 days in internet to find something, but nothing is working. Maybe it's simply not possible, but before giving up I would like to ask here. Thanks in advance,
The aim of this script is to ask to an admin on the local machine the IP address of the remote machine that will be configured, stock the IP in a variable and then automatically connect via ssh and execute the script that will change the hostname of the remote machine. I know that the way I m trying to do cannot work because the variable are not are not send over ssh.
Do you have idea about how I can do this ? I insist that I really want to do it in that way, because I know there is some more easier way to do but I want to solve it in the form for my own knowleadge purpose.

Sorry for my approxative english.

#!/bin/bash
# Script administration poste assosiation linux
# DĂ©finition du compte root Ă  utiliser lors de la connexion SSH
root='administrateur'
# DĂ©but script
PS3='Bonjour que voulez vous faire ?: '
options=("Configurer un poste""Reinitialiser un poste""Quitter")
select opt in"${options[@]}"
do
    case $optin
"Configurer un poste")
echo"Veuillez entrer l'adresse Ip du poste nous allons configurer son hostname et créer un compte utilisateur"
            read ip
ssh$root@$ip 
#On stock le troisieme octet dans la variable ipoctet3
            ipoctet3=$(echo $ip | awk -F '.' '{print $3}')
            ipoctet4=$(echo $ip | awk -F '.' '{print $4}')
echo$ipoctet3
echo$ipoctet4
if [ $ipoctet3-eq100 ] ; then
echo"Réseau Baobab détecté configuration en cours..."
echo"Le nom hostname sera PCbao"$ipoctet4
sed-i'1i PCbao'$ipoctet4'/etc/hostname
              sed -i '1d' /etc/hostname
              cat /etc/hostname
             fi
            ;;
        "Reinitialiser un poste")
            echo "Veuillez entrer l'adresse Ip du poste à réintialiser"
            ;;
        "Quitter")
            break
            ;;
        *) echo "mauvais choix $REPLY";;
    esac
done

You seem to have a problem with spaces in your listing yielding a code full of errors.
If all those corrected, use a "here document" for the remote commands. Like

ssh root@$ip <<EOF

#On stock le troisieme octet dans la variable ipoctet3

IFS=. read X X ipoctet3 ipoctet4 <<< $ip

echo \$ipoctet3
echo \$ipoctet4

if [ \$ipoctet3 -eq 100 ]
  then  echo "Réseau Baobab détecté configuration en cours..."
        echo "Le nom hostname sera PCbao"\$ipoctet4
        sed "1s/.*/PCbao\$ipoctet4/" hostname
fi
EOF

Be aware of the escaped $ - chars for the variable expansion - without those, expansion would be done by the local shell, not the one on the remote node.

2 Likes

Thanks a lot for your reply see bellow normally the script doesn't looks like this I don't know why when I posted on this page the spaces disappeared.

I'm sorry I'm not that good in scripting still learning.

Can you explain me why you have added the variable with "\" ( \$) , is it the way to explain to the terminal that those variables needs to be executed on the remote shell ?

Can you clarify this part plz ?

Many thanks :slight_smile:

Yes.
You had answered your own question before you were asking :stuck_out_tongue:

man bash is your friend:

You are in a dilemma here: you want the IP variable expanded locally, but the other variables expanded remotely. For the first, you can't have the "word" quoted, but it must be unquoted, so, for the second, you need to apply the escapes for the $ signs.

Thanks you guys with your help my script is working well now.