test icmp

This commit is contained in:
wencaiwulue
2021-08-15 20:51:02 +08:00
parent 766d5143d1
commit 22f1715dda

View File

@@ -4,6 +4,7 @@ import (
"context"
"crypto/md5"
"fmt"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
net2 "k8s.io/apimachinery/pkg/util/net"
"k8s.io/client-go/kubernetes"
@@ -14,6 +15,7 @@ import (
"os/exec"
"path/filepath"
"testing"
"time"
)
var (
@@ -31,7 +33,9 @@ var (
func TestCidr(t *testing.T) {
cidr, err := getCIDR(clientsets, namespaces)
if err == nil {
fmt.Println(cidr.String())
for _, ipNet := range cidr {
fmt.Println(ipNet)
}
}
}
@@ -61,3 +65,43 @@ func TestMac(t *testing.T) {
}
}
}
func TestPing1(t *testing.T) {
conn, err := net.DialTimeout("ip4:icmp", "www.baidu.com", time.Second*5)
if err != nil {
log.Print(err)
return
}
var msg [512]byte
msg[0] = 8
msg[1] = 0
msg[2] = 0
msg[3] = 0
msg[4] = 0
msg[5] = 13
msg[6] = 0
msg[7] = 37
length := 8
check := checkSum(msg[0:length])
msg[2] = byte(check >> 8)
msg[3] = byte(check & 255)
_, err = conn.Write(msg[0:length])
if err != nil {
log.Print(err)
return
}
conn.Read(msg[0:])
log.Println(msg[5] == 13)
log.Println(msg[7] == 37)
}
func checkSum(msg []byte) uint16 {
sum := 0
for n := 1; n < len(msg)-1; n += 2 {
sum += int(msg[n])*256 + int(msg[n+1])
}
sum = (sum >> 16) + (sum & 0xffff)
sum += sum >> 16
return uint16(^sum)
}