简介
基于 Golang实现一个批量自动化执行树莓派指令的软件(8)辅助模块-远程IP端口是否开放连接(TCP) 和 Golang实现一个批量自动化执行树莓派指令的软件(7)辅助模块-本地活动网络, 这两篇, 再新增使用系统ping指令判定设备是否在线。
环境描述
运行环境 : Windows, 基于Golang, 暂时没有使用什么不可跨平台接口, 理论上支持Linux/MacOS
目标终端:树莓派DebianOS(主要做用它测试)
实现
network_windows.go
go
package utils
import (
"os/exec"
)
func IsIPOnline(ip string) bool {
// 使用ping命令检查IP地址,-c 1表示发送一个请求,-w 1表示等待1秒
cmd := exec.Command("ping", "-n", "1", "-l", "1024", "-w", "1000", ip)
// 执行命令并等待它完成
//output, err := cmd.CombinedOutput()
_, err := cmd.CombinedOutput()
if err != nil {
return false
//if exitError, ok := err.(*exec.ExitError); ok {
// fmt.Printf("host is unreachable. Exit code: %d\n", exitError.ExitCode())
//} else {
// // 处理其他类型的错误
// fmt.Println("Failed to execute ping command.")
//}
}
return true
}
network.go
go
func GetNetworkStat(ipv4 string, port uint, timeout time.Duration) NetworkStatus {
var (
status = NetworkStatus{
IPAvailable: false,
PortAvailable: false,
}
)
if ok := IsIPOnline(ipv4); !ok {
return status
}
status.IPAvailable = true
if IsRemoteNetworkAlive(ipv4, port, timeout) {
status.PortAvailable = true
}
return status
}
network_linux.go
go
package utils
import (
"os/exec"
)
func IsIPOnline(ip string) bool {
// 使用ping命令检查IP地址,-c 1表示发送一个请求,-w 1表示等待1秒
cmd := exec.Command("ping", "-s", "1024", "-w", "1", ip)
// 执行命令并等待它完成
_, err := cmd.CombinedOutput()
if err != nil {
return false
}
return true
}
测试用例
go
func TestIsIPOnline(t *testing.T) {
var (
ipv4PrefixStr string
ipv4Str string
idx int
interfaces, err = GetLocalActivityNetworkInterfaces()
ipMin = 0
ipMax = 255
waiter sync.WaitGroup
)
if nil != err {
fmt.Println("get lcoal activity network fail, ", err.Error())
return
}
for _, intf := range interfaces {
if 0 >= len(intf.IPv4) {
continue
}
idx = strings.LastIndex(intf.IPv4, ".")
ipv4PrefixStr = intf.IPv4[:idx+1]
for ipIndex := ipMin; ipIndex < ipMax; ipIndex++ {
ipv4Str = fmt.Sprintf("%s%d", ipv4PrefixStr, ipIndex+1)
waiter.Add(1)
go func(ip string) {
if ok := IsIPOnline(ip); ok {
fmt.Println(ip, "is Alive.")
}
waiter.Done()
}(ipv4Str)
}
}
waiter.Wait()
}
func TestGetNetworkStat(t *testing.T) {
var (
ipv4PrefixStr string
ipv4Str string
idx int
interfaces, err = GetLocalActivityNetworkInterfaces()
ipMin = 0
ipMax = 255
port = uint(22)
waiter sync.WaitGroup
)
if nil != err {
fmt.Println("get lcoal activity network fail, ", err.Error())
return
}
for _, intf := range interfaces {
if 0 >= len(intf.IPv4) {
continue
}
idx = strings.LastIndex(intf.IPv4, ".")
ipv4PrefixStr = intf.IPv4[:idx+1]
for ipIndex := ipMin; ipIndex < ipMax; ipIndex++ {
ipv4Str = fmt.Sprintf("%s%d", ipv4PrefixStr, ipIndex+1)
waiter.Add(1)
go func(ip string, port uint) {
status := GetNetworkStat(ip, port, time.Second)
if status.IPAvailable || status.PortAvailable {
if status.IPAvailable {
fmt.Printf("%s ip is online, ", ip)
}
if status.PortAvailable {
fmt.Printf("port(%d) is opened.\n", port)
} else {
fmt.Printf("port(%d) is closed.\n", port)
}
}
waiter.Done()
}(ipv4Str, port)
}
}
waiter.Wait()
}
代码源
https://gitee.com/grayhsu/ssh_remote_access