34

I hope this isn't too basic a question. The title kind of asks it all. :-)

shamittomar
  • 46,210
  • 12
  • 74
  • 78
richb01
  • 1,097
  • 2
  • 13
  • 25
  • 3
    The title doesn't say whether you want a command to do this, or whether you want to write a program to do this. It also doesn't say what language or platform you're talking about. – John Saunders Jul 21 '10 at 20:32
  • ping -I (wlan0,eth0..) www.stackoverflow.com – Anders Jul 21 '10 at 22:12
  • 3
    @john - this is windows. I can use command scripting or VB Script. Thanks! – richb01 Jul 22 '10 at 03:36
  • @Anders that answer applies to Unix based systems. Though it will definitely be useful for others, since this question is pretty high up in search results. – HackerDaGreat57 Dec 13 '22 at 01:45

2 Answers2

33

ping also has an option in Windows:

-S srcaddr     Source address to use.

So you can do something like

ping 10.10.10.1 -l 0 -S 192.168.1.1

where 10.10.10.1 is the destination address and 192.168.1.1 is the address of the source adapter

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 2
    Does this not just change the source address? How are we sure this is coming out of the interface that has the same source address? – Dylan Mar 28 '17 at 19:26
  • 1
    @Dylan You can see that the selected interface is being used by setting that interface to use a nonexistent gateway and seeing the ping fail. Change it to a valid one and it succeeds. ABA. – firetiger77 Dec 19 '19 at 07:24
33

The ping command will allow strict source routing so you can specify the default gateway to use on the way out. (This assumes your interfaces have distinct gateways i.e. are on different networks)

ping -k 192.169.1.1 microsoft.com
        ^ default gateway for desired interface

Using WMI Win32_PingStatus you can do the same thing where you specify SourceRoute and SourceRouteType (This is essentially the same as using ping -k)

Alternatively:

If your network interfaces are on the same network, like a LAN card and a wireless adapater you can add a custom ROUTE to your machine, which can send all traffic through a specific interface.

From ROUTE HELP:

route ADD 207.46.0.0 MASK 255.255.0.0  192.168.1.1 METRIC 3 IF 2
        destination^      ^mask        ^gateway     metric^    ^
                                                      Interface^
Jack B Nimble
  • 5,039
  • 4
  • 40
  • 62