Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Logger for log #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 48 additions & 47 deletions arping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,57 @@
//
// The currently supported platforms are: Linux and BSD.
//
//
// The library requires raw socket access. So it must run as root, or with appropriate capabilities under linux:
// `sudo setcap cap_net_raw+ep <BIN>`.
//
//
// Examples:
//
// ping a host:
// ------------
// package main
// import ("fmt"; "github.com/j-keck/arping"; "net")
//
// func main(){
// dstIP := net.ParseIP("192.168.1.1")
// if hwAddr, duration, err := arping.Ping(dstIP); err != nil {
// fmt.Println(err)
// } else {
// fmt.Printf("%s (%s) %d usec\n", dstIP, hwAddr, duration/1000)
// }
// }
// ping a host:
// ------------
// package main
// import ("fmt"; "github.com/j-keck/arping"; "net")
//
// func main(){
// dstIP := net.ParseIP("192.168.1.1")
// if hwAddr, duration, err := arping.Ping(dstIP); err != nil {
// fmt.Println(err)
// } else {
// fmt.Printf("%s (%s) %d usec\n", dstIP, hwAddr, duration/1000)
// }
// }
//
// resolve mac address:
// --------------------
// package main
// import ("fmt"; "github.com/j-keck/arping"; "net")
//
// func main(){
// dstIP := net.ParseIP("192.168.1.1")
// if hwAddr, _, err := arping.Ping(dstIP); err != nil {
// fmt.Println(err)
// } else {
// fmt.Printf("%s is at %s\n", dstIP, hwAddr)
// }
// }
// resolve mac address:
// --------------------
// package main
// import ("fmt"; "github.com/j-keck/arping"; "net")
//
// func main(){
// dstIP := net.ParseIP("192.168.1.1")
// if hwAddr, _, err := arping.Ping(dstIP); err != nil {
// fmt.Println(err)
// } else {
// fmt.Printf("%s is at %s\n", dstIP, hwAddr)
// }
// }
//
// check if host is online:
// ------------------------
// package main
// import ("fmt"; "github.com/j-keck/arping"; "net")
//
// func main(){
// dstIP := net.ParseIP("192.168.1.1")
// _, _, err := arping.Ping(dstIP)
// if err == arping.ErrTimeout {
// fmt.Println("offline")
// }else if err != nil {
// fmt.Println(err.Error())
// }else{
// fmt.Println("online")
// }
// }
// check if host is online:
// ------------------------
// package main
// import ("fmt"; "github.com/j-keck/arping"; "net")
//
// func main(){
// dstIP := net.ParseIP("192.168.1.1")
// _, _, err := arping.Ping(dstIP)
// if err == arping.ErrTimeout {
// fmt.Println("offline")
// }else if err != nil {
// fmt.Println(err.Error())
// }else{
// fmt.Println("online")
// }
// }
package arping

import (
Expand All @@ -64,16 +61,20 @@ import (
"io/ioutil"
"log"
"net"
"os"
"time"
)

type Logger interface {
Printf(format string, v ...interface{})
Println(v ...interface{})
}

var (
// ErrTimeout error
ErrTimeout = errors.New("timeout")

verboseLog = log.New(ioutil.Discard, "", 0)
timeout = time.Duration(500 * time.Millisecond)
verboseLog Logger = log.New(ioutil.Discard, "", 0)
timeout = time.Duration(500 * time.Millisecond)
)

// Ping sends an arp ping to 'dstIP'
Expand Down Expand Up @@ -215,8 +216,8 @@ func GratuitousArpOverIface(srcIP net.IP, iface net.Interface) error {
}

// EnableVerboseLog enables verbose logging on stdout
func EnableVerboseLog() {
verboseLog = log.New(os.Stdout, "", 0)
func EnableVerboseLog(l Logger) {
verboseLog = l
}

// SetTimeout sets ping timeout
Expand Down
27 changes: 14 additions & 13 deletions cmd/arping/main.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
//
// command line arping utility which use the 'arping' library
//
// this utility need raw socket access, please run it
// under FreeBSD: as root
// under Linux: as root or with 'cap_net_raw' permission: sudo setcap cap_net_raw+ep <ARPING_PATH>
//
// under FreeBSD: as root
// under Linux: as root or with 'cap_net_raw' permission: sudo setcap cap_net_raw+ep <ARPING_PATH>
//
// options:
// -h: print help and exit
// -v: verbose output
// -U: unsolicited/gratuitous ARP mode
// -i: interface name to use
// -t: timeout - duration with unit - such as 100ms, 500ms, 1s ...
//
// -h: print help and exit
// -v: verbose output
// -U: unsolicited/gratuitous ARP mode
// -i: interface name to use
// -t: timeout - duration with unit - such as 100ms, 500ms, 1s ...
//
// exit code:
// 0: target online
// 1: target offline
// 2: error occurred - see command output
//
// 0: target online
// 1: target offline
// 2: error occurred - see command output
package main

import (
"flag"
"fmt"
"github.com/j-keck/arping"
"log"
"net"
"os"
"time"

"github.com/j-keck/arping"
)

var (
Expand All @@ -45,7 +46,7 @@ func main() {
printHelpAndExit()
}
if *verboseFlag {
arping.EnableVerboseLog()
arping.EnableVerboseLog(log.New(os.Stdout, "", 0))
}
arping.SetTimeout(*timeoutFlag)

Expand Down