I am a bit rusty on my scripting abilities and am trying to find a good starting point for creating a script that creates files (generating new file names) and edits the IP address in the file by incrementing it by +1
My scenario is like so - We have about 50 IP addresses to add to our network-scripts each with an incrementing IP. I could do this by hand, however I thought this would be a good time to try and get up to speed on my scripting skills.
The naming scheme is like so:
ifcfg-eth0:1, ifcfg-eth0:2, ifcfg-eth0:3 ...*n*
The content of these files is:
DEVICE=eth0:1 NETMASK=255.255.255.0 IPADDR=10.2.7.148 BOOTPROTO=none ONBOOT=yes DNS1=10.2.53.150 PEERDNS=yes DNS2=10.2.53.250 GATEWAY=10.2.7.1 TYPE=Ethernet USERCTL=no IPV6INIT=no
Where IPADDR=10.2.7.148
should be increased by +1 in each file.
For example: ifcfg-eth0:2
file would be the exact same except that the IPADDR
would be 10.2.7.149
and so on.
I am fairly certain I should invoke sed
to find and replace the IP addresses.
For instance, searching for an IP address using sed
could be accomplished like so:
sed -rn '/(IPPADDR=)((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/p' file
What steps need to be taken to generate a new file with the eth0:N
name incremented by +1 as well as the IP address in the file.
Thanks in advance!
EDIT: Let me clarify the names of the files should not contain the IP address.
I am needing file1 to be copied over to file2 eth0:1
->eth0:2
with same file contents as above, except the fact that the IPADDR=
field on the generated file should be incremented by one.
Note: The file name should also be incremented by +1 IE. cp ifcfg-eth0:1 ifcfg-eth0:2
sed -rn '/IPPADDR=)((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/p' file >> newFile