36

Ping using specific gateway interface or source IP address

 6 years ago
source link: https://www.tuicool.com/articles/hit/NvYVB3b
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

I have multiple gateways configured on my Linux or Unix based router. How do I ping through a specific interface of the gateway? How can I ping using a particular source IP address?

Introduction : The ping command

determines whether a network destination is reachable. ping is one of the useful command line tools to get info about computers including the LAN and Internet. One can use ping command to find whether a specific IP address is accessible from your host. Another usage of ping is to find out the distance (round trip) between any two network routers or other network devices such as switches, firewall, websites, hosts and other stuff. This page shows ping using specific gateway interface and ping using specific source IP address.

How to ping using specific gateway interface or source IP address

Initially, Unix-like systems had ping command. But, versions of ping are now available for all leading operating systems such as Microsoft Windows and others. The version discusses here only tested on a Linux, FreeBSD, and Unix-like system. The ping command works by sending ICMP packets to the target IP address and listening for returned packets. The syntax is:

ping ip-address
 ping domain-name
 ping www.cyberciti.biz
 ping 192.168.2.254
 ping [options] destination
 ping [options] [IP|hostname]

r6fuUrR.png!web

Pass the -c option to the ping command to send out only the number of packets. For example, to send out only four packets to the IP address 8.8.8.8:

ping -c 4 8.8.8.8

Ping using specific gateway interface

To find out your interface names on a Unix-like or *BSD system run the ifconfig command:

ifconfig
 ifconfig -a

Linux users use theip command or ifconfig command:

ip a

You need to pass the -I option as follows:

ping -I interface destination
ping -I eth0 www.cyberciti.biz
ping -I tun0 1.1.1.1
ping -I br0 8.8.8.8
##################################################
### for bsd ping use interface IP address ########
### for e.g. vio0 interface has 192.168.122.124 ##
##################################################
ping -c4 -I 192.168.122.124 www.cyberciti.biz
ping -c4 -I tun0 www.cyberciti.biz

uauY3e6.png!web

Ping using specific source IP address

One can set socket sndbuf. The syntax is:

ping -S ip dest
 ping -S 192.168.2.24 www.cyberciti.biz
 ping -c 4 -S 10.207.0.54 1.1.1.1
YZNZfyE.png!web

Putting it all tougher – Ping using specific source IP address

Here is a shell script that ping given domain/IP address using three wan interface configured on my FreeBSD based firewall/router. The script usefor loop and ping command:

#!/bin/sh
# Purpose : Linux ping from specific source ip address.
# Ping three differnet ISPs and find out the fastest 
# ping round time for domain www.cyberciti.biz.
# --------------------------------------------------
# Tested on FreeBSD and Linux only.
# --------------------------------------------------
# License: GPL version 3.0
# Author: Vivek Gite {https://www.cyberciti.biz}
# --------------------------------------------------
d="www.cyberciti.biz" # set me 
 
#
# my three ISPs A, B, and C with their interfaces IPv4 
#
for e in A:192.168.1.254 B:192.168.0.1 C:10.207.0.54
do	
	IFS=':'
	set -- $e
	isp="$1"
	ipv4="$2"
	echo "*********************************"
	echo "Ping "$isp" ISP using $ipv4 IP..."
	echo "*********************************"
	ping -c 4 -S${ipv4} "${d}"
done

NjYvMbF.png!web

Seems like “ISP A” offers the better option as it takes less time to make a complete round trip.

Conclusion

You just learned how to ping from a specific source IP and interface on a Linux, *BSD, macOS and Unix-like system. I strongly suggest that you read the man page of theping command using the man command:

man ping

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics viaRSS/XML feed or weekly email newsletter .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK