August 2010
Monthly Archive
Tue 31 Aug 2010
Maybe you have a giant warehouse and you’d like some large LCD monitors around the shipping lines which have the orders listed for all to see. You’d like these LCDs to update frequently and to be specific to each shipping line.
What to do? Well, there is meta-refresh every second, but that’s flickery and lame. Let’s get the javascript prototype library to update divs based on the location selected in a dropdown list:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>LCD Terminal Somewhere</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script>
<body>
<script>
var url = 'get_content.php';
var pars = 'location=1';
var myAjax = new Ajax.PeriodicalUpdater(
'placeholder',
url,
{
method: 'post', //use post or IE will cache(with get)
parameters: pars,
frequency: 2
});
</script>
</head>
<body>
<h2>Terminal Display</h2><i>(content refreshes every two seconds)</i><br />
<div id="placeholder" style="width:600px; height: 300px; border-width: .1em;border-style: solid;border-color: #000;">Lorum Ipsum</div>
<p><p><p>
<form>
<label for="myLocation">Change Location: </label><select name="myLocation" id="myLocation" onchange="pars='location='+this.options[this.selectedIndex].value; myAjax.options.parameters = pars.toQueryParams();">
<option value="1">Line 1</option>
<option value="2">Lines 2 and 3</option>
<option value="3">Reception</option>
<option value="4">Break Room</option>
</select>
</form>
</body>
</html>
And here is get_content.php
<?php
switch($_REQUEST['location'])
{
case 1:
echo("<h2>Some custom data for Line 1</h2>");
break;
case 2:
echo("<h2>Custom datagrid for Lines 2 and 3</h2>");
break;
case 3:
echo("<h2>This would be displayed on the receptionist LCD</h2>");
break;
case 4:
echo("<h2>Content for the break room</h2>");
break;
}
print("<p><p><div align=\"right\">Last Updated: ");
print strftime('%c');
print("</div>");
Wed 25 Aug 2010
here’s a handy utility script so you can check pings and DNS from a remote server though a web browser
<html>
<body>
<form action="ping.php">
IP address<input type="input" name="ipaddr" />
or
hostname<input type="input" name="host" />
<input type="submit" />
</form>
<?php
function ping($host) {
exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, $rval);
return $res;
}
if (isset($_REQUEST['ipaddr'])&&$_REQUEST['ipaddr']!='')
{
if(preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/",$_REQUEST['ipaddr']))
{
echo("<pre>");print_r(ping($_REQUEST['ipaddr']));echo("</pre>");
}
else { echo("ip address not valid");}
}
if (isset($_REQUEST['host'])&&$_REQUEST['host']!='')
{
if (preg_match('/^([a-z0-9][a-z0-9-]{0,62}\.)+([a-z]{2,4})$/',$_REQUEST['host']))
{
$result = dns_get_record($_REQUEST['host']);
echo("<pre>");print_r($result);echo("</pre>");
}
else { echo("host name not valid");}
}
?>
Mon 16 Aug 2010
Here’s a quick script I coded this morning to check a URL/URLs for changes and alert you
#!/bin/bash
HASHFILE="hashes.txt"
URLFILE="urls.txt"
EMAILADDRESS="name@example.com"
if [ -z "$1" ]
then
echo "Usage: `basename $0` [TRAIN|CHECK]"
echo "Create a file called urls.txt with URLS to check"
exit 1
fi
if [[ "${1}" == "CHECK" ]]
then
echo "CHECKING"
if [ -f $HASHFILE ]
then
echo "Hash file exists. Parsing"
while read line; do
URL=`echo $line |awk '{print $1}'`
HASH=`echo $line |awk '{print $2}'`
echo "Checking $URL"
wget $URL >& /dev/null
FILENAME=`basename $URL`
HASHVALUE=`md5sum $FILENAME|awk '{print $1}'`
if [[ "${HASHVALUE}" == "${HASH}" ]]; then
echo "NOTICE: hasn't changed"
else
echo "ALERT: [$URL] CHANGED from [$HASH] to [$HASHVALUE]"
PREFIX="BLOCK_"
BLOCKFILE=$PREFIX$FILENAME
if [ -f $BLOCKFILE ]
then
echo "BLOCK FILE EXISTS. NOT EMAILING"
else
echo "NO BLOCK FILE, EMAILING ALERT. WRITING BLOCK FILE"
mail -s "ALERT: [$URL] CHANGED from [$HASH] to [$HASHVALUE]" $EMAILADDRESS </dev/null
echo "$URL changed. alerted" > $BLOCKFILE
fi
fi
rm -f $FILENAME
done < $HASHFILE
else
echo "ERROR: no hash file exists. Exiting"
exit 1
fi
else
echo "TRAINING"
if [ -f $URLFILE ]
then
echo "URL file exists. Parsing"
rm -f $HASHFILE
touch $HASHFILE
while read line; do
wget $line >& /dev/null
FILENAME=`basename $line`
HASHVALUE=`md5sum $FILENAME|awk '{print $1}'`
echo "$line $HASHVALUE" >> $HASHFILE
echo "added $line to hash file"
rm -f $FILENAME
done < $URLFILE
else
echo "ERROR: no url file exists. Exiting"
exit 1
fi
fi
Tue 3 Aug 2010
You’d like to email someone seven times, every other day starting today. How do you get those dates?
function calculateIntervalDates($startDate, $interval, $count)
{
$intervalArray=array('Daily'=>1,'Every-other-day'=>2,'Weekly'=>7, 'Twice-monthly'=>15, 'Monthly'=>30);
$daysToAdd=$intervalArray[$interval];
for($x=0;$x<$count;$x++)
{
$intervalDateArray[]= date("m/d/Y", strtotime("$startDate + ".$daysToAdd*$x." days"));
}
return $intervalDateArray;
}
print_r(calculateIntervalDates("08/03/2010",'Every-other-day',7));
which outputs
Array
(
[0] => 08/03/2010
[1] => 08/05/2010
[2] => 08/07/2010
[3] => 08/09/2010
[4] => 08/11/2010
[5] => 08/13/2010
[6] => 08/15/2010
)
I recognize the monthly is an approximate and that I could do better by having a special case for that, but, it’s close enough.
Mon 2 Aug 2010
If you Google “setting the Selenium User-Agent header” you get a whole bunch of crapola. Specifically, Selenium folks want you to use web drivers, which is stupid because the whole point is that you want to be able to record tests in the Selenium Firefox plugin and then run them with Selenium RC. (setting the user-agent is useful when trying to filter out tests from live traffic)
Anyway, the solution is to create a Firefox profile with the user-agent string already set. Be sure to modify the profile to clear cookies and cache on exit, to not prompt for updates or add-ons etc.
First, start Firefox in profile editor mode:
firefox.exe -ProfileManager -no-remote
Go ahead and create a new profile and then start Firefox with that profile and make you customizations (set the user-agent, etc). Then launch selenium like this:
java -jar selenium-server.jar -firefoxProfileTemplate "your profile directory goes here"
Here is some test code to see if you’ve succeeded:
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
class testuagent extends PHPUnit_Extensions_SeleniumTestCase
{
function setUp()
{
$this->setBrowser("*firefox");
$this->setBrowserUrl("http://www.iopus.com/");
}
public function testMyTestCase()
{
$this->open("/imacros/demo/v5/user-agent.htm");
$this->waitForPageToLoad("30000");
sleep(20);
}
}
useful links:
http://johnbokma.com/mexit/2004/04/24/changinguseragent.html
http://girliemangalo.wordpress.com/2009/02/05/creating-firefox-profile-for-your-selenium-rc-tests/
http://www.iopus.com/imacros/demo/v5/user-agent.htm
http://support.mozilla.com/en-US/kb/How+to+clear+the+cache