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 IPList {} foreach ip {66.114.53.42 72.233.104.123 216.176.180.53 204.176.49.116} { address2name IPList $ip } foreach {ip addr} $IPList { puts "$ip -> $addr" }