Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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'm developing software to be delivered to a number of customers who are unfamiliar with Linux. The system to be delivered uses two Znyx ethernet cards, one of which has 125 aliases set up on it (znb0:1 to znb0:125) giving me a total of 127 IP addresses including the two base card addresses.
The aliases are set up in a script that is called from the boot.local file.
It it quite likely that the users of the system I deliver will want change the base address of the second ethernet card and possibly toggle the aliasing on and off. Despite the fact I could write comprehensive instructions on how to do this, I don't trust my users. They are bound to screw something up and I don't fancy all the support calls that are bound to come my way when they do.
So I want to make their life easy, I want to write them a shell script they can run that calls up a text based menu from which they can select the options they wish, such as change base IP address, toggle aliasing on/off.
I've done the easy bit - the text based menu. I need help with the hard bit, getting the changes I make to stick. I have been using ifconfig commands to make my changes but of course these are lost on reboot. So I have a problem. I don't really want to have to run my script every time the machine is booted. Any ideas?
And will I need to give my users the root password to run my script or can I work round that?
And does anyone know of a way of ensuring I don't duplicate IP addresses? If I change the base address of the second ethernet card, it is likely to be to an address that is already taken by one of my aliases. Is there a clever way of "swapping" the addresses of the alias and the base ethernet card?
Any help will be greatly appreciated and keep my boss off my back for a bit longer
Originally posted by saintt The aliases are set up in a script that is called from the boot.local file.
<SNIP>
I've done the easy bit - the text based menu. I need help with the hard bit, getting the changes I make to stick. I have been using ifconfig commands to make my changes but of course these are lost on reboot. So I have a problem. I don't really want to have to run my script every time the machine is booted. Any ideas?
At the risk of stating the obvious make your script edit the script that initially assigns the IP address at boot time! Or is there some reason you can't do this?
Quote:
Originally posted by saintt And will I need to give my users the root password to run my script or can I work round that?
Have a look into sudo I think you can create a wrapper about your programs. If is was a proper binary then you could make it a Set UID program (SUID) and make it owned by root. That way no matter who actually executes the program it runs with root's permissions. You need to be really careful with SUID programs though - if you can get a SUID program to executed a command for you then you effectively have cracked root on that machine.
Quote:
Originally posted by saintt And does anyone know of a way of ensuring I don't duplicate IP addresses?
I'd get the script itself to do this for you. You'll just need to compare the 'new' IP address to all the other address to check they are !=
Quote:
Originally posted by saintt If I change the base address of the second ethernet card, it is likely to be to an address that is already taken by one of my aliases. Is there a clever way of "swapping" the addresses of the alias and the base ethernet card?
Base address?? Do you mean the first IP address in your 125 IP-address list? I don't follow this bit! As for it already being taken - again check it again all existing interfaces before committing any changes.
And thank goodness this board has help for newbies like me. That means I have no fear about asking this - how do I make a script edit another script?? Or any file for that matter?
I have no problems with my script editing the script that runs on boot if I knew how to make the edits. Once the script that assigns the addresses at boot time has been changed then it would just be a reboot and I'm away, yeah? Is that preferable to just re-running the edited version of the script?
By base address I meant the address of the actual physically real card (znb0/znb1) not one of my aliases.
The likelihood is that the users will want to change the IP address of znb1. If they do then they will be changing it to one of the IP addresses that is used by one of my aliases on znb0. I suppose if I do as you suggest and run a loop that checks IP addresses are not equal I could add in a bit of code to do the swapping I spoke of. If the new address of znb1 matches the address of one of the znb0 aliases then replace the address of the alias with the old znb1 IP address.
Originally posted by saintt That means I have no fear about asking this - how do I make a script edit another script?? Or any file for that matter?
Well, thats programming language dependent!! Typically you would need to read the original file into memory, make your changes, then write the entire file back out over the original. What language you scripting in? Perl is always a winner if you want to manipulate text
Quote:
Originally posted by saintt Once the script that assigns the addresses at boot time has been changed then it would just be a reboot and I'm away, yeah? Is that preferable to just re-running the edited version of the script?
There is no need to reboot - you can just change the IP addresses by running the script again - just make sure that you've taken the interfaces down first using ifconfig <ifwhatever> down
Quote:
Originally posted by saintt By base address I meant the address of the actual physically real card (znb0/znb1) not one of my aliases.
I think that you'll just need to check this 'base' address aganst all your other IP addresses, they're treated no differently to the aliases so you need to carry out the same checks on all the address, at least as far as I can see.
The text menu I have written has been sort of ripped off from legacy code I found lying around on one of our systems. No-one here has any real Linux experience but this looks like a simple bit of Linux shell script code. I think!
It looks similar(ish) to C but it isn't C. But I do know C so would I be better off trying to re-write my script in C? I don't know any Perl at all by the way. Could I then use the system (?) function to call out and run shell commands such as ifconfig?
What is the easiest way to do what I am trying to achieve do you think? Create a whole new IP address definition file, rewriting over the original each time I run this script? I could store my IP addresses and corresponding aliases as an array and then write those values out into the relevant places in a number of text strings:
ifconfig <array element 1> <array element 2>
where array element 1 is a card or alias and array element 2 is an IP address.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.