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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need solution for my bash script executed localy for ssh remote machine
# 1  
Old 04-21-2020
Need solution for my bash script executed localy for ssh remote machine [SOLVE]

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.





Code:
#!/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


Last edited by Azura1003; 04-23-2020 at 10:50 AM..
# 2  
Old 04-21-2020
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


Code:
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.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-22-2020
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 ?



Quote:
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.
Can you clarify this part plz ?



Many thanks Smilie

Last edited by Azura1003; 04-22-2020 at 12:40 PM..
# 4  
Old 04-22-2020
Yes.
You had answered your own question before you were asking Smilie

Last edited by sea; 04-22-2020 at 01:35 PM.. Reason: time grammer, hopefuly
# 5  
Old 04-22-2020
man bash is your friend:
Quote:
Here Documents
This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks)
is seen. All of the lines read up to that point are then used as the standard input (or file descriptor n if n is specified) for a command.

The format of here-documents is:

[n]<<[-]word
here-document
delimiter

No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.


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.
# 6  
Old 04-23-2020
Thanks you guys with your help my script is working well now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh bash script exits without remote command completion

Hi, My goal is to connect from unix server A to windows server B and call a bat file on windows. I am able to succeed in remoting to windows and executing a command, the issue i am facing is the shell scrip is exiting without making sure of bat file success. Can you please help me in... (4 Replies)
Discussion started by: pxp018
4 Replies

2. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

3. Linux

Executing a script in remote machine through ssh

How to execute a script in remote machine through ssh I have a script test.sh which does some backup activity in remote machine. Wanted to keep backup also in remote machine. ssh -l username <remote machine> "commands to be exceuted as ; separted" but how to put the script in the place of... (5 Replies)
Discussion started by: sanvel
5 Replies

4. Red Hat

iptables applied in local machine, can't ssh remote machine after chain changed to DROP

I want to SSH to 192.168.1.15 Server from my machine, my ip was 192.168.1.99 Source Destination was UP, with IP 192.168.1.15. This is LAN Network there are 30 Machine's Connected to the network and working fine, I'm Playing around the local machine's because I need to apply the same rules in... (2 Replies)
Discussion started by: babinlonston
2 Replies

5. Shell Programming and Scripting

Mount fails (because remote machine is off) bash script stops

Hi, I'm trying to automate a couple of mounts. (I would do this in fstab, but the auto feature causes the virtual machine I'm running in to freeze when it boots up) If the machine I'm trying to connect to is OFF, I get the host unreachable error and then the bash script stops. The problem is... (3 Replies)
Discussion started by: jdilts
3 Replies

6. Shell Programming and Scripting

bash script to execute a command remote servers using ssh

Hello, I am running into few issues, please suggest me what I am missing. I am running this script on a linux host. Main idea of this script is to, login to each host via ssh and get uid of user, service user that I trying to run this script, has already deployed ssh keys and provide sudo... (8 Replies)
Discussion started by: bobby320
8 Replies

7. UNIX for Dummies Questions & Answers

how to use ssh to run shell script on a remote machine?

how to use ssh to run shell script on a remote machine? ssh user@remote sh ./script.unx i ran the above command ./script.unx HAS NOHUP COMMAND IN ITS BODY, I AM GETTING ERROR AS NOHUP NOT FOUND... i tried to run that script from remote server, its working fine do ineed to set... (6 Replies)
Discussion started by: only4satish
6 Replies

8. Shell Programming and Scripting

Cannot create variables via ssh on remote machine

Hello to all Background info: Local machine : Linux, /bin/bash Remote machine (for the user used for ssh) : SunOs, /bin/ksh (so we have different OS, different Shells) My problem : From the local host i execute $ var=bla $ result=$(ssh -q user@remote-machine " > echo \"this is... (12 Replies)
Discussion started by: black_fender
12 Replies

9. Shell Programming and Scripting

executing command in a remote machine through ssh - shell script

Hi All, i have two machines like x and y . my requirement is i should connect to machine Y from x through ssh connection . and do some operation such as copy and move and delete files in Y machine . i tried with this code but it is doing in machine x only . and i need to exit from Y when... (1 Reply)
Discussion started by: rateeshkumar
1 Replies

10. Shell Programming and Scripting

executng program on remote machine using ssh

I am trying to search and remove files from a list of servers. I want to find every occurence of this file on each machine and then remove it. If I execute the find command on the remote machine I would like to be able to pipe the output to xargs and remove the file. Does anyone know hat would be... (1 Reply)
Discussion started by: sewood
1 Replies
Login or Register to Ask a Question