Dec 2016

Weird Code

Ever gone back through your own old scripts and programs, taken a look at something and wondered aloud what it was you were trying to do at the time? Happens to most system administrators probably once a while, developers probably a bit more frequently. Recently I had the displeasure of doing this myself and while lots of it was comical most of it was just tragic. It can't all be serious all the time so here is a look at some of the strangeness I cam across.

C: portlist iteration

In a scan program I have a predefined list of ports in case I don't want to do a range or a particular port. It is a list gathered up through experience and theft of other peoples top ports list . . . anyhoo . . . I had decided for some reason that I did not want to use a regular for loop to manage it and instead look for a terminating zero (like a lot of the glibc structs do):

    int x = -1;
    while (portlist[++x] != 0)
        printf("%i ", portlist[x]);

Yes it does actually work! So as if that isn't gross enough check this out:

  c = 0; /* it does not... DOES NOT.. hurt to recycle counters */
    /* base case of 0? then proceed to the next port on the list */
    while (portlist[c+1] != 0) {
        if (check_port(portlist[c])) {
            portinfo = getservbyport(htons(portlist[c]), "tcp");
            printf("%d (%s) open\n", portlist[c],(portinfo == NULL)
              ? "UNKNOWN" : portinfo->s_name);

            if (isup == 1)
                return 0;


Same idea but a lot of the work being done inside the loop. No idea why it went that.

bash: Creating a list from files

At some point in time at my job I had to do this:

  1. search for user in ldap and count entries with their userid
  2. if they are in ldap see if they are in the adfile too
  3. if they are in the adfile and ldap add to users to convert list
  4. otherwise add them to local ldap user list (which we don't use but...)
  5. if they didn't turn up in ldap see if they are in AD
  6. if they are in AD add them to users that need backfilled completely into ldap
  7. see if they have a local account (which would suck) only

That list is taken right out of the sript. This is the code that does it (prepare thyself):

for u in `cat $userlist` ; do
    cnt=`ldapsearch -h $srvip -p 389 -x\
        -b "dc=$dcstring"|grep -i $u |wc -l`

    if [ "$cnt" -gt 0 ] ; then
        ad_cnt=`grep -i $u $ADFILE|wc -l`
        if [ "$ad_cnt" -gt 0 ] ; then
            echo $u >> $ad_convert_file
            echo $u >> $local_ldap_file
        ad_cnt2=`grep -i $u $ADFILE|wc -l`
        if [ "$ad_cnt2" -gt 0 ] ; then
            echo $u >> $backfill_file

It is okay, you can look now. It is over. It actually does work too.

Perl: Yes I still use Perl

And I will until one of us croaks. Again, at my workplace, I had an interesting task: Do a fuzzy (as in as good as I can relative to what I had to work with) DNS inventory type of program. Note that DNS was maintained by someone else and it is inside Active Directory. This led to a lot of wried problems like missing reverse entries, duplicate entries and the occasional full on vanishing of stuff. The names of the variables in the query_dns_entries code pretty much explains what they are (in fact I posted this script here) but it is still mildly sketchy looking:

sub query_dns_entries {
    my $subnet = shift;
    my $i = 2; # 1 and 255 are reserved

    for (;$i < $MAXHOSTS; $i++) {
        my @resolver_string = get_entries("$subnet$i");
        if (@resolver_string) {
            if ($Iflag == 1) {
            my $n_entries = scalar(@resolver_string);
            if ($n_entries > 1) {

            foreach(@resolver_string) {
                if (m/dhcp/) {

            if ($Qflag == 0) {
                if ($CK_M > 0) {
                    if ($n_entries > 1) {
                        print "$subnet$i ";
                } else {
                    print "$subnet$i ";

Amazingly it is a pretty reliable script with about a 5-10% margin of error across 248 or so addresses. Note some of the weird stuff like the pattern match for dhcp that is because the dynamic DNS would dole out names like dhcp-IPADDRESS-with-frikn-Dashes. Sometimes we would fine IP addresses with names and dhcp entries. Good times.

CSS: Site Stylesheet, because yes

To this day the one bit of code (although I don't know if formatting is code or precode or what) that I am scared to death of changing is this sites style sheet. A long time ago I found this template, from somewhere, and spent many days and much coffee hammering away at it until it was where I thought it should be. Here is just a sample of the lunacy:

.colleft {
.col3 {
    padding:0 0 1em 0;
.col2, .col3 { text-align: center; }
.threecol {
.col2 {
    background: #e9e9e9;
    border-right: solid 1px #ccc;
    border-bottom: solid 1px #ddd;

.threecol .colmid {
    right:15%;  /* width of the right column */
.threecol .colleft {
    right:60%;  /* width of the middle column */
    /* left column background colour */
    /*background:#efefef;   */
.threecol .col1 {

And it just goes on for 250 more lines. Outside of the colors and some of the positioning I have no idea how it actually works. All I do know is it does and I hope that whatever CSS spec it is written to is supported until the end of time (or the world wide wait, whichever is first).