Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Wednesday, August 6, 2008

Perl CGI cached parameters.

An intern at work today ran into a problem modifying a perl cgi script. It looked something like this


my $cgi = new CGI;
$cgi->param('foo');


The problem was this value appeared to be cached. Once I accesed the page with http://server/page?foo=bar it would remember bar no matter what I changed the value to.

First I thought it was a cache issue. So we changed the code to output a timestamp. On each page load we would see the time increment but the variable was still cached somehow.

We finally found out that putting the new CGI call inside a function made sure we got a new one each time.

So what happened? I'm not really sure, I'm guessing scope of the perl code is global per apache process? Is this configurable somehow?

Well if anyone knows please add a comment because i this point I'm still baffled by it.

Thursday, June 26, 2008

Using java as a shell language

I've been trying to find what shell language I like better as the 'glue' needed between real applications. I've used perl a lot in the past, experimenting with python and ruby now. But I don't want an interpreted language. I really want a compile time language.

So today I figured why not use java.

The main problem is that most scripts are easy to modify and don't require compiling. For example you start your python script with

#!/usr/bin/python

It would be nice if I could do the same for Java. Well now you can.

Introducing the 'JavaLoader'


import java.io.*;
class JavaLoader {
public static void main(String[] args) throws Exception {
String pwd = System.getProperty("user.dir");
System.out.println( "Received argument " + args[0]);
String javaFile = args[0];
if (javaFile.startsWith("./"))
javaFile = javaFile.replace("./","");

// Find class file
String className = (javaFile.split("\\."))[0];

// Comment out the shebang line so we can compile it.
RandomAccessFile file = new RandomAccessFile(javaFile, "rw");
file.seek(0);
file.write("//".getBytes());
file.close();

// Compile it
executeCommand(String.format("javac -cp $CLASSPATH:%s %s",pwd,javaFile));

//Run
executeCommand(String.format("java -cp $CLASSPATH:%s %s",pwd,className));

// Put back #! line.
file = new RandomAccessFile(javaFile, "rw");
file.seek(0);
file.write("#!".getBytes());
file.close();
}
public static void executeCommand(String cmd) throws Exception {
System.out.println("Executing command: [" + cmd + "]");
Process p = Runtime.getRuntime().exec(cmd);
printStream(p.getInputStream());
printStream(p.getErrorStream());
}
public static void printStream(InputStream is) throws Exception{
BufferedReader input = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
}
}


By using this you can now write a script like so:


#!/usr/bin/java JavaLoader

class my {

public static void main(String[] args) {
System.out.println ("my: from here I am in java");
}
}


To execute you first need to make sure the JavaLoader is compiled and in your classpath. Then execute your script. My script was called my.java so I did:


[root@pioneer local]# ./my.java
Received argument ./my.java
Executing command: [javac -cp $CLASSPATH:/usr/local my.java]
Executing command: [java -cp $CLASSPATH:/usr/local my]
my: from here I am in java


This is great no more settling, I get to use Java everywhere now!

Wednesday, June 25, 2008

Enumerate all hosts in a dns domain using Perl

I had a need to search for a hostname at my company. I found the Net::DNS perl module to be useful. Here is an example of enumerating all hosts for a domain.


use strict;
use Net::DNS;

my $res = Net::DNS::Resolver->new;
$res->nameservers("nameserver ip address");

my @zone = $res->axfr("example.com");

foreach my $rr (@zone) {
print $rr->print;