Tuesday, October 27, 2009

Grading Packet Tracer activity files from Moodle

I love Cisco's Packet Tracer. One of my favorite activities consists of building a completely operational network, then causing a random fault somewhere and leaving students with the broken-down network to figure the problem out. These kind of activities put a lot of skills to the test: you need to understand the network, know which tools to use, emit a valid diagnostic and find a proper solution that doesn't break something else. In fact, I'd only need to page my students at 3am to make it look like a real life problem :)

With Packet Tracer's "Activity Wizard", you can even automate the grading of these kind of exercises: you just create an answer network (the student's target) and an initial network (the faulty one). Packet Tracer will figure out what changed between both scenarios and grade each of the steps required to move between one another. A lot of things can be graded: setting the correct netmask, using the right cable, issuing any IOS command, etc. Students start off from 0% and get to 100% when they reach the answer network. This makes life extraordinarily easier for the teacher. Creating the initial and answer scenarios takes quite a bit, but it's definitely worth the effort.

If you use Moodle, you can propose new activities, let the students work on them, and after they're done, you just open the result PKA file to check their score. I normally grade PKAs as "completed", "incomplete" or "not handed in". Only when they're incomplete do I make some comment, usually with a hint to what's going wrong.

Now, wouldn't be great if Moodle could put the PKA score out to the gradebook automatically? That way you don't even have to download the "completed" activities. Moodle grades them for you.

Well... this is the closest I've gotten to it. The key to this solution is a .jar that comes with Packet Tracer. This .jar has a bunch of libraries that allow PT to be used as a back-end, issue commands, query the status of any device and, among other useful things, obtain the studen't score.

You need:
  1. Packet Tracer 5.2. I'm not sure if this file was in5.0 or 5.1, so just get 5.2 (it fixes quite a few bugs anyway). I tried this with the Linux version. Look for the "ptaplayer-011.01.jar" under /usr/local/PacketTracer5/extensions/ptaplayer/.
  2. A Java compiler. In Ubuntu, "apt-get install openjdk-6-jdk" or "sun-java6-jdk" will do.
Get this code and paste it in a file named "Grade.java":
import apiipc.generated.system.*;
import java.io.File;
import pse.pt.PacketTracer;

class Grade
public static void main(String args[])
throws Exception
File file;
PacketTracer packettracer;
if (args.length != 1) {
System.err.println("You need to specify a pka file to grade");
file = new File(args[0]);
packettracer = new PacketTracer("localhost", 39000, true);

ActivityFile activityfile = packettracer.getActivityFile();
Double s = activityfile.getPercentageComplete();

System.out.println((new StringBuilder()).append(s).append("\t").append(args[0]).toString());


Compile it with:
$ javac -classpath ptaplayer-011.01.jar Grade.java

Now get some PKA file and try to grade it!
$ java -classpath ./ptaplayer-011.01.jar:. Grade

Yes, that "100.0" indicates the score is 100% :)

You might want to wrap the ugly java command line into a simpler script. One thing I noticed is that Packet Tracer opens a window even though it's running in "no-gui" mode. No big deal on an X desktop (the window will pop up briefly and then disappear), but it makes the whole thing totally inadequate for headless servers without X, since Packet Tracer does need X. As a workaround, you can use xvfb. Like this:

sd=$(dirname $0)
xvfb-run java -classpath "$sd"/ptaplayer-011.01.jar:"$sd" Grade "$1"
sleep 1 # Give xvfb time to die

That's about it. Put this, Grade.class and the .jar file in ~/pkagrader and just do ~/pkagrader/grade to get the score.

When you have a bunch of PKAs, just do this to grade them all:
$ for i in *; do ~/pkagrader/grade "$i"; done

A few challenges still remain...
  1. As of 1.9.5+, Moodle doesn't support downloading all the assignment files at once. You have to go one by one. Fortunately this patch will add this feature. It applies cleanly on a 1.9.5+ install. This is the jira issue about the issue.
  2. The ultimate solution would be creating a new type of activity for Moodle, something like "Packet Tracer activity" and having the grader fire up automatically after a student uploads a .pka. Anyone interested?