校验IP
代码如下(main.go):
package main
import (
"flag"
"fmt"
"net"
"strings"
)
const IPV4 = 4
const IPV6 = 6
func getIPType(ip string) int {
for i := 0; i < len(ip); i++ {
switch ip[i] {
case '.':
return IPV4
case ':':
return IPV6
}
}
return 0
}
func main() {
// Define flags
macPtr := flag.String("mac", "", "MAC address")
sipPtr := flag.String("sip", "", "Source IP address")
dipPtr := flag.String("dip", "", "Destination IP address")
vlanidPtr := flag.Int("vlanid", -1, "VLAN ID")
intfNamePtr := flag.String("intf_name", "", "Interface name")
// Parse flags
flag.Parse()
// Check if all flags are provided
if *macPtr == "" || *sipPtr == "" || *dipPtr == "" || *vlanidPtr == 0 || *intfNamePtr == "" {
fmt.Println("Usage: go run check.go -mac <mac> -sip <sip> -dip <dip> -vlanid <vlanid> -intf_name <intf_name>")
return
}
// Check if the VLAN ID is valid
if *vlanidPtr < 1 || *vlanidPtr > 4095 {
fmt.Println("Invalid VLAN ID")
return
}
// Check if the interface name is valid
if len(*intfNamePtr) > 100 {
fmt.Println("Interface name is too long")
return
}
// Parse the MAC address
mac, err := net.ParseMAC(*macPtr)
if err != nil {
fmt.Println("Invalid MAC address")
return
}
// Check if the source IP address and destination IP address are of the same type
if getIPType(*sipPtr) != getIPType(*dipPtr) {
fmt.Println("Both source and destination IP addresses must be of the same type IPv4/IPv6")
return
}
// Parse the source IP address
sip := net.ParseIP(*sipPtr)
if sip == nil {
fmt.Println("Invalid source IP address")
return
}
// Parse the destination IP address
dip := net.ParseIP(*dipPtr)
if dip == nil {
fmt.Println("Invalid destination IP address")
return
}
//Print the validated arguments
fmt.Printf("MAC address: %s\n", mac)
fmt.Printf("Source IP address: %s\n", sip)
fmt.Printf("Destination IP address: %s\n", dip)
fmt.Printf("VLAN ID: %d\n", *vlanidPtr)
fmt.Printf("Interface name: %s\n", strings.TrimSpace(*intfNamePtr))
}
编译:
go build -o check main.go
执行
% ./check --help
Usage of ./check:
-dip string
Destination IP address
-intf_name string
Interface name
-mac string
MAC address
-sip string
Source IP address
-vlanid int
VLAN ID (default -1)
./check -mac A1:2B:C3:4D:E5:6F -sip 192.168.1.1 -dip 127.0.0.1 -intf_name abc -vlanid 10
MAC address: a1:2b:c3:4d:e5:6f
Source IP address: 192.168.1.1
Destination IP address: 127.0.0.1
VLAN ID: 10
Interface name: abc
推荐阅读
golang学习之旅——解开心中的go mod疑惑
在go1.16版本发布后,go module由原来的默认值 auto 变为 on 了,这意味着后续开发中,go更推荐用go module 模式开发,而不是gopath模式开发了。
Keson赞 11阅读 14.8k
「刷起来」Go必看的进阶面试题详解
逃逸分析是Go语言中的一项重要优化技术,可以帮助程序减少内存分配和垃圾回收的开销,从而提高程序的性能。下面是一道涉及逃逸分析的面试题及其详解。
王中阳Go赞 4阅读 1.9k评论 1
初学后端,如何做好表结构设计?
这篇文章介绍了设计数据库表结构应该考虑的4个方面,还有优雅设计的6个原则,举了一个例子分享了我的设计思路,为了提高性能我们也要从多方面考虑缓存问题。
王中阳Go赞 4阅读 1.7k评论 2
又一款眼前一亮的Linux终端工具!
今天给大家介绍一款最近发现的功能十分强大,颜值非常高的一款终端工具。这个神器我是在其他公众号文章上看到的,但他们都没把它的强大之处介绍明白,所以我自己体验一波后,再向大家分享自己的体验。
良许赞 5阅读 1.8k
一分钟搞明白!快速掌握 Go WebAssembly
最近因为各种奇怪的原因,更多的接触到了 WebAssembly。虽然之前很多博客也翻过写过各种文章,但总感觉欠些味道。于是今天梳理了一版,和大家一起展开学习。
煎鱼赞 4阅读 2.2k
面试官:请说一下如何优化结构体的性能?
使用内存对齐机制优化结构体性能,妙啊!前言之前分享过2篇结构体文章:10秒改struct性能直接提升15%,产品姐姐都夸我好棒 和 Go语言空结构体这3种妙用,你知道吗? 得到了大家的好评。这篇继续分享进阶内容:结...
王中阳Go赞 4阅读 3.8k评论 2
go 协程操作map导致的数据竞争及解决方法
有个查询结果集的操作,无可避免的需要在循环获取数据,然后将结果集放到 map 中,这个操作在压测的时候,没出现问题,发布到生产环境之后,开始偶现 fatal error: concurrent map read and map write 错误,导致...
hxd_赞 5阅读 842评论 4
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。