go-ping / ping

ICMP Ping library for Go
MIT License
1.31k stars 344 forks source link

How to get network interface used to make the ping? #217

Closed santosh closed 1 year ago

santosh commented 2 years ago

I have a use case where I have to know which interface was used to make the ping. Is it possible with this library to get the interface?

alikud commented 1 year ago

I have a use case where I have to know which interface was used to make the ping. Is it possible with this library to get the interface?

Maybe this is solution for you

pinger, err := ping.NewPinger("www.google.com")
pinger.Source = "192.168.17.100"

Where source is your interface, try to ens0 like this, maybe will also work

also, try run with sudo, like sudo go run main.go, because socket operations not permitted by default

santosh commented 1 year ago

I was more interested in know which interface was used.

If I am correct, what you are suggesting is to:

  1. First find out all the interfaces on the device.
  2. Try making ping request for N number of time where N is the number of interfaces.

Am I correct?

santosh commented 1 year ago

Well yeah. If there is no way then the only way is to bruteforce. I suppose I should launch N goroutines and the only ping which succeeds will be my interface.