1if {$::tcl_platform(platform) eq "windows" && ![catch {package require twapi}]} { 2# TWAPI Based implementation 3 4::namespace eval ::nettool {} 5 6### 7# topic: 825cd25953c2cc896a96006b7f454e00 8# title: Return pairings of MAC numbers to IP addresses on the local network 9# description: Under macosx, we call the arp command for arp table resolution 10### 11proc ::nettool::arp_table {} { 12 set result {} 13 catch { 14 foreach element [::twapi::get_arp_table] { 15 foreach {ifidx macid ipaddr type} { 16 lappend result [string map {- :} $macid] $ipaddr 17 } 18 } 19 } 20 return $result 21} 22 23 24### 25# topic: 57fdc331bc60c7bf2bd3f3214e9a906f 26### 27proc ::nettool::hwaddr_to_ipaddr args { 28 return [::twapi::hwaddr_to_ipaddr {*}$args] 29} 30 31 32 33if {[info command ::twapi::get_netif_indices] ne {}} { 34### 35# topic: 4b87d977492bd10802bfc0327cd07ac2 36# title: Return list of network interfaces 37### 38proc ::nettool::if_list {} { 39 return [::twapi::get_netif_indices] 40} 41 42 43### 44# topic: ac9d6815d47f60d45930f0c8c8ae8f16 45# title: Return list of mac numbers for this computer (primary first) 46### 47proc ::nettool::mac_list {} { 48 set result {} 49 foreach iface [::twapi::get_netif_indices] { 50 foreach {field value} [::twapi::get_netif_info $iface -physicaladdress] { 51 if { $value eq {} } continue 52 lappend result [string map {- :} $value] 53 } 54 } 55 return $result 56} 57 58### 59# topic: a43b6f42141820e0ba1094840d0f6fc0 60### 61proc ::nettool::network_list {} { 62 set result {} 63 foreach iface [::twapi::get_netif_indices] { 64 set dat [::twapi::GetIpAddrTable $iface] 65 foreach element $dat { 66 foreach {addr ifindx netmask broadcast reamsize} $element break; 67 set mask [::ip::maskToInt $netmask] 68 set addri [::ip::toInteger $addr] 69 lappend result [ip::nativeToPrefix [list [expr {$addri & $mask}] $netmask] -ipv4] 70 } 71 } 72 return [lsort -unique $result] 73} 74} else { 75 76if {[info commands ::twapi::get_network_adapters] ne {}} { 77proc ::nettool::if_list {} { 78 return [::twapi::get_network_adapters] 79} 80} 81 82if {[info commands ::twapi::get_network_adapter_info] ne {}} { 83proc ::nettool::mac_list {} { 84 85 set result {} 86 foreach iface [if_list] { 87 set dat [::twapi::get_network_adapter_info $iface -physicaladdress] 88 set addr [string map {- :} [lindex $dat 1]] 89 if {[string length $addr] eq 0} continue 90 if {[string range $addr 0 5] eq "00:00:"} continue 91 lappend result $addr 92 } 93 return $result 94} 95 96proc ::nettool::network_list {} { 97 set result {} 98 foreach iface [if_list] { 99 set dat [::twapi::get_network_adapter_info $iface -prefixes] 100 foreach kvlist [lindex $dat 1] { 101 if {![dict exists $kvlist -address]} continue 102 if {![dict exists $kvlist -prefixlength]} continue 103 set length [dict get $kvlist -prefixlength] 104 if {$length>31} continue 105 set address [dict get $kvlist -address] 106 if {[string range $address 0 1] eq "ff"} continue 107 lappend result $address/$length 108 } 109 } 110 return [lsort -unique $result] 111} 112 113} 114} 115 116### 117# topic: 417672d3f31b80d749588365af88baf6 118# title: Return list of ip addresses for this computer (primary first) 119### 120set body {} 121if {[info commands ::twapi::get_ip_addresses] ne {}} { 122proc ::nettool::ip_list {} { 123 set result [::twapi::get_ip_addresses] 124 ldelete result 127.0.0.1 125 return $result 126} 127} elseif {[info commands ::twapi::get_system_ipaddrs] ne {}} { 128# They changed commands names on me... 129if {[catch {::twapi::get_system_ipaddrs -version 4}]} { 130# THEY CHANGED THE API ON ME! 131proc ::nettool::ip_list {} { 132 set result [::twapi::get_system_ipaddrs -ipversion 4] 133 ldelete result 127.0.0.1 134 return $result 135} 136} else { 137proc ::nettool::ip_list {} { 138 set result [::twapi::get_system_ipaddrs -version 4] 139 ldelete result 127.0.0.1 140 return $result 141} 142} 143} 144 145 146proc ::nettool::status {} { 147 set result {} 148 #dict set result load [::twapi::] 149 set cpus [::twapi::get_processor_count] 150 set usage 0 151 for {set p 0} {$p < $cpus} {incr p} { 152 if [catch { 153 set pu [lindex [::twapi::get_processor_info $p -processorutilization] 1] 154 while {$pu eq {}} { 155 after 100 {set pause 0} 156 vwait pause 157 set pu [lindex [::twapi::get_processor_info $p -processorutilization] 1] 158 } 159 set usage [expr {$usage+$pu}] 160 } err] { 161 set usage -1 162 } 163 } 164 dict set result cpus $cpus 165 dict set result load [expr {$usage/$cpus}] 166 dict set result uptime [::twapi::get_system_uptime] 167} 168} 169