LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 08-16-2001, 09:37 AM   #1
saintt
LQ Newbie
 
Registered: Aug 2001
Location: London
Distribution: SuSE 7.1/7.3
Posts: 25

Rep: Reputation: 15
Shell script to change IP addresses


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
 
Old 08-16-2001, 04:51 PM   #2
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 47
Re: Shell script to change IP addresses

Quote:
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.

HTH

Jamie...
 
Old 08-20-2001, 05:39 AM   #3
saintt
LQ Newbie
 
Registered: Aug 2001
Location: London
Distribution: SuSE 7.1/7.3
Posts: 25

Original Poster
Rep: Reputation: 15
Thanks for your help.

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.
 
Old 08-20-2001, 05:50 AM   #4
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 47
Quote:
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.

HTH

Jamie...
 
Old 08-20-2001, 11:17 AM   #5
saintt
LQ Newbie
 
Registered: Aug 2001
Location: London
Distribution: SuSE 7.1/7.3
Posts: 25

Original Poster
Rep: Reputation: 15
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.

Or is there an easier/preferred way?
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to change password kamal_aitin Linux - General 6 07-25-2007 12:09 AM
Change batch script to shell script alan.belizario Programming 5 03-31-2005 12:41 AM
Change batch script to shell script alan.belizario Linux - Software 1 03-30-2005 01:49 AM
how to change PWD in a shell script slug420 Linux - General 8 01-29-2005 07:27 AM
How can i change the icon of a shell script? BajaNick Linux - Software 6 01-14-2004 12:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:55 PM.

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