How to extract specific words and replace them from a file

I want to make a file which will open a file ~eth0, I have come this far
2 <?php
3 $file = ‘/etc/sysconfig/network-scripts/ifcfg-eth0’;
4 $myfile = fopen($file, “r”) or die(“Unable to open file.”);
5 $test_str = explode("\n", fread($myfile,filesize($file)));
6 fclose($myfile);
7 $keys1 = preg_grep("/DEVICE/" , $test_str);
8 var_dump($keys);
9 ?>
This gives the complete line,
array(1) {
string(13) “DEVICE=“eth0””
I want to change the data after “=” , however I can extract the complete line only, how do I extract say eth0 and change it to something that I want, eg IPADDR=... to x.y.z.q and so on

Well, I seldom use FOPEN these days. I normally just use file_get_contents since it requires no closing.
Also, you don’t need to know the filesize that way. But, it looks like you are exploding the file by line breaks.
Then, you look for all that contain /DEVICE/. This gives you a list of all of them. You can just rebuild the
resulting text using another explode for “=” and then create it the way you wish. But, “eth0” is not an IP
address. That can not be altered into a 999.999.999.999 format.

I am not sure what the $file address points to. Are you attempting to acquire a list of IP addresses in
a Windows system, Linux, server, what??? Please explain what you are attempting to do. Thanks.

Appreciate your reply:grinning:
the $file is for the file on the server /etc/sysconfig…ifcfg-eth0, it is a text file (linux).
What I want to do is open the file contents and modify as needed using php and then saving the changed data to the same file. eg change IP address from to abc.def.ghi.zzz and so on

To change a Linux IP on a server, you can not do it thru a normal text change in any file. You must tell the
server to change using a Linux command, not some change in a text file. This Linux command will change
the IP address inside that file, but, it also alters other things in the server to handle the changes.
The Linux command needed is something like: ifconfig eth0 netmask
Using the IP address you want it changed to.

Now, you can do that Linux command from PHP. This would be done using a SHELL command.
Something like this should work:
shell_exec(“ifconfig eth0 netmask”);
No editing of the file is needed. Remember that on some server systems, this change will not work
until the server is rebooted. In most cases, it should change immediately, but, does not always work until
after a reboot of the server.

Also, note that on some servers, you need to use shell_exec(…), or exec(…) or system(…) So, not sure
on your server. I would try shell_exec() first. Please note that these commands are shut off on most all
shared servers. Linux or not, shared servers block this often because they control your IP address.

Hope this info helps you out. Wondering why you need to change your IP address dynamically.
Usually, you IP address is controlled by the ISP you use, but, at least you now know how to do it.

This is for an application that I have made, rather than editing the network file ( as I do presently) it should be possible for user to change the ipadd, ge etc according to the users choose through PHP

Well, not sure about that. You never would want a user to be allowed to change the server’s IP address.

If you mean you are creating an app that runs on their system, they will have their own IP and if you need
to change it, you can just read the local IP of their server and then use that value. Most users never know
their IP addresses. I mean, if you “clone” a system and store it on another server, that new server already
has it’s own IP address which you can read using PHP and store where needed. The user would not always know their own IP address. Happens often! (But, the shell command should work…)

OK thanks will try out the shell command Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service