Thursday, November 15, 2012

PHP script to search IP's and find matching URL's

For those who saw my Mobotix camera post, you probably have an idea of my love for security cameras and the like.
While trying to find more Mobotix cameras using Google, I quickly ran out of search results to check out. But I knew there had to be more out there that Google just isn't finding.

I decided there must be a simple script you could run that searches for URL's that match a particular search string (just like Google does).  But you could have it search a particular range of IP's, routinely checking one for the search string.
It wouldn't be pretty or efficient of course, but it would be simple and I could probably code it.

Let me preface this by saying, I'd never coded in PHP before, which might already be obvious.

So, my little PHP search script looks like this:

 You enter an IP address (minus the last 3 digits). This of course means that the search range is very limited.

Then you enter the IP range you want it to search. In the example above the IP's searched are:
216.172.100.1 through 216.172.100.80.

Then you enter a filename for the script to save its results to. If any valid IP's have a url that matches the search string, their header information is written to the file.

Like I said, it is very limited in what it can do, but not bad for my first PHP script, written on the fly. It's a starting place for anyone who wants to expand on it.

Here is the code. It's broken up into two files.

index.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>IP Check</title>
</head>

<body>
 

<a href ="index.php">Home</a>

<p>IP Check
<p>
<p>
IP format XXX.XXX.XXX. (ex. 70.147.100.)
<p>
 X = lower number<p>
 max = upper limit <p>
 Filename to write to.<p><p><p>
 <form action="camcheck.php" method="post">

Enter IP: <input name="ip" size="12">
<p>
Enter X: <input name="x" size="3">
Enter Max: <input name="max" size="3">
<p>
 Filename: <input name="myfile" size="20">

<input type="submit" value="Start">
</form>

<p>
 <img src="magnify.jpg" alt="magnify"/>
<?php

?>
<p><p>
 </body>
</html>

The index file is basically just a bunch of html text boxes. The info captured is then used by the second file.

camcheck.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>IP Check</title>
</head>
<body>


<?php

$path = "/control/userimage.html";  // url search string
 
$myFile = $_POST['myfile'];
   
$fh = fopen($myFile, 'w') or die("Can't open file");  //open file

for ($x =(int)$_POST['x']; $x <= (int)$_POST['max']; $x++)
{
    $url= $_POST['ip'].strval($x);
    //echo $url;

    $valid = @fsockopen($url, 80, $errno, $errstr, 4);   //check url

 if (!$valid) {
     // Output Error Message, if you care
    // echo '<p>not valid<p>';
    //echo "$errstr ($errno)<br />\n";
    //$returnstring = $url."  Not Valid $errstr ($errno)> \n
    //$returnstring = "X = ".$x;
     //fwrite($fh,$returnstring);

       }
    else {
         // Output Success Message!
       fputs($valid, "\nHEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n"); //socket opened
        $headers = fread($valid, 4096);
       
     $returnstring = "\n".$url."  Valid\n";
     fwrite($fh,$returnstring);    //write valid IP to file
     fwrite($fh,$headers);         //write header info
     fwrite($fh,"\n");
     }
}
 echo "\n Program Complete. Results sent to ".$myFile;
fclose($fh);

?>
<p><p>
</body>
</html>

In the camcheck.php file, you'll see that the url search string is hard-coded into $path. It would probably be easy to make that another textbox, so you can change what url string to search for.

"/control/userimage.html" is a typical Mobotix web cam url. Which is what I created the script to find.

When you run the script, it saves the header results to a file on your webserver. 

Here are some example results:
 




216.172.104.49  Valid
HTTP/1.1 302 Found
Date: Thu, 15 Nov 2012 16:32:40 GMT
Server: Apache
Location: https://billing.dathorn.com/control/userimage.html
Connection: close
Content-Type: text/html; charset=iso-8859-1





216.172.104.50  Valid
HTTP/1.1 404 Not Found
Date: Thu, 15 Nov 2012 16:32:42 GMT
Server: LiteSpeed
Connection: close
Content-Type: text/html


216.172.104.55  Valid
HTTP/1.1 302 Found
Date: Thu, 15 Nov 2012 16:32:43 GMT
Server: LiteSpeed
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Location: https://2/control/userimage.html


216.172.104.56  Valid
HTTP/1.1 404 Not Found
Date: Thu, 15 Nov 2012 16:32:43 GMT
Server: LiteSpeed
Connection: close
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Thu, 15 Nov 2012 16:32:43 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
P3P: CP="NON CURa ADMa DEVa TAIa CONi OUR DELa BUS IND PHY ONL UNI PUR COM NAV DEM STA"
Content-Type: text/html; charset=iso-8859-1
Set-Cookie: xid=788c930fc7639731a0609de479e12ae9; path=/
Set-Cookie: xid=788c930fc7639731a0609de479e12ae9; path=/; domain=www.comicjumps.com
Set-Cookie: xid=788c930fc7639731a0609de479e12ae9; path=/; domain=www.comicjumps.com
Set-Cookie: RefererCookie=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=www.comicjumps.com
Set-Cookie: store_language=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT
Set-Cookie: store_language=US; expires=Fri, 15-Nov-2013 16:32:43 GMT


I find it interesting just reading the different headers, and learning what they mean. You get an idea of what sites are located in certain IP's.
I haven't played with the script much, but I think it could be even more fun with some minor tweaks.

Right now it won't let me search more than 100 at a time before timing out. I haven't tried fixing that just yet.
Let me know if you have any hints/tips/fixes. I'd love to hear them.



Tuesday, November 13, 2012

Q. Why did the Geek cross the road?

A.  To get a better look at the Accessible Pedestrian Signals (APS).



Have you been at intersections and heard the push button talking to you (Wait. Wait. Wait.), or chirping like a manic sparrow?  These crossings have always peaked my interest. How do they work? Who makes them? Can I make the little speaker yell "RUUUUUUN!" at a specified time of my choosing?



These questions would pop in my head every time I drove by, so I decided it was time to do a little research.


First off, as far as I can tell, these 'talking' pedestrian signals are currently (2012) only being manufactured by one company. Yes, just one. So it would seem this is a fairly new technology.  The company behind this  genius tech is Polara Engineering out of Corona, California.  They create products used by many industries, including Traffic Management.

I was hoping, and happy to see that their website includes all the installation and operation manuals for their traffic products.  From here you can look at pages and pages of information.

I'm not going to go into great detail. You can read the manuals yourself, if you want to learn more.

Here is the Basic Setup:

1.   Their software communicates from their Central Control Unit to the individual intersection push buttons with digital data, over two low voltage wires. These wires also supply all of the power for the pushbuttons.

2. The Central Control Unit is installed in the Traffic Control Cabinet.
In my town, they are getting painted up by local artists.

3. The push buttons (EPBS) are installed on the traffic poles.


4. Run a system check to make sure everything can talk to each other.

5. Set preferences with E-Configurator.

6. Done!

 So what I'm interested in mainly, is the audio setup. This is part of the software, which is loaded in the Central Control Unit.

You can configure the setup using an E-Configurator. 


 From the manual:
"The E-Configurator is a battery operated hand-held device for making changes to the operating characteristics of Polara Navigator and EZ Communicator Push Button Stations (EPBS). 

It uses two-way infrared communication while held in front of a EPBS and pointed toward the red LED above the EPBS arrow button."









Sounds easy enough. Of course, it is password protected (factory default is AAAA).

Where could you find an E-configurator? Well, there are places, if you look around.




Using the Navigator Configurator (E-Config), you have access to a lot of setup options. As far as audio goes, you can change volume levels, and things like that. But what about a new audio file altogether?

Short answer.. No.

"Programming custom messages in the field requires a laptop, USB A/B cable and
the Polara EPBS Audio Programmer software. The software must be installed and
messages prepared prior to installation of the units on the intersection.

[...]
 if units are to be programmed in the field, a laptop with the proper software and voice messages must be prepared prior to installation on the intersection."

Polara has their own Youtube video that walks you through custom audio setup. But it's all done prior to installation.  I was disappointed by this, but reading the manuals was still pretty interesting.

 For example, the system can vary the volume of the audio based on how much ambient noise there is. And an 'extended push' option, where it will say additional information (street names), if the button is pushed and held longer.

Just know the internet is full of manuals. So if there is a gadget you have an interest in, it's worth taking a look.
 

Sunday, November 4, 2012

Playing board games online (against humans)

Register at YourTurnMyTurn.com

Several years ago I found this site yourturnmyturn.com, and I've recently jumped back into it. Instead of playing a whole game in one sitting, you and your opponent take turns, sometimes days at a time. After your opponent makes a play, you receive an email reminding you that it's now your turn.


They have about 40 games, including classics like checkers, chess, stratego and scrabble. And some more obscure and newer games.

It's great being able to play friends and family, even when we can't get to a computer at the same time.

Registration is free and easy. If you want to register, use this special link. It will give me referral points (doesn't do much, except maybe put a star by my name).  Once you register, go ahead and start a new game!