Specify Source IP on Linux Command


It is a very desirable  function if you could specify source IP ( source Port will be extra useful) for network testing and  debugging.

Here is a short list of command syntax for commonly used tools in Linux

curl

curl http://10.101.26.104 --interface  6.7.8.9 --local-port 23456

dig

dig jian.p1.tps.a10lab.com @7.7.7.1 -b 50.35.26.100#23456

wget

wget --bind-address=6.7.8.9  http://10.101.26.104

wget could not specify source port;
but you could use the following trick to limit the source ports used in Linux:

check the config of file /proc/sys/net/ipv4/ip_local_port_range

cat /proc/sys/net/ipv4/ip_local_port_range
32768   60999

By default the source ports ranges from 32678 to 60999
change the file to limit to one source port

echo "23456 23456" > /proc/sys/net/ipv4/ip_local_port_range

Then test again, your wget command will use source port 23456


nslookup

Does not have any option

openssl

From some of the openssl man page, it shows it has an option of 

openssl s_client [-help] [-connect host:port] [-bind host:port

-bind host:port
This specifies the host address and or port to bind as the source for the connection. For Unix-domain sockets the port is ignored and the host is used as the source socket address.
 But tested and found out at least my openssl instance does not support such option :(

General Idea using local proxy

It is possible to write a script or function to add source IP/source Port support to any commands by providing a local proxy port before sending out to remote target IP.
It should be useful and I might spend some time working on this and update later.