0

I am new in network and I don't know how to config properly.

I can ping to server

ping -I 192.168.42.1 42.112.178.185

but I couldn't ping

ping 42.112.178.185

42.112.178.185 is only allow accept for 192.168.42.0/24.

How could I add route table ? I would like to use

ping 42.112.178.185 

Instead of

ping -I 192.168.42.1 42.112.178.185

I am trying to call http://42.112.178.185 with curl and need to route from 192.168.42.1.

saturngod
  • 837
  • 2
  • 10
  • 12
  • 1
    btw, curl can take an `--interface` parameter that works much like ping's `-I` – A.B Mar 01 '18 at 20:11
  • @A.B, wow. thanks a lot. I was busy for adding route table and now solved with interface. Thanks alot. Can you do as answer instead of comment ? I will mark as correct answer. – saturngod Mar 02 '18 at 18:36
  • Done. I left the previous solution and added the simpler curl option above it. – A.B Mar 02 '18 at 23:49

1 Answers1

1

UPDATE As wished, here's a solution for the specific problem: have curl use the right source ip when connecting, by telling it to bind to it with the --interface option before connecting.

From the curl man page:

--interface

Perform an operation using a specified interface. You can enter interface name, IP address or host name.

So the problem can be solved by just using this curl command to reach 42.112.178.185 with the right IP:

curl --interface 192.168.42.1 http://42.112.178.185/

Of course you can also specify the interface.


I'm still leaving the routing solution below, in case it can help somebody.


A route can indeed be added that will tell to use an other source IP than the default IP (the one belonging to the gateway's network) when reaching the target.

note: Because some required informations were not given: the default gateway's IP and the interface to reach it, some additional scripting is required to find out its value. Also because there could be a tunnel which might not have replaced the default route but might have added two "half default routes" 0.0.0.0/1 and 128.0.0.0/1, I'll get the route to 42.112.178.185 to have the correct value.

So let's assign the gw IP to gw:

gw=$(ip -o route get 42.112.178.185 | sed -n 's/^.* via \([^ ][^ ]*\) *.*$/\1/p')

Now here's the simple routing command:

ip route add 42.112.178.185 src 192.168.42.1 via $gw

If for example the gateway was 10.0.8.1, with Ubuntu's supposed tun0's supposed ip 10.0.8.2, before the change, querying the route to 42.112.178.185 would give:

# ip route get 42.112.178.185
42.112.178.185 via 10.0.8.1 dev tun0 src 10.0.8.2 
    cache 

After the change that would become:

# ip route get 42.112.178.185
42.112.178.185 via 10.0.8.1 dev tun0 src 192.168.42.1
    cache

The ip route command can be changed to affect more than this single ip, but what to do depends on the other routes already present.

A.B
  • 11,090
  • 2
  • 24
  • 45