LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   passing local variable with sed via ssh (https://www.linuxquestions.org/questions/linux-newbie-8/passing-local-variable-with-sed-via-ssh-4175473217/)

tripialos 08-13-2013 04:21 AM

passing local variable with sed via ssh
 
guru "basheller" nedeed !!!

Ok, i have a file on a remote machine which contains the following text:

Code:

blah blah blah
NET_MAC[1]=

with the below command (which i found out here from LQ):

Code:

ssh -l root 192.168.0.100 "sed -i 's/^NET\_MAC\[2\]=.*/NET\_\MAC\[2\]=itworks/g' /home/file.txt"
i can remotely, via ssh, change the line 2 of the remote file to

Code:

blah blah blah
NET_MAC[1]=itworks

now what i want to do, is send the text "itworks" using a variable instead of hardcode it. in other words

Code:

textvar=itworks ; ssh -l root 192.168.0.100 "sed -i 's/^NET\_MAC\[2\]=.*/NET\_\MAC\[2\]=$textvar/g' /home/file.txt"
But this doesnt work for me. any help would be appriciated

druuna 08-13-2013 04:46 AM

This seems to work:
Code:

textvar=itworks ; ssh -l root 192.168.0.100 "sed -i \"s/^NET_MAC\[1\]=/&$textvar/\" /home/file.txt"
Your sed statement uses single quotes (sed 's/x/y/') which makes sure that variables are not expanded. You need to uses double quotes.

However: the whole command is surrounded by double quotes, which means you need to escape the ones used for sed.

BTW: I also changed the sed statement you used.

tripialos 08-13-2013 04:57 AM

Quote:

Originally Posted by druuna (Post 5008412)
This seems to work:
Code:

textvar=itworks ; ssh -l root 192.168.0.100 "sed -i \"s/^NET_MAC\[1\]=/&$textvar/\" /home/file.txt"
Your sed statement uses single quotes (sed 's/x/y/') which makes sure that variables are not expanded. You need to uses double quotes.

However: the whole command is surrounded by double quotes, which means you need to escape the ones used for sed.

BTW: I also changed the sed statement you used.

Druuana you are my hero man!!

Based on your commends , i further experimented and i also manage to find a workaround, again thanks to you.

Code:

textvar=itworks ; ssh -l root 192.168.0.100 "sed -i 's/^NET\_MAC\[1\]=.*/NET\_\MAC\[1\]=$textvar/g'" "/home/file.txt"
What i did was just to split the command parameters and the paths with double quotes , however your method is more neat.

Thanks for the help

druuna 08-13-2013 05:12 AM

You're welcome :)

BTW: Can you put up the [SOLVED] tag (upper right corner / Thread Tools menu)?

tripialos 09-04-2013 06:47 AM

Quote:

Originally Posted by druuna (Post 5008427)
You're welcome :)

BTW: Can you put up the [SOLVED] tag (upper right corner / Thread Tools menu)?

OK now i am stuck in the last part of what i am trying to do which confused me alot.

So the command that does the trick is

Quote:


textvar=itworks ; ssh -l root 192.168.0.100 "sed -i 's/^NET\_MAC\[1\]=.*/NET\_\MAC\[1\]=$textvar/g'" "/home/file.txt"

but now i want to run this command using su and if i do that it is not working. For example if i type the below (ommiting the variable decleration):

Quote:


su testuser -c 'ssh -l testuser 192.168.0.100 "sed -i 's/^NET\_MAC\[1\]=.*/NET\_\MAC\[1\]=$textvar/g'" "/home/file.txt"'

i do realise that things change again since the whole working command is surrounded by single quotes but i am confused of what to escape in order to make it work.

Thanks in advance for any assistance

druuna 09-05-2013 02:15 AM

@tripialos

This doesn't make too much sense:
Code:

su testuser -c 'ssh -l root 192.168.0.100 ......
You first become testuser using su (locally) and then you run a command remotely as root user. This seems unnecessarily complicated.

Why do you want/need to become testuser first? It doesn't influence the remote command (which will still be done as root).

About your problem:

This works for me:
Code:

su testuser -c 'textvar=itworks ; ssh -l root 192.168.0.100 "sed -i \"s/^NET_MAC\[1\]=/&$textvar/\" /home/file.txt"'
You say its not working, what is the problem when you run this?

tripialos 09-05-2013 02:39 AM

Quote:

Originally Posted by druuna (Post 5022097)
@tripialos

This doesn't make too much sense:
Code:

su testuser -c 'ssh -l root 192.168.0.100 ......
You first become testuser using su (locally) and then you run a command remotely as root user. This seems unnecessarily complicated.

Why do you want/need to become testuser first? It doesn't influence the remote command (which will still be done as root).

About your problem:

This works for me:
Code:

su testuser -c 'textvar=itworks ; ssh -l root 192.168.0.100 "sed -i \"s/^NET_MAC\[1\]=/&$textvar/\" /home/file.txt"'
You say its not working, what is the problem when you run this?

Druuna, once again thanks alot for your replay. I forgot to change the ssh user in my exaple hence why it did not make sense. Sorry about that.
Once again your solution did work but this time patialy, it didn`t read the variable for some reason.

I created the following script according to your sugestions which is what i am actually trying to achieve:

Code:

#Get the mac address
MAC_ADDR=`ifconfig eth1 | grep HWaddr | sed s/.*HWaddr\ // | tr -d ' '`

#save the mac address on the server on a specifc position within the file.txt
su testuser -c  'ssh -l testuser 192.168.0.100 "sed -i \"s/^NET\_MAC\[2\]=.*/NET\_\MAC\[2\]=$MAC_ADDR/g\" /home/file.txt"'

It does not load the value of the variable , it leaves it blank any ideas?

Thanks again

druuna 09-05-2013 02:55 AM

Quote:

Originally Posted by tripialos (Post 5022119)
I forgot to change the ssh user in my exaple hence why it did not make sense. Sorry about that.

I don't think you get what I'm trying to say.

You are logged in as user X, then you use su to become user Y (still locally), then you run ssh as user Z.

Why do you first switch from user X to user Y using su? Although I can think of a reason why you would have to do this, normally this isn't needed.

The reason your script doesn't work has to do with the single quotes around the around the ssh .... part. The shell will not expand variables when it sees single quotes, so MAC_ADDR will be empty when the whole su testuser -c 'ssh -l ...' part is executed.

Solving this will become rather messy, so please answer the other question first.

tripialos 09-05-2013 03:19 AM

Quote:

Originally Posted by druuna (Post 5022127)
I don't think you get what I'm trying to say.

You are logged in as user X, then you use su to become user Y (still locally), then you run ssh as user root.

Why do you first switch from user X to user Y using su? Although I can think of a reason why you would have to do this, normally this isn't needed.

The reason your script doesn't work has to do with the single quotes around the around the ssh .... part. The shell will not expand variables when it sees single quotes, so MAC_ADDR will be empty when the whole su testuser -c 'ssh -l ...' part is executed.

Solving this will become rather messy, so please answer the other question first.

OK.

I login as root on my machine. I then run a command as a testuser in order to "load" the known_host and the private/public keys from the testuser and also to login on the remote machine as the testuser.

One solution to the relevant problem is to run the command as root which would result not needing to add the "su -" command on my script. I dont want to create keys that will give root access to the remote machine thats why i want to su to another user.

Is my thinking wrong?

druuna 09-05-2013 03:40 AM

It all depends on what users are allowed to do (locally and/or remotely).
Quote:

Originally Posted by tripialos (Post 5022139)
I login as root on my machine.

Why? Do you have to be root for a specific reason?

There doesn't seem to be a good reason if I look at your example. You can run the ifconfig command as regular user (use /sbin/ifconfig instead of ifconfig).

Quote:

I then run a command as a testuser in order to "load" the known_host and the private/public keys from the testuser
I assume you become testuser to make sure that passwordless ssh is possible.

Quote:

and also to login on the remote machine as the testuser.
Except for the above (passwordless ssh) there is no real need to become testuser locally.

Putting it all together you should be able to do the following and assuming that testuser is set up to use passwordless ssh:

- log in as testuser
- run the following script:
Code:

#!/bin/bash

#Get the mac address
MAC_ADDR="$( /sbin/ifconfig eth1 | awk '/HWaddr/ { print $5 }')"

#save the mac address on the server on a specifc position within the file.txt
ssh -l testuser 192.168.0.100 "sed -i \"s/^NET_MAC\[1\]=/&$MAC_ADDR/\" /home/file.txt"


tripialos 09-05-2013 08:10 AM

Quote:

Originally Posted by druuna (Post 5022156)
It all depends on what users are allowed to do (locally and/or remotely).
Why? Do you have to be root for a specific reason?

There doesn't seem to be a good reason if I look at your example. You can run the ifconfig command as regular user (use /sbin/ifconfig instead of ifconfig).

I assume you become testuser to make sure that passwordless ssh is possible.

Except for the above (passwordless ssh) there is no real need to become testuser locally.

Putting it all together you should be able to do the following and assuming that testuser is set up to use passwordless ssh:

- log in as testuser
- run the following script:
Code:

#!/bin/bash

#Get the mac address
MAC_ADDR="$( /sbin/ifconfig eth1 | awk '/HWaddr/ { print $5 }')"

#save the mac address on the server on a specifc position within the file.txt
ssh -l testuser 192.168.0.100 "sed -i \"s/^NET_MAC\[1\]=/&$MAC_ADDR/\" /home/file.txt"


You are absolutely right . I never thought of that aproach. I did as you advised and it all went well.

I further improvised according to your suggestion, i came up with the "patent" that i could just insert the commands to the script, lets say sedscript.sh

so i then i just

su testuser -c sedscript.sh

where the script contain the command.


thanks alot for your help, if it werent you i would still strougling to nothingness!! BIG BIG BIG THANKS!

druuna 09-05-2013 08:18 AM

Quote:

Originally Posted by tripialos (Post 5022279)
thanks alot for your help, if it werent you i would still strougling to nothingness!! BIG BIG BIG THANKS!

You're welcome :)


All times are GMT -5. The time now is 10:30 AM.