ifconfig显示接口,但是if_nametoindex()找不到它

时间:2018-09-06 15:24:50

标签: sockets libc

键入ifconfig,我得到以下信息:

~$ ifconfig
vcan0     Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          UP RUNNING NOARP  MTU:72  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

但是,当尝试获取“ vcan0”的接口索引时,什么都没有返回(在这种情况下使用python):

avidane@u42cfffc3cd2b51:~$ python3.5                                                                 
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> import ctypes.util                                                                                                                                                                                      
>>> libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True)
>>> ifidx = libc.if_nametoindex("vcan0")
>>> print(ifidx)
0

那怎么可能?返回的错误号为19,据我所知,它的意思是"no such device"

顺便说一句。我需要索引才能创建sockaddr_can结构并将其绑定。如果有更好的方法来获取索引,我将不知所措。

0 个答案:

没有答案
相关问题