This is a creation in Article, where the information may have evolved or changed.
Before the use of a cool app, called Fing, it can get to the local LAN all the device's IP, MAC address and device manufacturers. Always feel very good, today think about it, found that there is not so much mystery.
The IP in the poor lift LAN
LAN IP generally has 192
and 10
two forms. In general, the 10
beginning of the LAN high-end some, can accommodate more devices.
Exhaustive all IP, one way is through trancert
the command, record access to a site through the path, then the first path is to access the router, get the router IP, the last part of the poor lifting.
However, this method is a bit cumbersome, to simplify the point is to obtain the current device in the LAN IP, with this IP for poor lifting. The method of obtaining IP via Golang can be referenced in [1]. The way to get the current device IP can be referred to:
func ExternalIP() (string, string, error) {ifaces, err := net.Interfaces()if err != nil {return "", "", err}for _, iface := range ifaces {if iface.Flags&net.FlagUp == 0 {continue // interface down}if iface.Flags&net.FlagLoopback != 0 {continue // loopback interface}addrs, err := iface.Addrs()if err != nil {return "", "", err}for _, addr := range addrs {var ip net.IPswitch v := addr.(type) {case *net.IPNet:ip = v.IPcase *net.IPAddr:ip = v.IP}if ip == nil || ip.IsLoopback() {continue}ip = ip.To4()if ip == nil {continue // not an ipv4 address}return ip.String(), iface.HardwareAddr.String(), nil}}return "", "", errors.New("are you connected to the network?")}
Ping the exhaustive IP to get the MAC address.
Ping the exhaustive IP address, get the IP of the online device, and then arp
get the MAC address of the device via the protocol. arp
protocol is the protocol used to convert IP into Mac. These two steps can be used in GitHub open source github.com/j-keck/arping
package Auxiliary implementation, this package of documentation can refer to [2]. Simple to use as follows:
func Mac(ip string) (net.HardwareAddr, time.Duration, error) {dstIP := net.ParseIP(ip)return arping.Ping(dstIP)}
Get the equipment of the manufacturer, which is commonly known as vendor
This step is the tallest. I always thought that the vendor of the device, like the HTTP protocol UserAgent
, could be obtained through this thing. So there's a protocol that can be used to get these things. But I couldn't find it. Later found that Baidu know (did not expect this thing still a bit of use) inside a big brother wrote.
MAC
The address is an international organization (like the IEEE), which ensures that each NIC has a different MAC
address. And it is not randomly allocated when assigning these addresses. The MAC
address has 6 bytes, it is simple to use the first three bytes to represent the vendor number, and the last 3 bytes to differentiate the vendor's network card number. So, we can get the device through the first 3 bytes Vendor
.
On the IEEE Web site, provides a way to query, I took that interface out, is to http://standards.ieee.org/cgi-bin/ouisearch The
sends a form
request for a post
. Also to parse the resulting HTML page, you get the vendor
.
func Vendor (Mac String) (string, error) {macs: = strings. Split (Mac, ":") If Len (MACS)! = 6 {return "", FMT. Errorf ("Mac Error:%s", Mac)}mac = strings. Join (Macs[0:3], "-") Form: = URL. Values{}form. ADD ("X", Mac) Form. ADD ("Submit2", "search!") Res, err: = Goreq. Request{method: "POST", Uri: "Http://standards.ieee.org/cgi-bin/ouisearch", ContentType: "Application/x-www-for M-urlencoded ", useragent:" Cyeam ", Showdebug:true,body:form. Encode (),}. Do () if err! = Nil {return "", err}body, err: = Res. Body.tostring () if err! = Nil {return "", Err}vendor: = Body[strings. Index (Body, strings. ToUpper (MAC)) +len (MAC):]vendor = strings. Trimleft (Vendor, "</b> (hex)") Vendor = strings. Trimspace (vendor) return strings. Split (Vendor, "\ n") [0], nil}
HTTP requests are goreq
sent by, form request application/x-www-form-urlencoded
format, and also need to be used by the net/url
package.
Results
Finally put the results of the operation:
The first one is the device I'm currently running, the NIC is Intel, and the rest is the router and the phone.
I will encapsulate the complete functions described above, please refer to the complete source code in this article.
Reference documents
- How does I get the local IP address in Go? -StackOverflow
- Package Arping-godoc
- Go how to read MAC address or hard disk Id-golang China
- How to query the manufacturer and model of network equipment by MAC address-Baidu knows
Original link: Detection of local area network equipment, reproduced please indicate the source!