#!/usr/bin/perl

# Persistant perl script to respond to pass-through smnp requests

# put the following in your snmpd.conf file to call this script:
#
# pass_persist .1.3.6.1.4.1.2021.255 /path/to/pass_persisttest

# Forces a buffer flush after every print
$|=1;

use strict;

my $counter = 0;
my $place = ".1.3.6.1.4.1.2021.255";

while (<>){
  if (m!^PING!){
    print "PONG\n";
    next;
  }

  my $cmd = $_;
  my $req = <>;
  my $ret;
  chomp($cmd);
  chomp($req);

  if ( $cmd eq "getnext" ) {
    if ($req eq $place) {
      $ret = "$place.1";
    } elsif ($req eq "$place.1") {
      $ret = "$place.2.1";
    } elsif ($req eq "$place.2.1") {
      $ret = "$place.2.2";
    } elsif ($req eq "$place.2.2") {
      $ret = "$place.3";
    } elsif ($req eq "$place.3") {
      $ret = "$place.4";
    } elsif ($req eq "$place.4") {
      $ret = "$place.5";
    } elsif ($req eq "$place.5") {
      $ret = "$place.6";
    } else {
      print "NONE\n";
      next;
    }
  } else {
    if ($req eq $place) {
      print "NONE\n";
      next;
    } else {
      $ret = $req;
    }
  }

  print "$ret\n";

  if ($ret eq "$place.1") {
    print "string\nlife the universe and everything\n";
  } elsif ($ret eq "$place.2.1") {
    print "integer\n423\n";
  } elsif ($ret eq "$place.2.2") {
    print "objectid\n.1.3.6.1.4.42.42.42\n";
  } elsif ($ret eq "$place.3") {
    print "timeticks\n363136200\n";
  } elsif ($ret eq "$place.4") {
    print "ipaddress\n127.0.0.1\n";
  } elsif ($ret eq "$place.5") {
    $counter++;
    print "counter\n$counter\n";
  } elsif ($ret eq "$place.6") {
    print "gauge\n42\n";
  } else {
    print  "string\nack... $ret $req\n";
  }
}
