May 25, 2010

Schlepping the FW Log, Part V

Posted in Uncategorized at 12:14 pm by dgcombs

I’ve been pulling together scripts to continually check the local network for bot-like behavior (PDF). You know, like waving arms, moving your head up and down and saying things like, “Danger, Will Robinson! Danger!” But due to the underlying architecture I picked, I’ve had to use several different scripting languages to achieve that goal:

  • MongoDB – JavaScript (or ECMAScript) – The default version in MongoDB is SpiderMonkey, Mozilla’s C implementation of JavaScript.
  • ResinJava Server Pages (or JSP) which looks a lot like Java once you peel back the first layer of the onion, or parfait.
  • SwatchPerl which is just as “feature rich” (read that “confusing”) as Java.
  • PyMongoPython as a general scripting interface for MongoDB
I had to admit, this was getting a little too complex. Even for me. Now, I could have swapped out PHP for the JSP but that’s another kettle of fish altogether. Looking at the list, I figured the only one I couldn’t change would be JavaScript. It is embedded into MongoDB. Since it is embedded in MongoDB, why could I not use MongoDB as my general purpose scripting language? In fact, could I not use JavaScript as a CGI-like replacement for the JSP pages? After all, most of my development time and energy was spent figuring out inheritance hierarchy and translating from one object oriented syntax to another.
I floated this idea on the Mongo Google Group and got an encouraging answer:

Haven’t tried it, but it sounds just crazy enough to work. Would be
interested to hear how this turns out…

So I penned as simple a script as I could:


echo “Howdy, World!”

It failed. Immediately. Miserably. Couldn’t even load the script.

Tue May 25 11:07:21 JS Error: SyntaxError: illegal character ./howdy.js:0

failed to load: ./howdy.js

And further I noticed that the error message indicated an illegal character in line 0 (zero). A tiny bit of sleuthing later, and it turns out that JavaScript doesn’t like the first line. At all. The first line contains the well known Linux/Unix shell scripting syntax called a “shebang” or “crunchbang.” In many Linux/Unix scripting languages, like BASH, PERL or even Python, if the hash symbol, “#” is the first character, that line is ignored. The combination of the hash mark and exlcamation point is used by convention to tell the Linux/Unix operating system what program should be used to execute the lines that follow. But JavaScript took one quick look at it and spit it right out.
But hey! MongoDB is Open Source, right? And so is the JavaScript version they’re using! So I combed through the source code for SpiderMonkey and MongoDB for a while. Things began to look increasingly bleak. SpiderMonkey’s code is not conducive to a quick and dirty modification to something so basic as single line comment. However, I found that while MongoDB uses SpiderMonkey, development is underway for Google’s V8. So I checked out the V8 code. It’s way simpler to add a new comment structure there.
I tucked the following code into the V8 source:

case ‘#’:
// crunchbang/shebang #!
if (c0_ == ‘!’) {
token = SkipSingleLineComment();

I recompiled MongoDB using V8 as the JavaScript interpreter. Then I modified my script to point to the new version of MongoDB and voila!

Howdy, World!

MongoDB has a commandline option –quiet which keeps MongoDB from printing its startup information. This is necessary to avoid problems if you are using JavaScript as a CGI to replace JSP. According to our friends over at Caucho, the simplet CGI script is the following:

1 #!/bin/sh
echo "Content-Type: text/html"
echo "<h>CGI Test<h1>"
echo "CGI Test"

The first line tells the operating system what program runs the following script. The second line tells the web browser what kind of file is coming. The third line must be blank. If you don’t follow these rules, the browser can hang giving you the impression that your script didn’t execute properly. It didn’t.

My version of this was

print(“Content-Type: text/html”);
print(“Howdy, World!”);

It worked. I am now simplifying my life with shell scripts and CGI components written in JavaScript. The first production page displays the count and IP addresses of internal systems which have been using Internet Relay Chat connections.
I only found six so far this week.

Posted via email from Meyeview


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: