/dev/rob0 <(E-Mail Removed)> wrote:
>In article <(E-Mail Removed)> ,
> Rohan Romanus Almeida wrote:
>> What i wanted was that, if an interface goes down,
>> shouldn't the entry vanish in /proc/net/dev ?
>> Or do I have to monitor the output of `ifconfig` command?
>
>Well, since ifconfig reads somewhere in /proc for its information, you
>would likely do best doing the same. Note that when an interface goes
Ifconfig uses ioctl commands to obtain information.
If the OP needs a shell script, just using ifconfig and grep
will tell if a given interface is up or not:
#!/bin/sh
#
if [ $# -ne 1 ]; then
echo "No interface specified."
exit 6
fi
if ifconfig | grep -q "^${1}" ; then
echo -e "$1\t is up."
exit 0
fi
echo -e "$1\t is down."
exit 1
Note also that ifconfig with no arguments tells if the interface
is up, while "ifconfig -a", or "ifconfig interface_name" also
shows interfaces that have been configured but are currently
down.
If the OP needs a C program it isn't doesn't take much more.
>down its routing table (/proc/net/route) entry is removed. You could
>look in the ifconfig source to find from whence it reads interface
>status flags.
True, but it is a bit convoluted (to put it mildly) and trying
to figure out exactly how it works from netlib is probably not
the easiest way. The easy way... is google (of course). There
have been many examples posted. The C program below, while not
in exactly the same form provided this time, has been posted a
dozen times.
This is a program which demonstrates how to either get all
interfaces that are configured and up, or how to specify a
single interface and determine its status. Note that unlike
ifconfig, there is no way to distinguish between a previously
configured interface that is now down and one that has never
been configured at all or is non-existant.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#define inaddrr(x) (*(struct in_addr *) &ifr->x[sizeof sa.sin_port])
#define IFRSIZE ((int)(size * sizeof (struct ifreq)))
int main(int argc, char *argv[])
{
int sockfd, size = 1;
struct sockaddr_in sa;
struct ifconf ifc;
struct ifreq *ifr;
if (0 > (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP))) {
perror("socket");
exit(EXIT_FAILURE);
}
/* look for just one interface */
if (argv[1] && argv[1][0] ) {
struct ifreq ifreq;
struct sockaddr_in *sin = (struct sockaddr_in *)&ifreq.ifr_addr;
ifr = &ifreq;
memset(ifr, 0, sizeof *ifr);
strcpy(ifr->ifr_name, argv[1]);
/*
* this is unnecessary, and shown only to demonstrate that
* the SIOCGIFFLAGS ioctl command indicates whether the
* interface is up or down. (The SIOCGIFADDR command does
* likewise.)
*/
if (0 != ioctl(sockfd, SIOCGIFFLAGS, ifr)) {
printf("%-7s: is down\n", ifr->ifr_name);
}
if (0 == ioctl(sockfd, SIOCGIFADDR, ifr)) {
printf("%-7s: %s\n", ifr->ifr_name, inet_ntoa(sin->sin_addr));
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}
/* list all interfaces that are up */
ifc.ifc_len = IFRSIZE;
ifc.ifc_req = NULL;
do {
++size;
/* realloc buffer size until no overflow occurs */
if (NULL == (ifc.ifc_req = realloc(ifc.ifc_req, IFRSIZE))) {
fprintf(stderr, "Out of memory.\n");
exit(EXIT_FAILURE);
}
ifc.ifc_len = IFRSIZE;
if (ioctl(sockfd, SIOCGIFCONF, &ifc)) {
perror("ioctl SIOCFIFCONF");
exit(EXIT_FAILURE);
}
} while (IFRSIZE <= ifc.ifc_len);
ifr = ifc.ifc_req;
for (;(char *) ifr < (char *) ifc.ifc_req + ifc.ifc_len; ++ifr) {
if (!ioctl(sockfd, SIOCGIFFLAGS, ifr)) {
printf("%-7s: %s\n", ifr->ifr_name, inet_ntoa(inaddrr(ifr_addr.sa_data)));
}
}
return EXIT_SUCCESS;
}
--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)
(E-Mail Removed)