Tuesday, August 19, 2008

DNS Resolver

I had a list of domains that needed their MX records updated. I first wanted to make sure that our DNS servers were authoritative for those zones. I used Perl and the Net::DNS CPAN module to figure out the NS records for each domain.
#!/usr/bin/perl

use Net::DNS;

my $resolver = Net::DNS::Resolver->new;
my $query = "";
my $domain = "";

open(FILE, "</tmp/domains.txt");
while() {
  chomp;
  $domain = $_;
  $query = $resolver->query($domain, "NS");
  print $domain;
  if($query) {
    foreach my $rr ($query->answer) {
      next unless $rr->type eq "NS";
      print ",", $rr->nsdname;
    }
    print "\n";
  } else {
    print ",DOES NOT RESOLVE\n";
  }
}

Labels: ,

Wednesday, August 13, 2008

Tcl version of ucfirst

The load balancers we use at work have a TCL command line interface. A co-worker needed a TCL function written that would take a string as input and return the string transformed so that the first letter was upper case and the rest were lower case. There is a built in string function called toupper but that capitalizes the full string instead of only the first.

My solution (shown below) is to split the string into two strings, toupper the first string and tolower the second, then append them together. The result is the equivalent of Perl's ucfirst implemented in TCL.

proc ucfirst str {
 set a [ string toupper [ string range $str 0 0 ]]
 append a [ string tolower [ string range $str 1 end ]]
 return $a
}
You can use this from the command line by using tclsh. Below is what running this code will look like (the % is the tclsh prompt).

[jasonn@jnoble-vm ~]$ tclsh
% proc ucfirst str {
 set a [ string toupper [ string range $str 0 0 ]]
 append a [ string tolower [ string range $str 1 end ]]
 return $a
}
%
% set mystring "firetruck"
firetruck
% set t [ ucfirst $mystring ]
Firetruck
% set mystring "cop car"
cop car
% set t [ ucfirst $mystring ]
Cop car
% exit
[jasonn@jnoble-vm ~]$

Labels: , ,