用strace工具观察一下ifconfig eth0的运行内幕。
$ strace ifconfig eth0 execve("/sbin/ifconfig", ["ifconfig", "eth0"], [/* 71 vars */]) = 0 brk(0) = 0x9565000 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb76fe000 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 fstat64(3, {st_mode=S_IFREG|0644, st_size=114965, ...}) = 0 mmap2(NULL, 114965, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb76e1000 close(3) = 0 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory) open("/lib/i386-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0P\234\1\0004\0\0\0"..., 512) = 512 fstat64(3, {st_mode=S_IFREG|0755, st_size=1763068, ...}) = 0 mmap2(NULL, 1772156, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7530000 mmap2(0xb76db000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1aa000) = 0xb76db000 mmap2(0xb76de000, 10876, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb76de000 close(3) = 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb752f000 set_thread_area({entry_number:-1 -> 6, base_addr:0xb752f940, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0 mprotect(0xb76db000, 8192, PROT_READ) = 0 mprotect(0x8058000, 4096, PROT_READ) = 0 mprotect(0xb7724000, 4096, PROT_READ) = 0 munmap(0xb76e1000, 114965) = 0 brk(0) = 0x9565000 brk(0x9586000) = 0x9586000 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE|O_CLOEXEC) = 3 fstat64(3, {st_mode=S_IFREG|0644, st_size=8752496, ...}) = 0 mmap2(NULL, 2097152, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb732f000 mmap2(NULL, 4096, PROT_READ, MAP_PRIVATE, 3, 0x855000) = 0xb76fd000 close(3) = 0 uname({sys="Linux", node="idorax", ...}) = 0 access("/proc/net", R_OK) = 0 access("/proc/net/unix", R_OK) = 0 socket(PF_LOCAL, SOCK_DGRAM, 0) = 3 socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 4 access("/proc/net/if_inet6", R_OK) = 0 socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 5 access("/proc/net/ax25", R_OK) = -1 ENOENT (No such file or directory) access("/proc/net/nr", R_OK) = -1 ENOENT (No such file or directory) access("/proc/net/rose", R_OK) = -1 ENOENT (No such file or directory) access("/proc/net/ipx", R_OK) = -1 ENOENT (No such file or directory) access("/proc/net/appletalk", R_OK) = -1 ENOENT (No such file or directory) access("/proc/sys/net/econet", R_OK) = -1 ENOENT (No such file or directory) access("/proc/sys/net/ash", R_OK) = -1 ENOENT (No such file or directory) access("/proc/net/x25", R_OK) = -1 ENOENT (No such file or directory) open("/proc/net/dev", O_RDONLY) = 6 fstat64(6, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb76fc000 read(6, "Inter-| Receive "..., 1024) = 447 close(6) = 0 munmap(0xb76fc000, 4096) = 0 ioctl(5, SIOCGIFFLAGS, {ifr_name="eth0", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0 ioctl(5, SIOCGIFHWADDR, {ifr_name="eth0", ifr_hwaddr=00:25:64:ba:8d:be}) = 0 ioctl(5, SIOCGIFMETRIC, {ifr_name="eth0", ifr_metric=0}) = 0 ioctl(5, SIOCGIFMTU, {ifr_name="eth0", ifr_mtu=1500}) = 0 ioctl(5, SIOCGIFMAP, {ifr_name="eth0", ifr_map={mem_start=0, mem_end=0, base_addr=0, irq=16, dma=0, port=0}}) = 0 ioctl(5, SIOCGIFMAP, {ifr_name="eth0", ifr_map={mem_start=0, mem_end=0, base_addr=0, irq=16, dma=0, port=0}}) = 0 ioctl(5, SIOCGIFTXQLEN, {ifr_name="eth0", ifr_qlen=1000}) = 0 open("/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 6 fstat64(6, {st_mode=S_IFREG|0644, st_size=2570, ...}) = 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb76fc000 read(6, "# Locale name alias data base.\n#"..., 4096) = 2570 read(6, "", 4096) = 0 close(6) = 0 munmap(0xb76fc000, 4096) = 0 open("/usr/share/locale/en_US/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/locale/en/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/locale-langpack/en_US/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/locale-langpack/en/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory) ioctl(4, SIOCGIFADDR, {ifr_name="eth0", ifr_addr={AF_INET, inet_addr("192.168.1.102")}}) = 0 ioctl(4, SIOCGIFDSTADDR, {ifr_name="eth0", ifr_dstaddr={AF_INET, inet_addr("192.168.1.102")}}) = 0 ioctl(4, SIOCGIFBRDADDR, {ifr_name="eth0", ifr_broadaddr={AF_INET, inet_addr("192.168.1.255")}}) = 0 ioctl(4, SIOCGIFNETMASK, {ifr_name="eth0", ifr_netmask={AF_INET, inet_addr("255.255.255.0")}}) = 0 fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 6), ...}) = 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb76fc000 write(1, "eth0 Link encap:Ethernet H"..., 58eth0 Link encap:Ethernet HWaddr 00:25:64:ba:8d:be ) = 58 write(1, " inet addr:192.168.1.10"..., 75 inet addr:192.168.1.102 Bcast:192.168.1.255 Mask:255.255.255.0 ) = 75 open("/proc/net/if_inet6", O_RDONLY) = 6 fstat64(6, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb76fb000 read(6, "00000000000000000000000000000001"..., 1024) = 108 read(6, "", 1024) = 0 write(1, " inet6 addr: fe80::225:"..., 61 inet6 addr: fe80::225:64ff:feba:8dbe/64 Scope:Link ) = 61 read(6, "", 1024) = 0 close(6) = 0 munmap(0xb76fb000, 4096) = 0 write(1, " UP BROADCAST RUNNING M"..., 61 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 ) = 61 write(1, " RX packets:31318 error"..., 65 RX packets:31318 errors:0 dropped:0 overruns:0 frame:0 ) = 65 write(1, " TX packets:29503 error"..., 67 TX packets:29503 errors:0 dropped:0 overruns:0 carrier:0 ) = 67 write(1, " collisions:0 txqueuele"..., 40 collisions:0 txqueuelen:1000 ) = 40 write(1, " RX bytes:29127563 (29."..., 65 RX bytes:29127563 (29.1 MB) TX bytes:4104503 (4.1 MB) ) = 65 write(1, " Interrupt:16 \n", 24 Interrupt:16 ) = 24 write(1, "\n", 1 ) = 1 close(5) = 0 exit_group(0) = ? +++ exited with 0 +++
注意上面的socket()和ioctl(), 我们不难发现如下关键方法了.
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 4
ioctl(4, SIOCGIFADDR, {ifr_name="eth0", ifr_addr={AF_INET, inet_addr("192.168.1.102")}}) = 0
ioctl(4, SIOCGIFNETMASK, {ifr_name="eth0", ifr_netmask={AF_INET, inet_addr("255.255.255.0")}}) = 0
参考:
https://www.cnblogs.com/idorax/p/7329276.html