Verify network connection
Hi,
I have more than 2 network interfaces and I need to check the network connection before seding packets down to it in my C++ program. What is the best way to verify network connections and to get it's subnet address in a C++ program? For example, I have eth0 and eth1 and I need to do following in my program: 1. Verify if eth0 or eth1 has been configurated. 2. Connection to eth0 or eth1 can get through. 3. Get its subnet IP address. Thank you. Jim |
This sort of thing could be quite easily done using a combination of ifconfig, ping, sed and awk in a Bash script.
Did you want the solution to be in C++, or is it completely open? |
Verify network connection
I can be a C program as well, any bash script can also be implemented in C program. Wuold you please explain more details?
Thank you. Jim |
Wonderful. Unfortunately, I know the way to go about doing this, I just don't know the exact syntax. Sed and Awk are somewhat of a mystery to me, but I'm sure you could more than easily figure it out. :)
1. Verify if eth0 or eth1 has been configurated. Running ifconfig and then piping the output to sed/awk to parse the lines that come out for the "inet addr:XXX.XXX.XXX.XXX" section (located in the second line of the output). 2. Connection to eth0 or eth1 can get through. Now, I'm not quite sure what you mean by this, but if you're on a network with access to the internet, pinging a website (or something of the like) will let you know if network traffic can get through. If you're on an intranet with no contact with the outside world, then ping the router, or destination and see if the packets are received and acknowledged. Code:
$ ping -c4 192.168.1.100 3. Get its subnet IP address. This step could be rolled into step one, as you may want to avoid the few extra flops it would take to re-run a particular process (ifconfig). The subnet IP address (the "broadcast" address), is also in the second line of the output, and is the next printed value. Here's what my ifconfig output looks like for eth0: Code:
eth0 Link encap:Ethernet HWaddr 00:40:f4:19:c5:f9 |
All times are GMT -5. The time now is 12:04 AM. |