Как пингануть с конкретного интерфейса, если интерфейсов несколько?
Как пингануть с конкретного интерфейса, если интерфейсов несколько?
стоит Linux ubuntu server и после настройки isc dhcp сервера, пинговаться почему-то начал с другого интерфейса.
то есть eth0 смотрит на локальную сеть, а eth1 на роутер.
хочу пингануть комп в локалке, но Destination Host Unreachable, так как icmp посылается с eth1
Как можно поменять? какой файл сконфигурировать? или что?
-
Вопрос задан
-
23326 просмотров
Пригласить эксперта
Пакеты отправляются в соответствии с таблицей маршрутизации. Если хотите, чтобы ехало через другой интерфейс — добавьте соответствующий маршрут. Если на одном интерфейсе несколько адресов, у пинга есть ключ -I
, чтобы вручную указать, с какого адреса слать.
# man ping
-I адрес
Установить адрес источника в указанный. В качестве аргумента
может выступать числовой IP-адрес или имя устройства.
ping -I «desired interface» 192.168.0.1
-
Показать ещё
Загружается…
Минуточку внимания
Test connectivity via a specific network interface
Recently while working on a Private cloud implementation, I
came across a scenario where I needed to test connectivity of a node to the
AD/DNS via multiple network adapters.
Many of us would know that having multiple network routes is usually done to take care of redundancy.
So that if a network adapter goes down, one can use the other network interface
to reach out to the node.
In order to make it easy for everyone to follow along, below
is an analogy for the above scenario:
My laptop has multiple network adapters (say Wi-Fi and Ethernet) connected to the same network. Now how do I test connectivity to a Server on the network only over say Wi-Fi network adapter?
So let’s get to it and explore some options at hand.
Below are the network adapters that list out on my laptop:
Now
I want to test connectivity to a Windows Server 2012R2 running in my network
having IP address 10.94.214.9
Using ping.exe
I can specify the source address to ping.exe using –S switch
and verify if the Server (IP 10.94.214.9) is responding to ping/ICMP requests on
a specific network interface.
But there is a gotcha with the above approach, what if the server response to ping is
disabled? Or the network firewall in place drops ICMP requests.
Using Test-NetConnection.
I initially thought of using Test-NetConnection cmdlet
(available on Server 2012 & Windows 8 above with NetTCPIP module) to do a winrm
port check to the server (port 5985), but the cmdlet doesn’t let you specify a
source address for doing a port query. It will automatically select a network
interface to perform the port query (based on the internal windows routing
table). See below the output of the cmdlet, it selects the Ethernet interface
to perform the port check.
See below the syntax of the cmdlet.
PS>gcm test-netconnection -syntax Test-NetConnection [[-ComputerName] ] [-TraceRoute] [-Hops ] [-InformationLevel ] [] Test-NetConnection [[-ComputerName] ] [-CommonTCPPort] [-InformationLevel ] [] Test-NetConnection [[-ComputerName] ] -Port [-InformationLevel ] []
One could play with route.exe and change the network route
to the network where the server lies and then do a Test-NetConnection on the
winrm port, complicated way to handle such a small problem.
Or better as my friend Jaap Brasser told me on IM, disable the network adapter and then do the Test-NetConnection.
Using TCPClient
Now let’s talk about how we can do this in PowerShell.
I can create a TCP Client and connect to the server on winrm port but how do we
make sure that it gets routed via a specific network interface.
The answer is really simple, we create a local endpoint (IP
+ port) and bind our TCP Client to it. All the communications then happen via
the socket.
Below is the code snippet and the explanation of it follows:
001 |
$SourceIP = [IPAddress]‘10.94.8.102’; # My WiFi Adapter IP address # get an unused local port, used in local IP endpoint creation # Create the local IP endpoint, this will bind to a specific N/W adapter for making the connection request # Create the TCP client and specify the local IP endpoint to be used. # Connect to the Destination on the required port. # Check the Connected property to see if the TCP connection succeeded. You can see netstat.exe output to verify the connection too |
In the above code after assigning the source IP, destination IP & destination port, there is code which selects a local port to be used.
We have to be careful while selecting a random local port as it
might be already be used in an active TCP connection. There is a clever .NET way of getting a list of already
used ports on a local machine by using the GetActiveTcpListeners() method.
$UsedLocalPorts = ([System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()).GetActiveTcpListeners() |
where -FilterScript {$PSitem.AddressFamily -eq ‘Internetwork’} |
Select -ExpandProperty Port
Once I have a list of all the used local port, I can select
a non-used ephemeral port (range) using the code snippet below:
do {
$localport = $(Get-Random -Minimum 49152 -Maximum 65535 )
} until ( $UsedLocalPorts -notcontains $localport)
Now it is time to create the local endpoint using the source
IP of the network interface and the local unused port.
$LocalIPEndPoint = New-Object -TypeName
System.Net.IPEndPoint -ArgumentList $SourceIP,$localport
Once the Local endpoint is created, construct a TCP client
passing the local endpoint as an argument to it. This will ensure that the TCP connection request flows via that specific N/W adapter.
Once that is done, call the Connect() Method on the TCPclient to connect to the destination. Now the TCP connection uses the SourceIP (on a specific network adapter) to reach out to the destination.
Note – One can try specifying a source address which is not
assigned to the machine, it will let you create the local endpoint but when you
try creating the TCPClient it will throw an error saying that the address is
not valid in the context.
Using the above logic and creating an advanced function should be straight enough, that is an exercise left for the reader.
This is something that has come up multiple times especially when working and dealing with network related tasks. Many times server engineers would contact us and complain about network traffic or VPN related issues or more recently I had to test VPN Connectivity from a server to other remote sites. However this server had 2 Network Cards. Both of them had different IP’s. Here is how the setup was:
- VLAN100 = NIC1 = 192.168.100.10
- VLAN200 = NIC2 = 192.168.200.10
Now I needed to ping the remote sites but I needed the source to be VLAN200 = NIC2 = 192.168.200.10. I opened up wireshark and did a simple ping and that showed traffic going out of VLAN100 = NIC1 = 192.168.100.10. Well that wasn’t going to help me because 192.168.100.10 was not part of the interesting traffic on the Cisco ASA. After a little digging I found out that I found out that there is a utility called NPing, that comes with NMap will allow me to accomplish that. I already had NMap installed on this server so I opened up the command prompt and typed:
nping
I got many options and the two options I was interested in were -e and -S.
- -e lets you specify the network interface you want to source the ICMP packet from
- -S lets you specify the IP Address of that network interface you want to source the ICMP packet from
- In my case I needed to source the ping (ICMP) from the second Network Card using the IP address of 192.168.200.10
- Now an important point to remember over here is that nping will not see the name of your network interface as it is in windows like “local area connection” etc. It uses the Linux way. So now I found out that mapping by utilizing the following command:
- It produced bunch of data towards the top second or third line was *************************INTERFACES************************
- Right under this line it shows all the network interfaces mapped in Linux style with the IP address. So the first interface was eth0, second was eth1
- I picked out the the interface I needed in my case it was eth1 and then ran the following command?
- Now looking at the wireshark capture I was able to see that the ICMP packets were going out of the second network card and on the Cisco ASA Firewall I was able to see the VPN Traffic
- nping offers so many more options to work with for troubleshooting purposes, in general NMAP is a great and must have utility for network and systems engineers
The ping command is a Command Prompt command used to test the ability of the source computer to reach a specified destination computer. It’s a simple way to verify that a computer can communicate with another computer or network device.
How Does the Ping Command Work?
The ping command operates by sending Internet Control Message Protocol (ICMP) Echo Request messages to the destination computer and waiting for a response. The two major pieces of information that the ping command provides are how many of those responses are returned and how long it takes for them to return.
For example, you might find no responses when pinging a network printer, only to find out that the printer is offline and its cable needs replaced. Or maybe you need to ping a router to verify that your computer can connect to it to eliminate it as a possible cause for a networking issue.
The word «ping» is also used online to refer to a brief message, usually over text or email. For example, you can «ping your boss» or send them a message about a project, but the ping command has nothing to do with it.
Ping Command Availability
The ping command is available from the Command Prompt in Windows 11, Windows 10, Windows 8, Windows 7, Windows Vista, and Windows XP operating systems. It’s also available in older versions of Windows like Windows 98 and 95.
This command can also be found in Command Prompt in the Advanced Startup Options and System Recovery Options repair/recovery menus.
Ping Command Syntax
ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS] [-r count] [-s count] [-w timeout] [-R] [-S srcaddr] [-p] [-4] [-6] target [/?]
The availability of certain ping command switches and other ping command syntax might differ from operating system to operating system.
How to Read Command Syntax
Ping Command Options | |
---|---|
Item | Explanation |
-t | Using this option will ping the target until you force it to stop by using Ctrl+C. |
-a | This ping command option will resolve, if possible, the hostname of an IP address target. |
-n count | This option sets the number of ICMP Echo Requests to send, from 1 to 4294967295. The ping command will send 4 by default if -n isn’t used. |
-l size | Use this option to set the size, in bytes, of the echo request packet from 32 to 65,527. The ping command will send a 32-byte echo request if you don’t use the -l option. |
-f | Use this ping command option to prevent ICMP Echo Requests from being fragmented by routers between you and the target. The -f option is most often used to troubleshoot Path Maximum Transmission Unit (PMTU) issues. |
-i TTL | This option sets the Time to Live (TTL) value, the maximum of which is 255. |
-v TOS | This option allows you to set a Type of Service (TOS) value. Beginning in Windows 7, this option no longer functions but still exists for compatibility reasons. |
-r count | Use this ping command option to specify the number of hops between your computer and the target computer or device that you’d like to be recorded and displayed. The maximum value for count is 9, so use the tracert command instead if you’re interested in viewing all the hops between two devices. |
-s count | Use this option to report the time, in Internet Timestamp format, that each echo request is received and echo reply is sent. The maximum value for count is 4, meaning that only the first four hops can be time stamped. |
-w timeout | Specifying a timeout value when executing the ping command adjusts the amount of time, in milliseconds, that ping waits for each reply. If you don’t use the -w option, the default timeout value of 4000 is used, which is 4 seconds. |
-R | This option tells the ping command to trace the round trip path. |
-S srcaddr | Use this option to specify the source address. |
-p | Use this switch to ping a Hyper-V Network Virtualization provider address. |
-4 | This forces the ping command to use IPv4 only but is only necessary if target is a hostname and not an IP address. |
-6 | This forces the ping command to use IPv6 only but as with the -4 option, is only necessary when pinging a hostname. |
target | This is the destination you wish to ping, either an IP address or a hostname. |
/? | Use the help switch with the ping command to show detailed help about the command’s several options. |
The -f, -v, -r, -s, -j, and -k options work when pinging IPv4 addresses only. The -R and -S options only work with IPv6.
Other less commonly used switches for the ping command exist including [-j host-list], [-k host-list], and [-c compartment]. Execute ping /? from the Command Prompt for more information on these options.
21 Best Command Prompt Tricks
Ping Command Examples
Below are several examples of commands that use ping.
Ping Google.com
ping -n 5 -l 1500 www.google.com
In this example, the ping command is used to ping the hostname www.google.com. The -n option tells the ping command to send 5 ICMP Echo Requests instead of the default of 4, and the -l option sets the packet size for each request to 1500 bytes instead of the default of 32 bytes.
The result displayed in the Command Prompt window will look something like this:
Reply from 172.217.1.142: bytes=1500 time=30ms TTL=54
Reply from 172.217.1.142: bytes=1500 time=30ms TTL=54
Reply from 172.217.1.142: bytes=1500 time=29ms TTL=54
Reply from 172.217.1.142: bytes=1500 time=30ms TTL=54
Reply from 172.217.1.142: bytes=1500 time=31ms TTL=54
Ping statistics for 172.217.1.142:
Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 29ms, Maximum = 31ms, Average = 30ms
The 0% loss reported under Ping statistics for 74.217.1.142 explains that each ICMP Echo Request message sent to www.google.com was returned. This means that, as far as this network connection goes, it can communicate with Google’s website just fine.
Ping localhost
ping 127.0.0.1
In the above example, we’re pinging 127.0.0.1, also called the IPv4 localhost IP address or IPv4 loopback IP address, without options.
Using the ping command with this address is an excellent way to test that Windows’ network features are working properly but it says nothing about your own network hardware or your connection to any other computer or device. The IPv6 version of this test would be ping ::1.
Find Hostname With Ping
ping -a 192.168.1.22
In this example, we’re asking the ping command to find the hostname assigned to the 192.168.1.22 IP address, but to otherwise ping it as normal.
The command might resolve the IP address, 192.168.1.22, as the hostname J3RTY22, for example, and then execute the remainder of the ping with default settings.
Ping Router Command
ping 192.168.2.1
Similar to the ping command examples above, this one is used to see if your computer can reach your router. The only difference here is that instead of using a ping command switch or pinging the localhost, we’re checking the connection between the computer and the router (192.168.2.1 in this case).
If you’re having trouble logging in to your router or accessing the internet at all, see if your router is accessible with this ping command, of course, replacing 192.168.2.1 with your router’s IP address.
Ping With IPv6
ping -t -6 SERVER
In this example, we force the ping command to use IPv6 with the -6 option and continue to ping SERVER indefinitely with the -t option. You can interrupt the ping manually with Ctrl+C.
The number after the % in the replies generated in this ping command example is the IPv6 Zone ID, which most often indicates the network interface used. You can generate a table of Zone IDs matched with your network interface names by executing netsh interface ipv6 show interface. The IPv6 Zone ID is the number in the Idx column.
Ping Related Commands
The ping command is often used with other networking related Command Prompt commands like tracert, ipconfig, netstat, and nslookup.
Other Ping Uses
Given the results you see above, it’s clear that you can also use the ping command to find a website’s IP address. Follow that link to learn more about how to do that.
You can also use ping on a Linux computer, and third-party ping tools exist as well which offer more features than the basic ping command.
FAQ
-
Open the command prompt and enter ping followed by the IP address of the DNS server (for example, ping 192.168.2.1).
-
To ping a port, use the telnet command. In the command prompt, enter telnet followed by the IP address (or the domain name) and the port number (for example, telnet 192.168.2.1 10).
Thanks for letting us know!
Get the Latest Tech News Delivered Every Day
Subscribe