如何固定以太口地址为指定IP?

电子说

1.2w人已加入

描述

rxw的RK3568的evb1公板,有2个以太口,

默认UI界面只能配置eth0,无法配置eth1,

实际应用中,有时需要一旦有网线插入,就需要该地址设置为指定IP地址

本文介绍2个最简单的方法实现固定IP。

一、通过修改ipconfig.txt

安卓设备以太口的配置信息保存在以下文件中:

 

/data/misc/ethernet/ipconfig.txt

 

该文件是二进制文件,而且默认只有eth0的配置信息,一口君通过一些特殊方法,生成了包含eth0、eth1的两个网口配置的配置文件,

并且rxw原厂的ui界面可以识别该配置文件。

配置文件用16进制格式打开内容如下:

 

00000000 00 00 00 03 00 0C 69 70 41 73 73 69 67 6E 6D 65 ......ipAssignme
00000010 6E 74 00 06 53 54 41 54 49 43 00 0B 6C 69 6E 6B nt..STATIC..link
00000020 41 64 64 72 65 73 73 00 0D 31 39 32 2E 31 36 38 Address..192.168
00000030 2E 34 30 2E 33 34 00 00 00 18 00 07 67 61 74 65 .40.34......gate
00000040 77 61 79 00 00 00 00 00 00 00 01 00 0C 31 39 32 way..........192
00000050 2E 31 36 38 2E 34 30 2E 31 00 03 64 6E 73 00 07 .168.40.1..dns..
00000060 30 2E 30 2E 30 2E 30 00 03 64 6E 73 00 07 30 2E 0.0.0.0..dns..0.
00000070 30 2E 30 2E 30 00 0D 70 72 6F 78 79 53 65 74 74 0.0.0..proxySett
00000080 69 6E 67 73 00 04 4E 4F 4E 45 00 02 69 64 00 04 ings..NONE..id..
00000090 65 74 68 30 00 03 65 6F 73 00 0C 69 70 41 73 73 eth0..eos..ipAss
000000A0 69 67 6E 6D 65 6E 74 00 06 53 54 41 54 49 43 00 ignment..STATIC.
000000B0 0B 6C 69 6E 6B 41 64 64 72 65 73 73 00 0D 31 39 .linkAddress..19
000000C0 32 2E 31 36 38 2E 32 2E 31 32 35 00 00 00 18 00 2.168.2.125.....
000000D0 07 67 61 74 65 77 61 79 00 00 00 00 00 00 00 01 .gateway........
000000E0 00 0B 31 39 32 2E 31 36 38 2E 32 2E 31 00 03 64 ..192.168.2.1..d
000000F0 6E 73 00 07 30 2E 30 2E 30 2E 30 00 03 64 6E 73 ns..0.0.0.0..dns
00000100 00 07 30 2E 30 2E 30 2E 30 00 0D 70 72 6F 78 79 ..0.0.0.0..proxy
00000110 53 65 74 74 69 6E 67 73 00 04 4E 4F 4E 45 00 02 Settings..NONE..
00000120 69 64 00 04 65 74 68 31 00 03 65 6F 73 -- -- -- id..eth1..eos

 

用 ascii格式打开如下【因为是二进制文件,会有部分内容是乱码】:

 

     ipAssignment STATIC  linkAddress 
192.168.40.34    gateway         192.168.40.1 dns 0.0.0.0 dns 0.0.0.0 
proxySettings NONE id eth0 eos  ipAssignment STATIC  linkAddress 
192.168.2.125    gateway         192.168.2.1 dns 0.0.0.0 dns 0.0.0.0 
proxySettings NONE id eth1 eos

 

总结一下,主要配置信息如下:

 

| 网口 |        ip        |      网关      |
|-----------------------------------------|
| eth0 |  192.168.40.34   |  192.168.40.1 |
| eth1 |  192.168.2.125   |  192.168.2.1  |

 

将该文件push进开发板,重启即可

 

adb root
adb remount
adb pull /data/misc/ethernet/ipconfig.txt

 

这样只要eth1对应的以太口up,就会自动设置ip地址192.168.2.125

同时并不影响UI配置eth0接口

注意:

这种方法仅在rxw3568+android11上测试通过,其他平台或者android版本没有测试。

二、通过开源项目ifplugd

除了第一种种方法还可以通过一个开源的项目ifplugd来实现。

通过ifplugd工具监听网口热插拔信息,然后执行指定脚本,将配置命令存放在脚本中即可。

ifplugd的移植需要用到libdaemon库,该库主要提供守护进程以及log接口功能。

1. 移植步骤

1)准备文件:

 

libdaemon-0.14.tar.gz
ifplugd-0.14.tar.gz

 

后台回复:eth,既可以获取

没有采用最新的libdaemon、ifplugd,会有版本兼容问题

本实例基于ndk编译器编译,

为了方便起见,我把libdaemon、ifplugd源文件全部拷贝到ndk实例工程下,libdaemon的头文件,拷贝了2次,

方便c文件包含。

 

.....src-ifplugd$ tree ./
./
├── Application.mk
├── daemon.h
├── dexec.c
├── dexec.h
├── dfork.c
├── dfork.h
├── dlog.c
├── dlog.h
├── dnonblock.c
├── dnonblock.h
├── dpid.c
├── dpid.h
├── dsignal.c
├── dsignal.h
├── ethtool-kernel.h
├── ethtool-local.h
├── ifplugd.c
├── ifstatus.c
├── include
├── interface.c
├── interface.h
├── libdaemon
│   ├── daemon.h
│   ├── dexec.h
│   ├── dfork.h
│   ├── dlog.h
│   ├── dnonblock.h
│   ├── dpid.h
│   └── dsignal.h
├── svn-revision.h
└── YROS.mk

2 directories, 29 files

 

2)修改配置文件

编译描述信息位于文件YROS.mk中,

【读者根据自己工程,文件会有所不同】

YROS.mk修改如下:

 

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_SRC_FILES:= 
 ifplugd.c  interface.cdexec.c dfork.c dlog.c dnonblock.c  dpid.c dsignal.c   
 

LOCAL_SHARED_LIBRARIES := 

LOCAL_MODULE_TAGS := optional
LOCAL_CLANG := true

LOCAL_MODULE:= ethcheckd

include $(BUILD_EXECUTABLE)

 

3)编译

编译错误1

 

λ build.bat                                                                                                                         
                                                                                                                                    
ndk-build.cmd                                                                                                                       
[armeabi-v7a] Compile thumb  : ifplugd <= ifplugd.c                                                                                 
src/ifplugd.c13: error: use of undeclared identifier 'SYSCONFDIR'                                                               
char *run = SYSCONFDIR"/ifplugd/ifplugd.action";                                                                                    
            ^                                                                                                                       
src/ifplugd.c23: error: expected ';' after top level declarator                                                                 
char *run = SYSCONFDIR"/ifplugd/ifplugd.action";                                                                                    
                      ^                                                                                                             
                      ;                                                                                                             
src/ifplugd.c18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced]                                        
    FD_SET(sigfd = daemon_signal_fd(), &rfds);                                                                                      
                 ^                                                                                                                  
H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h50: note: expanded from macro 'FD_SET'                            
#define FD_SET(fd, set) (__FDS_BITS(set)[__FDELT(fd)] |= __FDMASK(fd))                                                              
                                                 ^                ~~                                                                
H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h23: note: expanded from macro '__FDELT'                           
#define __FDELT(fd) ((fd) / NFDBITS)                                                                                                
                      ^                                                                                                             
src/ifplugd.c36: error: expected ')'                                                                                           
    daemon_log(LOG_INFO, "ifplugd "VERSION" successfully initialized, link beat %sdetected.", status == IFSTATUS_UP ? "" : "not "); 
                                   ^                                                                                                
src/ifplugd.c15: note: to match this '('                                                                                       
    daemon_log(LOG_INFO, "ifplugd "VERSION" successfully initialized, link beat %sdetected.", status == IFSTATUS_UP ? "" : "not "); 
              ^                                                                                                                     
src/ifplugd.c18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced]                                        
    FD_SET(sigfd = daemon_signal_fd(), &rfds);                                                                                      
                 ^                                                                                                                  
H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h50: note: expanded from macro 'FD_SET'                            
#define FD_SET(fd, set) (__FDS_BITS(set)[__FDELT(fd)] |= __FDMASK(fd))                                                              
                                                 ^                ~~                                                                
H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h23: note: expanded from macro '__FDELT'                           
#define __FDELT(fd) ((fd) / NFDBITS)                                                                                                
                      ^                                                                                                             
src/ifplugd.c26: error: expected ')'                                                                                           
        printf("ifplugd "VERSION" (SVN: "SVN_REVISION")
");                                                                        
                         ^                                                                                                          
src/ifplugd.c15: note: to match this '('                                                                                       
        printf("ifplugd "VERSION" (SVN: "SVN_REVISION")
");                                                                        
              ^                                                                                                                     
2 warnings and 4 errors generated.                                                                                                  
make: *** [obj/local/armeabi-v7a/objs/ifplugd/ifplugd.o] Error 1                                                                    
接口请添加图片描述

 

修改文件ifplugd.c

 

59  #define VARRUN "/system"
63  #define SYSCONFDIR ""
64  #define VERSION "yikoulinux"

 

编译错误2

 

λ build.bat

ndk-build.cmd
[armeabi-v7a] Compile thumb  : ifplugd <= dexec.c
[armeabi-v7a] Compile thumb  : ifplugd <= dfork.c
[armeabi-v7a] Compile thumb  : ifplugd <= dlog.c
[armeabi-v7a] Compile thumb  : ifplugd <= dnonblock.c
[armeabi-v7a] Compile thumb  : ifplugd <= dpid.c
src/dpid.c43: error: use of undeclared identifier 'LOCALSTATEDIR'
    snprintf(fn, sizeof(fn), "%s/%s.pid", VARRUN, daemon_pid_file_ident ? daemon_pid_file_ident : "unknown");
                                          ^
src/dpid.c16: note: expanded from macro 'VARRUN'
#define VARRUN LOCALSTATEDIR "/run"
               ^
1 error generated.
make: *** [obj/local/armeabi-v7a/objs/ifplugd/dpid.o] Error 1

 

修改文件dpid.c

 

50 #define LOCALSTATEDIR

 

4) 编译成功

编译成功log如下:

 

H:compileforandroid                                                                                                     
λ build.bat                                                                                                              
                                                                                                                         
ndk-build.cmd                                                                                                            
[armeabi-v7a] Compile thumb  : ifplugd <= ifplugd.c                                                                      
src/ifplugd.c53: warning: missing sentinel in function call [-Wsentinel]                                            
        execl(run, run, interface, arg, extra_arg, 0);                                                                   
                                                    ^                                                                    
                                                    , NULL                                                               
H:/yros-ndk-windows/build//../sysroot/usr/includeunistd.h5: note: function has been explicitly marked sentinel here
int execl(const char* __path, const char* __arg0, ...) __attribute__((__sentinel__));                                    
    ^                                                                                                                    
src/ifplugd.c18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced]                             
    FD_SET(sigfd = daemon_signal_fd(), &rfds);                                                                           
                 ^                                                                                                       
H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h50: note: expanded from macro 'FD_SET'                 
#define FD_SET(fd, set) (__FDS_BITS(set)[__FDELT(fd)] |= __FDMASK(fd))                                                   
                                                 ^                ~~                                                     
H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h23: note: expanded from macro '__FDELT'                
#define __FDELT(fd) ((fd) / NFDBITS)                                                                                     
                      ^                                                                                                  
src/ifplugd.c18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced]                             
    FD_SET(sigfd = daemon_signal_fd(), &rfds);                                                                           
                 ^                                                                                                       
H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h50: note: expanded from macro 'FD_SET'                 
#define FD_SET(fd, set) (__FDS_BITS(set)[__FDELT(fd)] |= __FDMASK(fd))                                                   
                                                 ^                ~~                                                     
H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h23: note: expanded from macro '__FDELT'                
#define __FDELT(fd) ((fd) / NFDBITS)                                                                                     
                      ^                                                                                                  
3 warnings generated.                                                                                                    
[armeabi-v7a] Compile thumb  : ifplugd <= interface.c                                                                    
[armeabi-v7a] Compile thumb  : ifplugd <= dexec.c                                                                        
[armeabi-v7a] Compile thumb  : ifplugd <= dfork.c                                                                        
[armeabi-v7a] Compile thumb  : ifplugd <= dlog.c                                                                         
[armeabi-v7a] Compile thumb  : ifplugd <= dnonblock.c                                                                    
[armeabi-v7a] Compile thumb  : ifplugd <= dpid.c                                                                         
[armeabi-v7a] Compile thumb  : ifplugd <= dsignal.c                                                                      
[armeabi-v7a] Executable     : ifplugd                                                                                   
[armeabi-v7a] Install        : ifplugd => libs/armeabi-v7a/ifplugd                                                       
接口在这里插入图片描述

 

程序位置如下:

 

libsarmeabi-v7aifplugd

 

测试

1. 查看ifplugd参数

 

ifplugd -h
rk3568_r:/ # ifplugd -h
ifplugd [options]
   -a --no-auto              Do not enable interface automatically (off)
   -n --no-daemon            Do not daemonize (for debugging) (off)
   -s --no-syslog            Do not use syslog, use stderr instead (for debugging) (off)
   -b --no-beep              Do not beep (off)
   -f --ignore-fail          Ignore detection failure, retry instead (failure is treated as DOWN) (off)
   -F --ignore-fail-positive Ignore detection failure, retry instead (failure is treated as UP) (off)
   -i --iface=IFACE          Specify ethernet interface (eth0)
   -r --run=EXEC             Specify program to execute (/ifplugd/ifplugd.action)
   -I --ignore-retval        Don't exit on nonzero return value of program executed (off)
   -t --poll-time=SECS       Specify poll time in seconds (1)
   -u --delay-up=SECS        Specify delay for configuring interface (0)
   -d --delay-down=SECS      Specify delay for deconfiguring interface (5)
   -m --api-mode=MODE        Force API mode (mii, priv, ethtool, wlan, auto) (auto)
   -q --no-shutdown          Don't run script on daemon quit (off)
   -w --wait-on-fork         Wait until daemon fork finished (off)
   -x --extra-arg            Specify an extra argument for action script
   -h --help                 Show this help
   -k --kill                 Kill a running daemon
   -c --check-running        Check if a daemon is currently running
   -v --version              Show version
   -S --suspend              Suspend running daemon
   -R --resume               Resume running daemon
   -z --info                 Write status of running daemon to syslog

 

2. 增加脚本if.sh

当网口eth1 up后,ifplugd会执行如下命令【下面动作由ifplugd自动执行】:

 

/system/if.sh eth1 up

 

根据参数顺序,编写脚本if.sh如下:

 

#!/bin/bash

IPADDR=192.168.40.8
ETHPORT=eth1
echo "daniel peng set" $ETHPORT $IPADDR
echo $#
echo $0
echo $1
echo $2
if [ $# -eq 2 ];then
 if [ $1 = $ETHPORT ];then 
 echo $ETHPORT
 if [ $2 = "up" ];then
 ifconfig $ETHPORT $IPADDR
 sleep 1
 ip rule add from all lookup main pref 9000 
 sleep 1
 echo 1 > /proc/sys/net/ipv4/ip_forward 
 iptables -F
 echo "set" $ETHPORT "done"
 elif [ $2 = "down" ];then
 echo "down"
 elif [ $2 = "disable" ];then
 echo "disable"
 elif [ $2 = "error" ];then
 echo "error"
 fi
 fi
fi

 

功能:

网口eth1 up后,设置该网口地址为IPADDR,即:192.168.40.8

读者可以根据自己的需要编写相应的脚本。

3. 运行ifplugd

ifplugd监听网口eth1并且执行后面的脚本文件

在板子上输入以下命令:

 

ifplugd -i eth1 -r "sh /system/if.sh"

 

查看该守护进程:

 

H:compileforandroid
λ adb shell
rk3568_r:/ # ifplugd -i eth1 -r "sh /system/if.sh"
rk3568_r:/ # ps -ef | grep if
wifi            280      1 0 0952 ?     0000 android.hardware.wifi@1.0-service
wifi            385      1 0 0953 ?     0000 wificond
root           1826      1 0 1006 ?     0000 ifplugd -i eth1 -r sh /system/if.sh
root           1834   1819 2 1017 pts/0 0000 grep if

 

一旦网口插入网线后,地址就会被自动设置。

补充

ifplugd的确可以实现监测网卡的状态,并执行相应脚本,

但是有个前提,就是网口已经注册到系统中,即用ifconfig -a能查看到

如何是usb网口这种设备,在插入usb口之后网口设备才会注册,

那么这种情况下,要想设置usb网卡,那么就就需要修改ifplugd程序。

下文,给大家讲解如何自己实现一个简单的网口检测并设置ip的小程序

  审核编辑:汤梓红

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分