I am a newbie to Linux Shell Script. I want to change the IP address by Linux Shell Script and called by a PHP web application.
I have finished my PHP web application and the shell script. Before they combine together and work, I would like to test if the Shell Script can run properly.
My Linux Shell Script will store 7 variables excluding $0 (the script name).:
- The network interface file (
/etc/sysconfig/network-scripts/ifcfg-{XXX}
) - Old IP Address
- New IP Address
- Old Subnet Mask
- New Subnet Mask
- Old Gateway
- New Gateway
Most checking was already perform by my PHP application as I am a newbie to Linux Shell Script, I don't want to do this on my shell script. My PHP application can make sure the data is correct and filled with values.
So to run this Shell Script, I type the following to see the results:
sh ./ipchanger.sh /etc/sysconfig/network-scripts/ens32 192.168.1.18 192.168.1.201 32 24 192.168.1.1 192.168.1.1
Meaning:
- Specify the target network scripts file.
- Changing the IP from
192.168.1.18
to192.168.1.201
. - Changing the Subnet Mask from
255.255.255.255
to255.255.255.0
.
Actually it will run the following commands:
sudo sed -i -r "s/$currentIpAddress/$newIpAddress/g" $fileName
sudo sed -i -r "s/$currentSubnetMask/$newSubnetMask/g" $fileName
sudo sed -i -r "s/$currentGateway/$newGateway/g" $fileName
It returns no error and its looking good as the last modified date of the network script file has been updated. But when I opened the file, nothing changed.
Then I echo
the commands, it returns the exact command I wanted. Then I run all three lines by command prompt and check the file again, all information has changed!
echo $SHELL
orps -p $$
in both interfaces to confirm that they are the same.