Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change IP address of Server via PHP

Status
Not open for further replies.

Codesse

Programmer
Jan 28, 2002
10
US
I am running apache with php5 on mac OS X. My application requires the ability to enable a user to change the IP address of the server through a web interface. Is there a slick method that can use PHP to shell_exec to the OS X system to modifiy the IP address?

I tried using IFCONFIG, but it gave a permission denied reponse.

<?php
//get new ip address
$newip = $_GET['newip'];
//Build a command to change the IP address
$command = "ifconfig en0 $newip";
//shell to some function in OS X
$response = shell_exec($command);
?>

 
have you tried correcting the $command to use "ipconfig" rather than "ifconfig"?

but anway i don't think ipconfig on a win box anyway can change the ip address of an adapter. better surely to run a netsh session from php and pipe in a dynamic script.

 
jpadie: Codesse's box is a Mac, not windows, and the correct command in mac (as in unix/linux) is "ifconfig".

Codese: unfortunatelly, the "ifconfig" command can be run only for root since this is an administrative command. The only way I can think is to write a shell script in order to use the ifconfig command through "sudo" (to get the privilegies) and call that script from php.

Cheers.
 
I tried to modify the sudoers file to allow the "www" to access the ifconfig, but I still am getting a "permission denied". Do I have to use chmod on the directory or ifconfig file? I am new at OS X so I don't know how to properly set the permissions or the ramifications of setting those permissions. Can anyone help me? Again I am not a OS X(Unix) expert.

Thanks
 
I'm not that familiar with OSX intrinsics, but on Linux ifconfig is an application that can only be run as root. Unless your web server is running as root (which I do not recommend), you're probably going to have to run ifconfig through the sudo system. I assume this is available on your OSX system.

On Linux, I'd invoke [tt]man sudoers[/tt] for more information.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
your command should be something like:

$command = "sudo ifconfig en0 $newip";

you need to put "sudo" in order to grant access to that command.
 
As I understand it, the "sudoers" file must be set up first to allow the user as which the web server is running to execute ifconfig.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I have tried the:

$command = "sudo ifconfig en0 $newip";

however it fails. When I attempt that command directly on the command line it requires you to enter a password. However, in my sudoers file I placed:

www ALL=(root) NOPASSWD: /sbin/ifconfig

Shouldn't this allow Apache to run ifconfig without a password?

Thanks
 
actually this is a sudo configuration, not apache nor php... take a look to the man pages for sudo (man sudo). Unfortunately I have my linux broken to take a look for myself and test it.

your problem will be solved when you (via command line) can issue "sudo ifconfig" command with no password.

Cheers.
 
I used pico. That might be my problem. Do I have to chmod the file after I pico it?
 
I did

chmod 440 /etc/sudoers

Now the ifconfig will work by the command line, but it still doesn't work through php. I think I am on the right track now.

Thanks for your help!
 
If it works through the command-line but not through PHP, I would guess that the problem is that your sudoers configuration is not set up for the right user.

Unless you're running ifconfig from the command-line as the user as which your web server runs.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
AHHH!

ifconfig only makes temporary change to IP address. When the box is rebooted it reverts back to the old ip address.

I give up....

Any OS X or Unix experts have any advice.

Thanks.
 
That is true. ifconfig only makes a change that takes effect during the current run.

However, the configuration is stored in a text file on the filesystem somewhere. In Linux, there is a file named "ifcfg-eth0", which configures the IP address, etc., for the interface eth0.

From your invocation of ifconfig, I'd recommend you examine your filesystem for a file named "ifcfg-en0" then write a PHP script which rewrites the file and also invokes ifconfig to make the change immediately.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top