lxz 1 week ago
parent
commit
38df617308
2 changed files with 44 additions and 22 deletions
  1. 13 22
      demo3.7/main.go
  2. 31 0
      network/netns/main.go

+ 13 - 22
demo3.7/main.go

@@ -1,30 +1,21 @@
 package main
 
 import (
+	"encoding/json"
 	"fmt"
-	"strings"
 )
 
 func main() {
-	// 原始脚本内容
-	script := `#!ipxe
-dhcp net2
-
-set base-url    centos-7.9.2009/image #请替换为镜像所在的HTTP文件服务器地址
-set ks-url   centos-7.9.2009   # 替换为你的 Kickstart 文件所在的 HTTP 服务器地址
-set ks-file centos-7.9.2009.cfg
-
-kernel ${base-url}/images/pxeboot/vmlinuz initrd=initrd.img inst.repo=${base-url} ip=dhcp ks=${ks-url}/${ks-file}  # 指定 Kickstart 文件的位置和参数
-initrd ${base-url}/images/pxeboot/initrd.img
-boot`
-
-	// 新的base-url和ks-url
-	newBaseURL := "http://192.168.16.121:8000/images/centos-7.9.2009/image/"
-	newKSURL := "http://192.168.16.121:8000/images/centos-7.9.2009/"
-
-	// 替换原始脚本中的set base-url和set ks-url
-	script = strings.Replace(script, "set base-url    ", fmt.Sprintf("set base-url    %s", newBaseURL), 1)
-	script = strings.Replace(script, "set ks-url   ", fmt.Sprintf("set ks-url   %s", newKSURL), 1)
-
-	fmt.Println(script)
+	test := []int{1, 2, 3, 4, 5}
+	marshal, err := json.Marshal(test)
+	if err != nil {
+		panic(err)
+	}
+	fmt.Println(string(marshal))
+	var unMarshalled []int
+	err = json.Unmarshal(marshal, &unMarshalled)
+	if err != nil {
+		panic(err)
+	}
+	fmt.Println(unMarshalled)
 }

+ 31 - 0
network/netns/main.go

@@ -0,0 +1,31 @@
+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
+	netns.Set
+	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)
+}