Jump to content

Edit network configuration, re-start network, and redirect


NotionCommotion

Recommended Posts

Without performing an ajax post and then redirecting the page using JavaScript, am I able to change network settings and redirect to the new static IP?  The following changes the IP settings, but does not redirect.  Thanks

 

PS.  No warning that this is a stupid idea are necessary.

<?php
$new_ip_address='10.5.3.5/24';
$static=explode('/',$new_ip_address);
$file='/etc/dhcpcd.conf';
$network = file_get_contents($file);
preg_match('/(?<=static ip_address\=)(.*)/', $network, $old_static_ip);
$redirect=explode('/',$old_static_ip[0])[0]==$static[0]?'/':$static[0];
$network=preg_replace('/(?<=static ip_address\=)(.*)/', trim($static[0]).'/'.trim($static[1]), $network);
safefilerewrite($file, $network);
header("Location: $redirect");
exec('../netstart');
//header("Location: $redirect");
 
cat > wrapper.c <<CONTENT
  #include <stdlib.h>
  #include <sys/types.h>
  #include <unistd.h>


  int
  main (int argc, char *argv[])
  {
     setuid (0);
     system("sudo ip link set eth0 down && sudo ip link set eth0 up");
     return 0;
   }
CONTENT


gcc-6 wrapper.c -o netstart
sudo chown root netstart
sudo chmod u=rwx,go=xr,+s netstart

 

Link to comment
Share on other sites

How would the client know where to redirect if the server doesn't tell it what the new address is? And if the address is truly static then what is the point of all this?

 

The entire purpose of the website is to allow the user to locally logon using the existing static IP,  view the current settings (static IP, but also gateway, etc), and change them.

 

As such, the client knows the new desired IP.

 

I first tried the header with the new location approach, but as I indicated in my original post, it didn't redirect.  Note that I should have used header("Location: http://$redirect");, but it didn't make a difference.

 

I've got it working using an ajax call and a client side redirect, however, ideally I didn't want to use JavaScript.  Guess I will just put a note on the page instructing the user to manually change the IP to the new IP, and reload the browser.

 

Side note.  I've been trying to use the hostname in the browser instead of a static IP, but have been unsuccessful.  Device is a Pi and HTTP server is nginx.  Should this be possible to do?

 

Thanks

Link to comment
Share on other sites

If the client knows the new IP then it can certainly redirect, yes. Something like

window.location.href = window.location.protocol + "//" + newip + window.location.pathname;
Probably need a delay between the request and redirect - to give the server time to actually change.

 

You can't rely on the hostname for the new address: DNS will very likely not have caught up, what with the various caches between the user and the server.

Link to comment
Share on other sites

I was doing the same, but by hard coding the protocol.  I like your approach more.

 

Delay isn't required (at least not with Chrome).  Browser just hangs for a second or two, and then redirects.

 

The host name wouldn't be used for this purpose, so I don't think I will have the cache issues.  The purpose would be if the IP was forgotten but the host name was known, the page could still be accessed.

Link to comment
Share on other sites

The purpose would be if the IP was forgotten but the host name was known, the page could still be accessed.

DNS would have to have the correct information for the hostname. With a static address I would assume DNS has a static record too, however what you've said up to now suggests that's not the case.

 

Configuring a static address on the server will not trigger the DHCP server to issue a lease, it being a service that might be capable of automatically updating DNS (eg, on a Windows network), so if there's any DNS updating to do then the server will have to do it on its own. Or the user, I suppose.

Link to comment
Share on other sites

Just put a link on the page they can click on if the JS doesn't work. For example

 

<p>Settings changed.  If the page does not refresh automatically, <a href="<?=$redirect?>">click here</a></p>
<script>
  location.href="<?=$redirect?>";
</script>
Be sure to take care of all the necessary variable escaping and what not.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.