• ping命令


    package main

    import (
    "bytes"
    "database/sql"
    "errors"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
    "net"
    "os"
    "strings"
    "time"
    )

    func mainPing2() {
    const DBDriverName = "mysql"
    const DBDataSourceName = "root:qazwsx@tcp(10.128.1.55:3306)/noc"
    db, e := sql.Open(DBDriverName, DBDataSourceName)
    if e != nil {
    panic(e)
    }
    e = db.Ping()
    if e != nil {
    panic(e)
    }
    // close database after all work is done
    defer db.Close()

    ips := []string{"10.128.1.194", "10.128.1.57"}
    s := "SELECT ipaddr,lastconntime FROM devicestatus WHERE ipaddr IN ("" + strings.Join(ips, "","") + "");"
    m := map[string]map[string]string{}
    now := func() string {
    return fmt.Sprint(time.Now())
    }
    f := func() {
    rows, e := db.Query(s)
    if e != nil {
    panic(e)
    }
    for rows.Next() {
    var ipaddr, lastconntime string
    e = rows.Scan(&ipaddr, &lastconntime)
    if m[ipaddr] == nil {
    m[ipaddr] = map[string]string{}
    }
    m[ipaddr][now()] = strings.Join([]string{lastconntime}, ",")
    }
    }

    for i := 0; i < 8; i++ {
    f()
    time.Sleep(60 * time.Second)
    }
    for k, v := range m {
    for k1, v1 := range v {
    fmt.Println(k, k1, v1)
    }
    }
    }

    // Copyright 2009 The Go Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style
    // license that can be found in the LICENSE file.

    // taken from http://golang.org/src/pkg/net/ipraw_test.go

    const (
    icmpv4EchoRequest = 8
    icmpv4EchoReply = 0
    icmpv6EchoRequest = 128
    icmpv6EchoReply = 129
    )

    type icmpMessage struct {
    Type int // type
    Code int // code
    Checksum int // checksum
    Body icmpMessageBody // body
    }

    type icmpMessageBody interface {
    Len() int
    Marshal() ([]byte, error)
    }

    // Marshal returns the binary enconding of the ICMP echo request or
    // reply message m.
    func (m *icmpMessage) Marshal() ([]byte, error) {
    b := []byte{byte(m.Type), byte(m.Code), 0, 0}
    if m.Body != nil && m.Body.Len() != 0 {
    mb, err := m.Body.Marshal()
    if err != nil {
    return nil, err
    }
    b = append(b, mb...)
    }
    switch m.Type {
    case icmpv6EchoRequest, icmpv6EchoReply:
    return b, nil
    }
    csumcv := len(b) - 1 // checksum coverage
    s := uint32(0)
    for i := 0; i < csumcv; i += 2 {
    s += uint32(b[i+1])<<8 | uint32(b[i])
    }
    if csumcv&1 == 0 {
    s += uint32(b[csumcv])
    }
    s = s>>16 + s&0xffff
    s = s + s>>16
    // Place checksum back in header; using ^= avoids the
    // assumption the checksum bytes are zero.
    b[2] ^= byte(^s & 0xff)
    b[3] ^= byte(^s >> 8)
    return b, nil
    }

    // parseICMPMessage parses b as an ICMP message.
    func parseICMPMessage(b []byte) (*icmpMessage, error) {
    msglen := len(b)
    if msglen < 4 {
    return nil, errors.New("message too short")
    }
    m := &icmpMessage{Type: int(b[0]), Code: int(b[1]), Checksum: int(b[2])<<8 | int(b[3])}
    if msglen > 4 {
    var err error
    switch m.Type {
    case icmpv4EchoRequest, icmpv4EchoReply, icmpv6EchoRequest, icmpv6EchoReply:
    m.Body, err = parseICMPEcho(b[4:])
    if err != nil {
    return nil, err
    }
    }
    }
    return m, nil
    }

    // imcpEcho represenets an ICMP echo request or reply message body.
    type icmpEcho struct {
    ID int // identifier
    Seq int // sequence number
    Data []byte // data
    }

    func (p *icmpEcho) Len() int {
    if p == nil {
    return 0
    }
    return 4 + len(p.Data)
    }

    // Marshal returns the binary enconding of the ICMP echo request or
    // reply message body p.
    func (p *icmpEcho) Marshal() ([]byte, error) {
    b := make([]byte, 4+len(p.Data))
    b[0], b[1] = byte(p.ID>>8), byte(p.ID&0xff)
    b[2], b[3] = byte(p.Seq>>8), byte(p.Seq&0xff)
    copy(b[4:], p.Data)
    return b, nil
    }

    // parseICMPEcho parses b as an ICMP echo request or reply message body.
    func parseICMPEcho(b []byte) (*icmpEcho, error) {
    bodylen := len(b)
    p := &icmpEcho{ID: int(b[0])<<8 | int(b[1]), Seq: int(b[2])<<8 | int(b[3])}
    if bodylen > 4 {
    p.Data = make([]byte, bodylen-4)
    copy(p.Data, b[4:])
    }
    return p, nil
    }

    func Ping(address string, timeout int) (alive bool) {
    err := Pinger(address, timeout)
    alive = err == nil
    return
    }

    func Pinger(address string, timeout int) (err error) {
    c, err := net.Dial("ip4:icmp", address)
    if err != nil {
    return
    }
    c.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
    defer c.Close()

    typ := icmpv4EchoRequest
    xid, xseq := os.Getpid()&0xffff, 1
    wb, err := (&icmpMessage{
    Type: typ, Code: 0,
    Body: &icmpEcho{
    ID: xid, Seq: xseq,
    Data: bytes.Repeat([]byte("Go Go Gadget Ping!!!"), 3),
    },
    }).Marshal()
    if err != nil {
    return
    }
    if _, err = c.Write(wb); err != nil {
    return
    }
    var m *icmpMessage
    rb := make([]byte, 20+len(wb))
    for {
    if _, err = c.Read(rb); err != nil {
    return
    }
    rb = ipv4Payload(rb)
    if m, err = parseICMPMessage(rb); err != nil {
    return
    }
    switch m.Type {
    case icmpv4EchoRequest, icmpv6EchoRequest:
    continue
    }
    break
    }
    return
    }

    func ipv4Payload(b []byte) []byte {
    if len(b) < 20 {
    return b
    }
    hdrlen := int(b[0]&0x0f) << 2
    return b[hdrlen:]
    }

    golang实现ping命令 - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1071047

  • 相关阅读:
    PLSQL导入导出oracle表 表空间
    IDL(Interactive Data Language——交互式数据语言)
    easyui更改messager的OkCancel按钮为(中文)确定取消
    java 中判断字符串相等
    hql实现对表的某几个(部分)字段查询
    MyEclipse 断点打不上 提示 absent line number information
    cocos2dx-3.x 导出自定义类到 lua 过程
    cocos2d-x中DrawNode常见的图像绘制函数
    cocos2d环境及创建一个自己的项目
    cocos2d基本类介绍 director/scene/layer/sprite
  • 原文地址:https://www.cnblogs.com/rsapaper/p/15407216.html
Copyright © 2020-2023  润新知