#!/usr/bin/tclsh ###################################################################### # Quick tcl script to pull pound entries for syslog files and strip # # any extra fluff from the start # # Mat Kovach mkovach at alal dot com # ###################################################################### package require dns proc err { reason } { puts stderr $reason } proc process_pound { line } { set split_line [split $line] if {[lindex $split_line 4] == "pound:"} { if {[regexp -nocase {^[a-z0-9]} [lindex $split_line 5] ]} { if { [regexp "(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})" [lindex $split_line 6] all first second third fourth] } { set dns_tok [::dns::resolve $all -nameserver 127.0.0.1] if {[::dns::status $dns_tok] == "ok"} { lset split_line 6 [::dns::name $dns_tok] } catch {::dns::cleanup $dns_tok} } puts [join [lrange $split_line 5 end]] } } } proc read_file { fp func } { while {[gets $fp line] >= 0} { $func $line } close $fp } proc check_file { filename } { set fp -1 if {[catch {open $filename "r"} fp]} { err "Error reading $filename, $fp. Skipping file" } return $fp } proc file_list { files } { foreach file $files { if { [file isfile $file] && [file readable $file] } { set fp [ check_file $file ] if { $fp != -1 } { read_file $fp process_pound } } else { err "File is not readable or not a file, skipping $file" } } } proc main { argv } { if { [llength $argv] == 0 } { read_file "stdin" process_pound } else { file_list $argv } } main $argv