Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm making an script, and I need to detect in what interface is the traffic of an IP going through. This is because in the arch, an IP address is attended only for one of four NIC's, but can be any of those four.

The manual way I did is running a tcpdump: if I see packets, then this is the interface; if not, cancel tcpdump, and do another tcpdump on another interface.

I'm running FreeBSD.

share|improve this question
    
How about route -n? –  jordanm Apr 26 '13 at 1:51
    
route -n don't let me to know packets going on an interface, only changes of routing –  X3MBoy Apr 26 '13 at 2:05
add comment

3 Answers 3

Call route get.

route get 192.0.2.42 | awk '$1 ~ /interface/ {print $2}'
share|improve this answer
    
This work, but i have another issue, this is not the interface i'm looking for. I'm now thinking that i'm having traffic for several interfaces. So the question is dismissed because have a tecnical error. –  X3MBoy May 8 '13 at 4:45
    
Thx for support –  X3MBoy May 8 '13 at 4:46
add comment

netstat -tau shows you the local address and port used by a program. Each interface has its own address, except cases like bonding... Is that the information you need?

If that's so, it's easy to grep and cut the output to use it in a script.

For example: netstat -taunpe | grep firefox | awk '{print $4}' | cut -d ':' -f 1 retrieves the local address used by the connections "firefox" has established.

share|improve this answer
add comment

I'm not sure about *BSDs, but on Linuxes route -n tells you (apart from other things) flags (you'd probably want to search for the line which is marked as default route) and the device name:

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.100.63.254   0.0.0.0         UG    0      0        0 eth0
10.100.0.0      0.0.0.0         255.255.192.0   U     0      0        0 eth0
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0

or:

$ ip route
default via 10.100.63.254 dev eth0 
10.100.0.0/18 dev eth0  proto kernel  scope link  src 10.100.12.38 
127.0.0.0/8 dev lo  scope link 
169.254.0.0/16 dev eth0  scope link
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.