source keyedListLibByReference.tcl ################################################################ # proc address2name {listName address}-- # resolve an IP address into a name and save it for future use # Arguments # listName The keyed list of IP addresses and site names # address The IP address to check # # Results # The list passed (by name) to this procedure may be modified. # proc address2name {listName address} { upvar $listName addrList # Try to convert the IP address to a name with the keyed list. set name [getValue $addrList $address] if {$name eq ""} { # If the IP address is not in the list, use nslookup to # find the name and save it. set txt [exec nslookup $address] set lst [split $txt] # Look for line like by searching for list element "name" # 50.53.114.66.in-addr.arpa name = ord-agg-n40.panthercdn.com. set pos [lsearch $lst name] if {$pos > 0} { # If "name" is found, the qualified name is the element after # the = - skip 2 to get the name. incr pos 2 set name [lindex $lst $pos] } else { # If $pos < 0, then there is no name identified, # use the IP address as the resolved name. set name $address } appendKeyedPair addrList $address $name } return $name } set if [open practice.txt r] set d [read $if] close $if set IPName {} foreach l [split $d \n] { set ip [lindex $l 5] if {$ip ne ""} { address2name IPName $ip } } set of [open IP2Name.txt w] foreach {ip nm} $IPName { puts $of [list $ip $nm] } close $of