-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
) | ||
|
||
func main() { | ||
netInterfaces, err := net.Interfaces() | ||
if err != nil { | ||
fmt.Printf("fail to get net interfaces: %v", err) | ||
} | ||
|
||
for _, netInterface := range netInterfaces { | ||
interfaceName := netInterface.Name | ||
inter, err := net.InterfaceByName(interfaceName) | ||
if err != nil { | ||
log.Fatalf("无法获取信息: %v", err) | ||
} | ||
|
||
addrs, err := inter.Addrs() | ||
|
||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
// 获取IP地址,子网掩码 | ||
for _, addr := range addrs { | ||
if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() { | ||
if ip.IP.To4() != nil { | ||
fmt.Println("Name:", interfaceName) | ||
fmt.Println("IP:", ip.IP) | ||
fmt.Println("Mask:", ip.Mask) | ||
fmt.Println("Mac:", inter.HardwareAddr.String()) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/google/gopacket/pcap" | ||
"log" | ||
) | ||
|
||
func main() { | ||
// Find all devices | ||
devices, err := pcap.FindAllDevs() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Print device information | ||
fmt.Println("Devices found:") | ||
for _, d := range devices { | ||
for _, address := range d.Addresses { | ||
ip := address.IP | ||
if ip.To4() != nil && !ip.IsLoopback() { | ||
fmt.Println("Name: ", d.Name) | ||
fmt.Println("Description: ", d.Description) | ||
fmt.Println("Devices addresses: ", d.Description) | ||
fmt.Println("- IP address: ", address.IP) | ||
fmt.Println("- Subnet mask: ", address.Netmask) | ||
} | ||
} | ||
} | ||
} |