golang命令行网络诊断工具插件myLG的使用
Golang命令行网络诊断工具插件myLG的使用
myLG是一款开源软件工具,它将多种网络探测功能整合到一个网络诊断工具中。
主要功能
- 流行的Looking Glass服务(ping/trace/bgp):Telia、Level3、NTT、Cogent、KPN
- 超过200个国家的DNS查询信息
- 本地ping和实时路由跟踪
- 数据包分析器 - TCP/IP和其他数据包
- 快速网络管理系统(NMS)
- 本地HTTP/HTTPS ping(GET、POST、HEAD)
- RIPE信息(ASN、IP/CIDR)
- PeeringDB信息
- 端口扫描
- 网络LAN发现
- 互联网速度测试
- Web仪表板
- 可配置选项
- 直接从shell访问命令
- 支持vi和emacs模式,几乎所有基本功能
- CLI自动完成和历史功能
使用示例
// 示例1: HTTP ping
local> hping https://www.google.com -trace -c 4
HPING www.google.com (216.58.194.196), Method: HEAD, DNSLookup: 17.2923 ms
HTTP Response seq=0, proto=HTTP/1.1, status=200, time=183.097 ms, connection=34.789 ms, first byte read=148.167 ms
HTTP Response seq=1, proto=HTTP/1.1, status=200, time=164.960 ms, connection=27.764 ms, first byte read=137.096 ms
HTTP Response seq=2, proto=HTTP/1.1, status=200, time=153.559 ms, connection=27.881 ms, first byte read=125.526 ms
HTTP Response seq=3, proto=HTTP/1.1, status=200, time=164.309 ms, connection=28.904 ms, first byte read=135.296 ms
--- www.google.com HTTP ping statistics ---
4 requests transmitted, 4 replies received, 0% requests failed
HTTP Round-trip min/avg/max = 153.56/164.05/183.10 ms
HTTP Code [200] responses : [████████████████████] 100.00%
// 示例2: 端口扫描
local> scan www.google.com -p 1-500
+----------+------+--------+-------------+
| PROTOCOL | PORT | STATUS | DESCRIPTION |
+----------+------+--------+-------------+
| TCP | 80 | Open | |
| TCP | 443 | Open | |
+----------+------+--------+-------------+
Scan done: 2 opened port(s) found in 5.605 seconds
// 示例3: IPv6 ping
local> ping google.com -6
PING google.com (2607:f8b0:400b:80a::200e): 56 data bytes
64 bytes from 2607:f8b0:400b:80a::200e icmp_seq=0 time=23.193988 ms
64 bytes from 2607:f8b0:400b:80a::200e icmp_seq=1 time=21.265492 ms
64 bytes from 2607:f8b0:400b:80a::200e icmp_seq=2 time=24.521306 ms
64 bytes from 2607:f8b0:400b:80a::200e icmp_seq=3 time=25.313072 ms
// 示例4: 路由跟踪
local> trace google.com
trace route to google.com (172.217.4.142), 30 hops max
1 192.168.0.1 4.705 ms 1.236 ms 0.941 ms
2 142.254.236.25 [ASN 20001/ROADRUNNER-WEST] 13.941 ms 13.504 ms 12.303 ms
3 agg59.snmncaby01h.socal.rr.com. (76.167.31.241) [ASN 20001/ROADRUNNER-WEST] 14.834 ms 11.625 ms 13.050 ms
4 agg20.lamrcadq01r.socal.rr.com. (72.129.10.128) [ASN 20001/ROADRUNNER-WEST] 17.617 ms 18.064 ms 15.612 ms
5 agg28.lsancarc01r.socal.rr.com. (72.129.9.0) [ASN 20001/ROADRUNNER-WEST] 16.291 ms 24.079 ms 20.456 ms
6 bu-ether26.lsancarc0yw-bcr00.tbone.rr.com. (66.109.3.230) [ASN 7843/TWCABLE-BACKBONE] 18.339 ms 23.278 ms 23.434 ms
7 216.0.6.25 [ASN 2828/XO-AS15] 19.842 ms 21.025 ms 35.105 ms
8 216.0.6.42 [ASN 2828/XO-AS15] 16.666 ms 18.252 ms 18.872 ms
9 209.85.245.199 [ASN 15169/GOOGLE] 14.358 ms 17.478 ms
209.85.246.125 [ASN 15169/GOOGLE] 18.593 ms
10 72.14.239.121 [ASN 15169/GOOGLE] 21.635 ms
72.14.238.213 [ASN 15169/GOOGLE] 16.133 ms
72.14.239.121 [ASN 15169/GOOGLE] 21.541 ms
11 lax17s14-in-f14.1e100.net. (172.217.4.142) [ASN 15169/GOOGLE] 18.127 ms 17.151 ms 18.892 ms
如何构建
myLG可以在Linux和Darwin系统上构建。需要依赖libpcap库:
# Linux
apt-get install libpcap-dev
# OSX
brew install homebrew/dupes/libpcap
如果已安装Go语言编译器(需要1.7或更高版本),可以使用以下命令构建:
go get github.com/mehrdadrad/mylg
cd $GOPATH/src/github.com/mehrdadrad/mylg/
go build mylg.go
许可证
该项目使用MIT许可证。请阅读LICENSE文件。
贡献
欢迎任何形式的贡献,请遵循以下步骤:
- 在github.com上fork项目
- 创建一个新分支
- 将更改提交到新分支
- 发送pull请求
更多关于golang命令行网络诊断工具插件myLG的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang命令行网络诊断工具插件myLG的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
myLG - Go语言命令行网络诊断工具
myLG (my Looking Glass) 是一个用Go语言编写的命令行网络诊断工具,它集成了多种网络测试功能,如ping、traceroute、DNS查询等。下面我将介绍如何使用myLG以及提供一些Go代码示例。
安装myLG
# 使用go install安装
go install github.com/mehrdadrad/mylg@latest
# 或者从源码编译
git clone https://github.com/mehrdadrad/mylg.git
cd mylg
go build
./mylg
基本使用
启动myLG交互式界面:
mylg
或者直接执行命令:
mylg ping google.com
主要功能示例
1. Ping测试
mylg ping google.com
Go实现简单ping的代码示例:
package main
import (
"fmt"
"net"
"os"
"time"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: ping <host>")
return
}
host := os.Args[1]
ips, err := net.LookupIP(host)
if err != nil {
fmt.Printf("Could not get IPs: %v\n", err)
return
}
ip := ips[0].String()
fmt.Printf("PING %s (%s)\n", host, ip)
for i := 0; i < 4; i++ {
start := time.Now()
c, err := net.Dial("ip4:icmp", ip)
if err != nil {
fmt.Printf("Dial error: %v\n", err)
continue
}
defer c.Close()
msg := []byte("abcdefghijklmnopqrstuvwabcdefghi")
_, err = c.Write(msg)
if err != nil {
fmt.Printf("Write error: %v\n", err)
continue
}
reply := make([]byte, 256)
err = c.SetReadDeadline(time.Now().Add(time.Second * 2))
n, err := c.Read(reply)
if err != nil {
fmt.Printf("Read error: %v\n", err)
continue
}
duration := time.Since(start)
fmt.Printf("%d bytes from %s: time=%v\n", n, ip, duration)
time.Sleep(time.Second)
}
}
2. Traceroute
mylg trace google.com
3. DNS查询
mylg dns google.com
Go实现DNS查询的代码示例:
package main
import (
"fmt"
"net"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: dns <host>")
return
}
host := os.Args[1]
// A记录查询
ips, err := net.LookupIP(host)
if err != nil {
fmt.Printf("Could not get IPs: %v\n", err)
return
}
fmt.Println("A Records:")
for _, ip := range ips {
fmt.Println(ip)
}
// MX记录查询
mxs, err := net.LookupMX(host)
if err == nil {
fmt.Println("\nMX Records:")
for _, mx := range mxs {
fmt.Printf("%s (pref %d)\n", mx.Host, mx.Pref)
}
}
}
4. HTTP测试
mylg http google.com
5. 网络扫描
mylg scan 192.168.1.1/24
高级功能
- 自定义外观:可以修改~/.mylg/config.json来更改界面颜色和布局
- 插件系统:支持编写自定义插件扩展功能
- 批量测试:可以对多个目标执行相同的测试
创建自定义插件示例
myLG支持通过Go插件扩展功能。创建一个简单的插件:
// hello.go
package main
import (
"fmt"
)
var CmdHello = &Command{
Name: "hello",
Help: "Say hello",
Func: func(cmd *Command, args []string) {
fmt.Println("Hello from myLG plugin!")
},
}
编译为.so文件:
go build -buildmode=plugin -o hello.so hello.go
然后在myLG中加载:
mylg load hello.so
总结
myLG是一个功能强大的命令行网络诊断工具,特别适合网络管理员和开发人员使用。它的Go语言实现保证了跨平台兼容性和高性能。通过其插件系统,你可以轻松扩展功能以满足特定需求。
对于更复杂的需求,可以考虑结合使用标准库的net
、net/http
等包,或者使用更专业的网络库如github.com/google/gopacket
进行数据包级别的操作。