首页 > 其他分享 >瑞芯微 | 如何固定以太口地址为指定ip?ifplugd妙用

瑞芯微 | 如何固定以太口地址为指定ip?ifplugd妙用

时间:2023-10-10 22:12:58浏览次数:37  
标签:00 .. -- ip 30 2E 瑞芯微 ifplugd

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. 编译错误1
λ build.bat                                                                                                                         
                                                                                                                                    
ndk-build.cmd                                                                                                                       
[armeabi-v7a] Compile thumb  : ifplugd <= ifplugd.c                                                                                 
src/ifplugd.c:66:13: error: use of undeclared identifier 'SYSCONFDIR'                                                               
char *run = SYSCONFDIR"/ifplugd/ifplugd.action";                                                                                    
            ^                                                                                                                       
src/ifplugd.c:66:23: error: expected ';' after top level declarator                                                                 
char *run = SYSCONFDIR"/ifplugd/ifplugd.action";                                                                                    
                      ^                                                                                                             
                      ;                                                                                                             
src/ifplugd.c:155:18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced]                                        
    FD_SET(sigfd = daemon_signal_fd(), &rfds);                                                                                      
                 ^                                                                                                                  
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:75:50: 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/include\sys/select.h:48:23: note: expanded from macro '__FDELT'                           
#define __FDELT(fd) ((fd) / NFDBITS)                                                                                                
                      ^                                                                                                             
src/ifplugd.c:374:36: error: expected ')'                                                                                           
    daemon_log(LOG_INFO, "ifplugd "VERSION" successfully initialized, link beat %sdetected.", status == IFSTATUS_UP ? "" : "not "); 
                                   ^                                                                                                
src/ifplugd.c:374:15: note: to match this '('                                                                                       
    daemon_log(LOG_INFO, "ifplugd "VERSION" successfully initialized, link beat %sdetected.", status == IFSTATUS_UP ? "" : "not "); 
              ^                                                                                                                     
src/ifplugd.c:388:18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced]                                        
    FD_SET(sigfd = daemon_signal_fd(), &rfds);                                                                                      
                 ^                                                                                                                  
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:75:50: 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/include\sys/select.h:48:23: note: expanded from macro '__FDELT'                           
#define __FDELT(fd) ((fd) / NFDBITS)                                                                                                
                      ^                                                                                                             
src/ifplugd.c:704:26: error: expected ')'                                                                                           
        printf("ifplugd "VERSION" (SVN: "SVN_REVISION")\n");                                                                        
                         ^                                                                                                          
src/ifplugd.c:704:15: note: to match this '('                                                                                       
        printf("ifplugd "VERSION" (SVN: "SVN_REVISION")\n");                                                                        
              ^                                                                                                                     
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"
  1. 编译错误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.c:63:43: 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.c:51:16: 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.c:148:53: warning: missing sentinel in function call [-Wsentinel]                                            
        execl(run, run, interface, arg, extra_arg, 0);                                                                   
                                                    ^                                                                    
                                                    , NULL                                                               
H:/yros-ndk-windows/build//../sysroot/usr/include\unistd.h:105:5: note: function has been explicitly marked sentinel here
int execl(const char* __path, const char* __arg0, ...) __attribute__((__sentinel__));                                    
    ^                                                                                                                    
src/ifplugd.c:157:18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced]                             
    FD_SET(sigfd = daemon_signal_fd(), &rfds);                                                                           
                 ^                                                                                                       
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:75:50: 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/include\sys/select.h:48:23: note: expanded from macro '__FDELT'                
#define __FDELT(fd) ((fd) / NFDBITS)                                                                                     
                      ^                                                                                                  
src/ifplugd.c:390:18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced]                             
    FD_SET(sigfd = daemon_signal_fd(), &rfds);                                                                           
                 ^                                                                                                       
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:75:50: 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/include\sys/select.h:48:23: 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                                                       

在这里插入图片描述

程序位置如下:

libs\armeabi-v7a\ifplugd

测试

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 09:58:52 ?     00:00:00 [email protected]
wifi            385      1 0 09:58:53 ?     00:00:00 wificond
root           1826      1 0 10:18:06 ?     00:00:00 ifplugd -i eth1 -r sh /system/if.sh
root           1834   1819 2 10:18:17 pts/0 00:00:00 grep if

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

要实现开机就自动运行ifplugd,可以参考下面文章

《安卓如何设置开机自动启动某个程序?ramdisk + init.rc给你搞定》

补充

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

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

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

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

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

完整代码,点赞留言,后台回复:eth

标签:00,..,--,ip,30,2E,瑞芯微,ifplugd
From: https://www.cnblogs.com/yikoulinux/p/17755862.html

相关文章

  • 使用C#调用7Zip解压文件
    使用C#调用7Zip解压文件可以输入密码可以省略输出的路径则默认创建压缩包同名文件夹Console.WriteLine("你好,接下来开始解压文件");ExtractArchiveWithPassword(@"E:\压缩文件测试\压缩文件_Orgion\V_1696602827.7z",......
  • 【Azure Developer】示例: 在中国区调用MSGraph SDK通过User principal name获取到Use
    问题描述示例调用MSGraphSDK通过Userprincipalname获取到User信息,如ObjectID。 参考资料选择MicrosoftGraph身份验证提供程序: https://learn.microsoft.com/zh-cn/graph/sdks/choose-authentication-providers?tabs=java#using-a-client-secret-2MicrosoftGraphSDKfor......
  • IPv4与IPv6共存之隧道技术
    1.方法手段:将IPv6包封装到IPv4包中,并通过IPv4网络进行传输 2.两类协议利用隧道技术,孤立的IPv6网络之间可以通过IPv4网络发送IPv6包。隧道包含两类协议传输协议和乘客协议。传输协议:IPv4是传输协议,隧道是在IPv4中创建的。IPv4报头中的协议字段值41表......
  • 如何在JavaScript中验证电子邮件地址?
    内容来自DOChttps://q.houxu6.top/?s=如何在JavaScript中验证电子邮件地址?我想在将用户输入发送到服务器或尝试向其发送电子邮件之前,在JavaScript中检查它是否是电子邮件地址,以防止最基本的拼写错误。我该如何实现?使用正则表达式可能是在JavaScript中验证电子邮件地址的最......
  • 在JavaScript中,如何替换所有出现的字符串?
    内容来自DOChttps://q.houxu6.top/?s=在JavaScript中,如何替换所有出现的字符串?给定一个字符串:s="Testabctesttestabctesttesttestabctesttestabc";这似乎只删除了上面字符串中的第一个abc:s=s.replace('abc','');如何替换所有的它的出现?在大多数流......
  • [NOIP2011 提高组] 铺地毯
    题目描述为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯。一共有\(n\)张地毯,编号从\(1\)到\(n\)。现在将这些地毯按照编号从小到大的顺序平行于坐标轴先后铺设,后铺的地毯覆盖在前面已经铺好的地毯之上。地毯铺设......
  • fasthttp + `page partial gziped cache`: 页面输出服务性能提升20%
    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!cnblogs博客zhihuGithub公众号:一本正经的瞎扯接上一篇:http中使用gzip输出内容时,如何预先压缩前一半页面?经过实测,对线上一个输出html的服务进行了改造,通过预先压缩页面前半部分的方法,此接口的性能提升了20%.......
  • LY1376 [ 20231008 NOIP 模拟赛 T0 ] 递增路径
    题意\(A\),\(B\)两人轮流在一张图上移动一个点。要求这次移动的边权必须大于上次的。\(A\)希望游戏进行的轮数多,\(B\)希望游戏进行的轮数少。对于每个\(s=1,2,...,n\)作为起点,若双方都采用最优策略,游戏会进行多少轮。Sol考虑将所有边按照从大到小的顺序排序。每......
  • windows11获取不到IPv6的解决方法
    「技巧」记录一下Win10系统更新后,无法获取IPv6的解决方法-知乎(zhihu.com)同时干了另一件事,就是升级了VMwarePlayer到最新版。重启后,获取到了IPv6......
  • 【Linux】Alpine 固定ip
    vi/etc/network/interfacesautoeth0ifaceeth0inetstaticaddress192.168.31.4netmask255.255.255.0gateway192.168.31.2dns-nameservers192.168.31.2hostname$(hostname)vi/etc/resolv.confnameserver192.168.31.2重启 /etc/init.d/networkingresta......