61

I am developing a code in c, which simply FTPs the files on FTP server by using curl utility, by following this example

now i have one simple thing to do, specify curl to use particular interface (IP) of the server for upload, as other interface of the server is associated with other socket connection.

is there any way to do it?

madiha malik
  • 827
  • 2
  • 8
  • 15

3 Answers3

122

Seems like curl support --interface option

curl --interface eth0 
MrFreezer
  • 1,627
  • 1
  • 10
  • 8
  • 2
    "interface" yes! thats exactly what i was looking for, thanks alot, CURLOPT_INTERFACE is the option to set in curl_easy_setopt (Y) – madiha malik Oct 11 '15 at 15:07
  • windows :` !curl -vvvik --interface tun0 https://ipinfo.io * Rebuilt URL to: https://ipinfo.io/ * Trying 34.117.59.81... * TCP_NODELAY set * Could not resolve host: tun0 * Couldn't bind to 'tun0'` – CS QGB Jan 05 '23 at 06:56
9

For anyone that comes here looking for the same answer, but for wlan0 or whatever the non-default interface is on Raspberry/RPi, you'll need to sudo or run as root.

In my case I was running eth0 and wlan0, but eth0 was my default NIC. Ping worked without sudo, curl didn't have verbose enough logging beyond saying the connection timed out and only worked without sudo on eth0. Traceroute finally led me to my answer when the error said "setsockopt SO_BINDTODEVICE: Operation not permitted"

Brandocious
  • 99
  • 1
  • 1
  • Thanks! It saved me a lot of looking around, especially since curl gives no indications that it's falling back to using the default interface! I'd suggest anyone wanting to use this first read the interface section of `man curl` . – aliqandil Jan 23 '20 at 02:38
  • This should be in the man page! Thanks – ikwyl6 Nov 15 '21 at 02:38
2

Look at your adapters, all adapters should have an IP address, just use the IP address in the curl query. In my case I only have 1 real adapter, so I wasn't sure how to specify secondary interface, so I grepped curl --help for interfaces and you can specify the IP address - Even better

# ip a<enter>

ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group de                                              fault qlen 1000
link/ether 00:50:56:9e:2a:42 brd ff:ff:ff:ff:ff:ff
inet 172.16.99.25/24 brd 172.16.99.255 scope global ens160
   valid_lft forever preferred_lft forever
inet 172.16.99.26/24 brd 172.16.99.255 scope global secondary ens160
   valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fe9e:2a42/64 scope link
   valid_lft forever preferred_lft forever

eric@mx:~$ curl --help | grep interface
 --dns-interface <interface> Interface to use for DNS requests
 --interface <name> Use network INTERFACE (or address)

# curl https://ipinfo.io/ip<enter> 
<This shows what your default external IP is>

curl --interface 172.16.99.26 https://ipinfo.io/ip
curl --interface 172.16.99.25 https://ipinfo.io/ip

On my server I get different answers back as I have (2) different NAT'd external IP's

Had to share, perhaps this will help someone else.

Babbage
  • 21
  • 1