当前位置:网站首页>Golang通过exec模块实现Ping连通性检测案例
Golang通过exec模块实现Ping连通性检测案例
2022-04-23 05:32:00 【坐公交也用券】
源码
import (
"fmt"
"os/exec"
"runtime"
)
// 小写函数名称只能在当前文件调用
func pingcmd(host string) bool {
// 实例化一个执行任务
cmds := exec.Command("ping", host)
// 开始执行
err := cmds.Run()
// 判断执行状态(只获取成功或者失败)
if err != nil {
fmt.Println("Connection failed: ", host)
return false
}else {
fmt.Println("Connection successful: ", host)
return true
}
}
// 大写开头的函数可被外部调用
func PingStatus(host string) bool {
// 检测ping是否连接成功
fmt.Println("Pinging ", host)
// 获取当前系统类型
sysType := runtime.GOOS
// 打印当前系统类型
fmt.Println("Current system type: ", sysType)
// 根据系统类型设置对应的参数
if sysType == "windows" {
// Windows 系统下直接使用ping ip
status := pingcmd(host)
return status
}else{
// 非windows下需要使用-c 指定数据包数量
host := host + " -c 5"
status := pingcmd(host)
return status
}
return false
}
func main() {
d := PingStatus("baidu.com")
if d {
fmt.Println("ok")
}else {
fmt.Println("Errors")
}
}
执行结果
Pinging baidu.com
Current system type: windows
Connection successful: baidu.com
ok
版权声明
本文为[坐公交也用券]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_36154886/article/details/124357338
边栏推荐
- 弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
- Differences between auto and decltype inference methods (learning notes)
- QT displays the specified position and size of the picture
- egg的static的前缀是可以修改惹,靴靴
- 可执行程序执行流程
- Generation of straightening body in 3D slicer
- 点击添加按钮--出现一个框框(类似于添加学习经历-本科-研究生)
- Formal parameters, local variables and local static variables
- If I am PM's performance, movie VR ticket purchase display
- Log introduction and building web application
猜你喜欢
Excel 2016 cannot open the file for the first time. Sometimes it is blank and sometimes it is very slow. You have to open it for the second time
Double click The jar package cannot run the solution
After NPM was upgraded, there was a lot of panic
Hongji | how does HR carry out self change and organizational change in the digital era?
Traversal array, object parent-child communication props / $emit
(11) Vscode code formatting configuration
Three methods of list rendering
[the background color changes after clicking a line]
2021-09-27
Nécessité de précharger les cookies dans le sélénium
随机推荐
College entrance examination volunteer filling reference
Double click The jar package cannot run the solution
Getting started with varnish
Create process memory management copy_ Mm - processes and threads (IX)
World and personal development
If I am PM's performance, movie VR ticket purchase display
uni使用的一些坑
Click the Add button - a box appears (similar to adding learning experience - undergraduate - Graduate)
Membarrier (personal learning and understanding)
The main difference between pointer and reference
[triangle Yang Hui triangle printing odd even cycle JS for break cycle]
Data bus realizes the communication between brother components
史上最强egg框架的error处理机制
五一劳动节期间什么理财产品会有收益?
Requirements for SQL server to retrieve SQL and user information
d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
Simple and basic use of switch and if
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
Traversal array, object parent-child communication props / $emit
On the use of constant pointer and pointer constant -- exercise (record)