Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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
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.
It all depends on what users are allowed to do (locally and/or remotely).
Quote:
Originally Posted by tripialos
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"
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.