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 Code:
ssh -l root 192.168.0.100 "sed -i 's/^NET\_MAC\[2\]=.*/NET\_\MAC\[2\]=itworks/g' /home/file.txt" Code:
blah blah blah Code:
textvar=itworks ; ssh -l root 192.168.0.100 "sed -i 's/^NET\_MAC\[2\]=.*/NET\_\MAC\[2\]=$textvar/g' /home/file.txt" |
This seems to work:
Code:
textvar=itworks ; ssh -l root 192.168.0.100 "sed -i \"s/^NET_MAC\[1\]=/&$textvar/\" /home/file.txt" 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. |
Quote:
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" Thanks for the help |
You're welcome :)
BTW: Can you put up the [SOLVED] tag (upper right corner / Thread Tools menu)? |
Quote:
So the command that does the trick is Quote:
Quote:
Thanks in advance for any assistance |
@tripialos
This doesn't make too much sense: Code:
su testuser -c 'ssh -l root 192.168.0.100 ...... 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"' |
Quote:
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 Thanks again |
Quote:
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. |
Quote:
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? |
It all depends on what users are allowed to do (locally and/or remotely).
Quote:
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:
Quote:
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 |
Quote:
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! |
Quote:
|
All times are GMT -5. The time now is 10:30 AM. |