C语言+shell 实现linux网卡状态检测 和 C语言实现linux网卡连接检测 2文的方法各有缺陷,比如有些系统执行ifconfig需要root权限,要不就不支持ioctl(fd, SIOCGMIIPHY, &ifr)这样的操作。以下给出了C语言实现linux网卡连接检测 的改进版实现与C语言+shell 实现linux网卡状态检测 同样的功能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <linux/if.h> #include <linux/mii.h> #include <linux/sockios.h> #include <errno.h> #include <ifaddrs.h> #include <arpa/inet.h> #include <linux/ethtool.h> int cshell_netlink_status( char *if_name) { char buffer[BUFSIZ]; char cmd[100]; FILE *read_fp; int chars_read; int ret =0; memset ( buffer, 0, BUFSIZ ); memset ( cmd, 0, 100 ); sprintf (cmd, "ifconfig -a | grep %s" ,if_name); read_fp = popen(cmd, "r" ); if ( read_fp != NULL ) { chars_read = fread (buffer, sizeof ( char ), BUFSIZ-1, read_fp); pclose(read_fp); if (chars_read > 0) { ret = 1; } else { fprintf (stderr, "DEVICE_NONE\r\n" ); return 0; } } if (ret == 1) { memset ( buffer, 0, BUFSIZ ); memset ( cmd, 0, 100 ); sprintf (cmd, "ifconfig |grep %s" ,if_name); read_fp = popen(cmd, "r" ); if ( read_fp != NULL ) { chars_read = fread (buffer, sizeof ( char ), BUFSIZ-1, read_fp); pclose(read_fp); if (chars_read > 0) { ret = 2; } else { fprintf (stderr, "DEVICE_DOWN\r\n" ); return 1; } } } if (ret == 2) { memset ( buffer, 0, BUFSIZ ); memset ( cmd, 0, 100 ); sprintf (cmd, "ifconfig %s | grep RUNNING | awk '{print $3}'" ,if_name); read_fp = popen(cmd, "r" ); if ( read_fp != NULL ) { chars_read = fread (buffer, sizeof ( char ), BUFSIZ-1, read_fp); pclose(read_fp); if (chars_read > 0) { fprintf (stderr, "DEVICE_LINKED\r\n" ); return 3; } else { fprintf (stderr, "DEVICE_UNPLUGGED\r\n" ); return 2; } } } return -1; } int c_netlink_status( const char *if_name ) { int fd = -1; struct ifreq ifr; struct ifconf ifc; struct ifreq ifrs_buf[100]; int if_number =0; int i; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { fprintf (stderr, "%s: socket error [%d] %s\r\n" ,if_name, errno , strerror ( errno )); close(fd); return -1; } ifc.ifc_len = sizeof (ifrs_buf); ifc.ifc_buf = (caddr_t)ifrs_buf; if (ioctl(fd, SIOCGIFCONF, ( char *)&ifc) <0) { fprintf (stderr, "%s: ioctl SIOCGIFCONF error [%d] %s\r\n" ,if_name, errno , strerror ( errno )); close(fd); return -1; } if_number = ifc.ifc_len / sizeof ( struct ifreq); for (i=0; i< if_number; i++) { if ( strcmp (if_name,ifrs_buf[i].ifr_name ) == 0) { break ; } } if (i >= if_number) { close(fd); fprintf (stderr, "DEVICE_NONE\r\n" ); return 0; } bzero(&ifr, sizeof (ifr)); strncpy (ifr.ifr_name, if_name, IFNAMSIZ-1); ifr.ifr_name[IFNAMSIZ-1] = 0; if (ioctl(fd, SIOCGIFFLAGS, ( char *)&ifr) <0) { fprintf (stderr, "%s: ioctl SIOCGIFFLAGS error [%d] %s\r\n" ,if_name, errno , strerror ( errno )); close(fd); return -1; } #if 1 if (!(ifr.ifr_flags & IFF_UP)) { close(fd); fprintf (stderr, "DEVICE_DOWN\r\n" ); return 1; } if (!(ifr.ifr_flags & IFF_RUNNING)) { close(fd); fprintf (stderr, "DEVICE_UNPLUGGED\r\n" ); return 2 ; } fprintf (stderr, "DEVICE_LINKED\r\n" ); return 3; #else { struct ethtool_value edata; if (!(ifr.ifr_flags & IFF_UP) || !(ifr.ifr_flags & IFF_RUNNING)) { close(fd); fprintf (stderr, "%s: DOWN\r\n" ,if_name); return 1; } edata.cmd = ETHTOOL_GLINK; edata.data = 0; ifr.ifr_data = ( char *) &edata; if (ioctl( fd, SIOCETHTOOL, &ifr ) < 0) { fprintf (stderr, "%s: ioctl SIOCETHTOOL error [%d] %s\r\n" ,if_name, errno , strerror ( errno )); close(fd); return -1; } if (edata.data == 0) { fprintf (stderr, "DEVICE_UNPLUGGED\r\n" ); return 2; } else { fprintf (stderr, "DEVICE_LINKED\r\n" ); return 3; } } #endif } int main( int argc, char * argv[]) { int i=0; if (argc != 2) { fprintf (stderr, "usage: %s <ethname>\r\n" , argv[0]); return -1; } i = cshell_netlink_status(argv[1]); printf ( "cshell_netlink_status if_status = %d\n" , i ); i = c_netlink_status(argv[1]); printf ( "c_netlink_status if_status = %d\n" , i ); return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/handyhuang/article/details/17604623