Getting information from a DNS or DHCP Server - php

I'm making a network monitoring program with PHP.
I want to be able to connect to the DNS or DHCP Server and get all of the computers that belong to the network.
Currently I'm running nested for loops to ping every possible IP Address however it takes way to long with over 10 subnets.
I'm trying to change from doing it like a noob to doing it like a boss.
I want to get a list of all computer names and IP Addresses as well as if their IP was assigned Dynamically or Statically.
How should I do this?

There is no way to enumerate all hosts on a network, even pinging all of them isn't sufficient as some may not reply to ping. For the dynamically allocated ones you could query the DHCP server, but DHCP as a protocol does not support this, you'd have to write a daemon process and query the logs or assignment file.
If you do want to do it by pinging I'd suggest writing a small program the runs in the background and pings everything, keeping the results in memory, and then have your php script query that. This way you response time will be near immediate. So long as hosts don't appear and disappear too frequently (sounds unlikely) then this should be fine.

Related

How to detect if bot is scanning ports

Is there a way to detect if a bot is scanning your website/server for open ports with PHP? I googled this but i only found scripts to make a port scanner with php.
PHP is probably not the best language to do this as PHP mainly just meant to run whenever a client makes a request.
I imagine you'd need to interface with the OS directly to do this to capture network traffic on specific ports, which isn't one of PHPs strongsuits either.
You can then either try to inspect the incoming payload and determine whether it is a port scanner. Or you could keep a log with to count the total amount of different ports a single machine has connected to and block based on that.

How to detect a power failure?

I want to be able to perform an action on power failure i.e when electricity goes off since my network routers are on UPS so it doesn't know about power failures so somehow I want to be able to store the power failure data into a text file through php so anyway possible to write power-failure data to a text file through PHP??
Here is a screenshot of Mikrotik with different windows open regarding the question :
There is no proper way of doing this.
First of, you need an external machine.
Why? Because if a machine shuts down because it has no power, it cannot execute code anymore !
So here is what I thought:
If you have a server running on the routers that can be accessed remotly, you could simply "ping" ("file_get_contents" or "curl") your router with requests. If you get an error, that means it's not online anymore. And then store this into a text file.
If you want to use a local laptop to do this, you could just simply "ping" your router location (could be 192.168.0.1, depends on your router) and if it doesn't respond, that means it's offline.
Most UPS devices have SNMP server where you can query simple statistics (definitely Power failures).
There are some linux packages for instance nut-snmp, which you could use to get information from the UPS.

remote rendering of PHP file

this question might quite possibly not have an answer or be quite stupid. I guess we will find out.
My situation is the following:
I am wokring with an embedded device. On the embedded device there
are 10-20 webservices running -- you can actually imagine small
webservers. Each of them is on its own port. This one might have the IP 152.0.0.1
A top-level web-interface is wrapped around these webservices. This
webinterface runs on a standard LEMP setup (debian). Say this has the IP 10.0.0.1
To work on the embedded device one can locally jump from webservice to webservice by calling 152.0.0.1:xxxx, 152.0.0.1:xxxy, 152.0.0.1:xxxz in a (local) webbrowser. This works fine. It seems crazy to have a setup like this but I do not have the possiblity to change things on this end.
What I would like to do is to embed the small webservices in the top-level webinterface, which will have a navigation bar where one can chose which one to look at.
Both systems are not in the same network but connected via a ssh tunnel. However, the ports themselves are dynamic, so I can not simply hard-code them in the top-level web-interface. I should be able by building the navbar using dynamic ports obtained from a database -- this is possible. Since there will be multiple such embedded devices there will be multiple ports to forward with the ssh connection. This is the problematic part.
My question is: is there actually a way to get rid on this port-dilemma situation only on the nginx/php7 level? The embedded device should keep its own ports but I would not want to call them explicitely from the webinterface. Also, each device having a set of ports is quite cumbersome. I could use nginx reverse proxy to map the ports to URLs but I would have to do that on the embedded hardware, which is complicated. I could do it on the debian server as well using nginx but this means I would have to forward a whole bunch of ports from the embedded device to the debian server, which is not nice.
Is there a way I can make the embedded device build a page all on its own, using its internal port-scheme and then display that on the top-level webinterface? I am thinking about a command which allows me to do so, e.g.
buildpage($device, $port) on the embedded device alone and "forward" the entire content of that built webpage to the top-level webinterface. The top-level webinterface would then never have to deal with the port-confusion on the embedded device, it just has to say which port it should use, the embedded device internally pre-renders the page and sends it to the top-level webinterface. The point here being, the aforementioned ssh tunnel would not have to forward a whole bunch of ports anymore but possibly onlyone -- wich is the ultimate goal!
I might be overseeing an easy solution here and the approach probably does not make all that much sense but please consider it. In case there is a better option, I would be very keen to know.
Thank you a lot

PHP best way to show a user's ping to the webserver?

I have several VPS' and want to use PHP or shell scripting to display the ping in ms from the client to the server(s) so the user may compare which server to connect to. What is the best way to do this?
Assuming you want this to be asynchronous (so it doesn't stop the whole page from loading) then you could wrap something up in an Ajax call and then have a server-side script perform a ping on the $_SERVER['REMOTE_ADDR'] (IP of the person visiting the site). As for performing the ping itself, you should just be able to wrap that up in a "system('ping ..." call.
Hope that helps!
Depending on how sophisticated you want to get with your networking, you might want to give anycast a shot. The premise is that you give the same IP address to the two different VPS servers in the two different locations. When the client tries to connect to your server using a DNS lookup, it will find the single IP address, and depending on the closer server (ergo, better pinging server) should pickup that client. Cloudflare does this, and they explain it in their blog. Although, this might be seen a using a cannon to kill a mosquito, but it seems to work and it might even work for you. Here's some data that shows that Anycast is not evil.

Is it possible to keep a MYSQL session open?

Is it possible to keep a SQL connection/session "open" between PHP program iterations, so the program doesn't have to keep re-logging in?
I've written a PHP program that continually (and legally/respectfully) polls the web for statistical weather data, and then dumps it into a local MYSQL database for analysis. Rather than having to view the data through the local database browser, I've wanted to have it available as an online webpage hosted by an external web host.
Not sure of the best way to approach this, I exported the local MYSQL database up onto my web host's server, figuring that because the PHP program needs to be continually looping (and longer than the default runtime, with HTML also continually refreshing its page), it would be best if I kept the "engine" on my local computer where I can have the page continually looping in a browser, and then have it connect to the database up on my web server and dump the data there.
It worked for a few hours. But then, as I feared might happen, I lost access to my cPanel login/host. I've since confirmed through my own testing that my IP has been blocked (the hosting company is currently closed), no doubt due to the PHP program reconnecting to the online SQL database once every 10 minutes. I didn't think this behavior and amount of time between connections would be enough to warrant an IP blacklisting, but alas, it was.
Now, aside from the possibility of getting my IP whitelisted with the hosting company, is there a way to keep a MYSQL session/connection alive so that a program doesn't have to keep re-logging in between iterations?
I suppose this might only be possible if I could keep the PHP program running indefinitely, perhaps after manually adjusting the max run-time limits (I don't know if there would be other external limitations, too, perhaps browser limits). I'm not sure if this is feasible, or would work.
Is there some type of low-level system-wide "cookie" for a MYSQL connection? With the PHP program finishing and closing (and then waiting for the HTML to refresh the page), I suppose the only way to not have to re-log in again would be with some type of cookie, or IP address access (which would need server-side functionality/implementation).
I'll admit that my approach here probably isn't the most efficient/effective way to accomplish this. Thus, I'm also open to alternative approaches and suggestions that would accomplish the same end result -- a continual web-scrape loop that dumps into a database, and then have the database continually dumped to a webpage.
(I'm seeking a way to accomplish this other than asking my webhost for an IP whitelist, or merely determining their firewall's access ban rate. I'll do either of these if there's truly no feasible or better way.)
Perhaps you can try Persistent Database Connection.
This link explains about persistent connectivity: http://in2.php.net/manual/en/function.mysql-pconnect.php

Categories