If the ip-range is in the format "10.43.8.64 - 10.43.8.79" everything is ok, I can turn the ips into longips and store them for later lookups in a mysql db.
But sometimes the whois lookup returns CIDR formated values like 10.43.8.67/28 and I don't know on which way I can calculate the dotted ip range. Could somebody help?
My code at the moment:
Code: Select all
if {[namespace exists ::ip2nation]} {namespace delete ::ip2nation}
namespace eval ::ip2nation {
namespace export dowhois
variable whoisbin [lindex [split [exec which whois]] 0]
variable version 0.1
variable rp; array set rp {
inetnum {(?ni)inetnum: (.*)}
country {(?ni)country: (.*)}
}
putlog "\00312$rp(inetnum)\003";
proc dowhois {uhost} {
if [regexp {[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$} $uhost] {
if {[catch {set lookup [eval exec $::ip2nation::whoisbin $uhost]} xError] != 0} {
return -code error "Error calling whois: ($::ip2nation::whoisbin $uhost): $xError"
}
foreach line [split [string trim [regsub -all {;(.+?)\n} $lookup {}]] \n] {
if {![string length $line]} {continue}
if [string match "*inetnum*" $line] {
regexp $::ip2nation::rp(inetnum) $line {} netblock
putlog "\00304netblock [string trim $netblock]\003";
if [string match "*/*" $rep] {
# here I am lost, I could start a perl script to calculate the ip but that's not what I want.
}
}
if [string match "*country*" $line] {
regexp $::ip2nation::rp(country) $line {} country
putlog "\00304country [string trim $country]\003";
}
}
} else {
dnslookup $uhost dns_test
}
}
proc dns_test {ip uhost status} {
dowhois $ip
}
}
package provide ip2nation $::ip2nation::version
putlog "\00304ip2nation.tcl v$::iptnation:version\003 "