1

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

  1. The network interface file (/etc/sysconfig/network-scripts/ifcfg-{XXX})
  2. Old IP Address
  3. New IP Address
  4. Old Subnet Mask
  5. New Subnet Mask
  6. Old Gateway
  7. 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:

  1. Specify the target network scripts file.
  2. Changing the IP from 192.168.1.18 to 192.168.1.201.
  3. Changing the Subnet Mask from 255.255.255.255 to 255.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!

1
  • if your issue isn't sudo as @davidgo has suggested, you may want to confirm that both interfaces are using the same shell. you might want to try running echo $SHELL or ps -p $$ in both interfaces to confirm that they are the same. Commented Sep 4, 2015 at 3:56

1 Answer 1

0

The problem is most likely the sudo bit at the beginning of each line. sudo implies “run as root.” You will probably find that, effectively, the account you are logged in as at the command line has been set up in /etc/sudoers to work from the command line, but not through the PHP script - and indeed you need to consider the implications of this when you run it through the web user - the web user will probably not be set up in /etc/sudoers - and should not be!

You can test this theory by changing the ownership of the files to you and running the script without sudo. To change the file ownership, simply chown filename.ext

I posit that you would be better off :

  1. Creating a new directory under /var/www (or wherever you are comfortable and the web user has access)
  2. Moving the appropriate config files to that directory
  3. Symlinking either the individual files or the directory containing it to www.
  4. CHANGE THE OWNERSHIP OF THESE FILES TO THE WWW directory
  5. Drop the sudo at the beginning of each command. Better, why not drop calling the script and simply write the changes in PHP.
5
  • After removing the sudo at the beginning of each line, its still works!
    – AkiEru
    Commented Sep 4, 2015 at 4:02
  • What does "whoami" say, and what is the PHP "exec" type command you are using ?
    – davidgo
    Commented Sep 4, 2015 at 4:07
  • whoami shows apache
    – AkiEru
    Commented Sep 4, 2015 at 4:11
  • That makes little sense. Does that happen when you run it from the bash prompt or via the script ? What are the permissions on the files ?
    – davidgo
    Commented Sep 4, 2015 at 5:31
  • Now I can make the shell script itself change the IP address. But in PHP, it couldn't.
    – AkiEru
    Commented Sep 4, 2015 at 6:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .