LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2013, 04:21 AM   #1
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Rep: Reputation: Disabled
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
 
Old 08-13-2013, 04:46 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
1 members found this post helpful.
Old 08-13-2013, 04:57 AM   #3
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by druuna View Post
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
 
Old 08-13-2013, 05:12 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome

BTW: Can you put up the [SOLVED] tag (upper right corner / Thread Tools menu)?
 
Old 09-04-2013, 06:47 AM   #5
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
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

Last edited by tripialos; 09-05-2013 at 02:31 AM.
 
Old 09-05-2013, 02:15 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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?
 
Old 09-05-2013, 02:39 AM   #7
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
@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

Last edited by tripialos; 09-05-2013 at 02:55 AM.
 
Old 09-05-2013, 02:55 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by tripialos View Post
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.

Last edited by druuna; 09-05-2013 at 03:08 AM. Reason: changed root user to Z user
 
Old 09-05-2013, 03:19 AM   #9
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
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?
 
Old 09-05-2013, 03:40 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
It all depends on what users are allowed to do (locally and/or remotely).
Quote:
Originally Posted by tripialos View Post
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"
 
Old 09-05-2013, 08:10 AM   #11
tripialos
Member
 
Registered: Apr 2012
Posts: 169

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
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!
 
Old 09-05-2013, 08:18 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by tripialos View Post
thanks alot for your help, if it werent you i would still strougling to nothingness!! BIG BIG BIG THANKS!
You're welcome
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] passing one variable to another coolsreejith Linux - Newbie 2 01-08-2011 10:22 PM
[SOLVED] Bash - Passing variable to ssh zepphead5 Programming 8 05-09-2010 09:34 AM
[SOLVED] Passing local variable pointers in ASM MTK358 Programming 8 04-13-2010 07:58 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 02:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration