Avatar of RonWensley
RonWensley
 asked on

Programmatically determine which eth0:x interfaces is requesting data

Greetings,

I have edited the /etc/network/interfaces file to provide me with several eth0:x interfaces. Now I want to programmatically determine which interface or IP address a request is coming through.

For example; I have eth0:1 = 192.168.227.205 and eth0:2 = 192.168.227.206
From my laptop I can ping either address.
In linux I want to run a program that responds differently based on the IP address being hit. If .205 do this if .206 do that.

How can I programmatically determine which IP address is being hit?
Linux NetworkingLinuxC++

Avatar of undefined
Last Comment
Tom Chadaravicius

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
RonWensley

ASKER
Thank you for the quick response.  I looked up some example code for this and it looks like it will work perfectly.
Tom Chadaravicius

There's getifaddrs function, defined in ifaddr.h. Calling it will retrieve an array of interfaces and IP addresses.
http://man7.org/linux/man-pages/man3/getifaddrs.3.html
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes