vishvananda/netns
GitHub: vishvananda/netns
Go 语言的网络命名空间操作库,提供创建、切换和恢复 Linux network namespace 的简洁接口。
Stars: 445 | Forks: 134
# netns - Go 中的 network namespaces
netns 包提供了一个极其简单的接口,用于在 Go 中处理
network namespaces。更改 namespaces 需要提升
权限,因此在大多数情况下,此代码需要以 root 身份运行。
## 本地构建和测试
你可以使用 go get 命令:
```
go get github.com/vishvananda/netns
```
测试(需要 root):
```
sudo -E go test github.com/vishvananda/netns
```
## 示例
```
package main
import (
"fmt"
"net"
"runtime"
"github.com/vishvananda/netns"
)
func main() {
// Lock the OS Thread so we don't accidentally switch namespaces
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Save the current network namespace
origns, _ := netns.Get()
defer origns.Close()
// Create a new network namespace
newns, _ := netns.New()
defer newns.Close()
// Do something with the network namespace
ifaces, _ := net.Interfaces()
fmt.Printf("Interfaces: %v\n", ifaces)
// Switch back to the original namespace
netns.Set(origns)
}
```
标签:EVTX分析, Go, Ruby工具, 容器网络, 开发库, 日志审计, 系统底层, 网络命名空间, 网络隔离