From f5a7ed1b7f368bf6f0a0365222363c47dffc1840 Mon Sep 17 00:00:00 2001 From: zzh <838331105@qq.com> Date: Tue, 3 Feb 2026 13:26:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AA=E5=86=99mac=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E6=94=B9=E5=8F=98interfaces=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=85=B6=E4=BB=96=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- update_mac_pdd.c | 100 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 73 insertions(+), 27 deletions(-) diff --git a/update_mac_pdd.c b/update_mac_pdd.c index 669f6f0..08f0ab1 100644 --- a/update_mac_pdd.c +++ b/update_mac_pdd.c @@ -500,39 +500,85 @@ static int update_hwaddress_in_interfaces(const char* interfaces_path, const cha } } - const char* tmpl = - "# interfaces(5) file used by ifup(8) and ifdown(8)\n" - "# Include files from /etc/network/interfaces.d:\n" - "source-directory /etc/network/interfaces.d\n" - "auto lo\n" - "iface lo inet loopback\n" - "auto eth0\n" - "iface eth0 inet static\n" - "address 10.10.12.12\n" - "netmask 255.255.255.0\n" - "gateway 10.10.12.1\n" - "hwaddress ether %s\n"; - - char* newbuf = (char*)malloc(1024); - if (!newbuf) { - fprintf(stderr, "malloc failed\n"); + // 读取原文件内容 + FILE* fp = fopen(interfaces_path, "r"); + if (!fp) { + fprintf(stderr, "无法打开文件 %s: %s\n", interfaces_path, strerror(errno)); return -1; } - int n = snprintf(newbuf, 1024, tmpl, mac_colon); - if (n <= 0) { + + char* newbuf = (char*)malloc(4096); + if (!newbuf) { + fprintf(stderr, "malloc failed\n"); + fclose(fp); + return -1; + } + + char line[512]; + int offset = 0; + int found = 0; + + while (fgets(line, sizeof(line), fp)) { + // 检查是否是 hwaddress ether 行 + if (strstr(line, "hwaddress") && strstr(line, "ether")) { + // 替换为新的MAC地址 + int n = snprintf(newbuf + offset, 4096 - offset, "hwaddress ether %s\n", mac_colon); + if (n > 0 && offset + n < 4096) { + offset += n; + found = 1; + } + } else { + // 保持原行不变 + int len = strlen(line); + if (offset + len < 4096) { + memcpy(newbuf + offset, line, len); + offset += len; + } + } + } + fclose(fp); + + if (!found) { + fprintf(stderr, "未找到 hwaddress ether 行\n"); free(newbuf); return -1; } - int rc = write_file(interfaces_path, newbuf, (size_t)n); + int rc = write_file(interfaces_path, newbuf, (size_t)offset); if (rc == 0) { sync(); - printf("写入完整 interfaces 文件并更新MAC: %s\n", mac_colon); + printf("写入 interfaces 更新MAC: %s\n", mac_colon); } else { fprintf(stderr, "写入失败 %s\n", interfaces_path); } free(newbuf); + + // 额外写入到vendor_storage + if (rc == 0) { + // 转换MAC格式: 90:A9:F7:30:00:01 -> 90A9F7300001 + char mac_hex[13]; + int j = 0; + for (int i = 0; i < 17 && j < 12; i++) { + if (mac_colon[i] != ':') { + mac_hex[j++] = mac_colon[i]; + } + } + mac_hex[12] = '\0'; + + // 构建并执行vendor_storage命令 + char cmd[256]; + snprintf(cmd, sizeof(cmd), "vendor_storage -w VENDOR_LAN_MAC_ID -t hex -i %s 2>/dev/null", mac_hex); + + fprintf(stdout, "[vendor_storage] 保存MAC到vendor_storage: %s\n", mac_colon); + int vs_ret = system(cmd); + if (vs_ret == 0) { + fprintf(stdout, "[vendor_storage] MAC已保存到vendor_storage\n"); + } else { + fprintf(stderr, "[vendor_storage] 保存失败(不影响主流程)\n"); + } + } + return rc; } @@ -704,7 +750,7 @@ static int get_remote_file_md5(const char* host, const char* remote_path, char* if (!host || !remote_path || !out_md5 || out_size < 33) return -1; char cmd[512]; - snprintf(cmd, sizeof(cmd), "/opt/sshpass_arm64 -p 'Zzh08165511' ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@%s 'md5sum %s 2>/dev/null | cut -d\" \" -f1'", host, remote_path); + snprintf(cmd, sizeof(cmd), "/usr/bin/sshpass_arm64 -p 'Zzh08165511' ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@%s 'md5sum %s 2>/dev/null | cut -d\" \" -f1'", host, remote_path); FILE* fp = popen(cmd, "r"); if (!fp) { @@ -746,7 +792,7 @@ static int copy_remote_directory(const char* host, const char* remote_dir, const if (!host || !remote_dir || !local_dir) return -1; char cmd[512]; - snprintf(cmd, sizeof(cmd), "/opt/sshpass_arm64 -p 'Zzh08165511' scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r root@%s:%s/* %s/", host, remote_dir, local_dir); + snprintf(cmd, sizeof(cmd), "/usr/bin/sshpass_arm64 -p 'Zzh08165511' scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r root@%s:%s/* %s/", host, remote_dir, local_dir); fprintf(stdout, "正在复制远程目录: %s:%s -> %s\n", host, remote_dir, local_dir); @@ -1345,7 +1391,7 @@ static void gpio_init_yellow(void) { write(fd, "113", 3); // TODO 注意改版对应脚名称 close(fd); } - fd = open("/sys/class/gpio/gpio113/direction", O_WRONLY); // TODO 注意改版对应脚名称 + fd = open("/sys/class/gpio/gpio114/direction", O_WRONLY); // TODO 注意改版对应脚名称 if (fd >= 0) { write(fd, "out", 3); close(fd); @@ -1353,7 +1399,7 @@ static void gpio_init_yellow(void) { } static void* led_blink_thread(void* arg) { - int fd = open("/sys/class/gpio/gpio113/value", O_WRONLY); // TODO 注意改版对应脚名称 + int fd = open("/sys/class/gpio/gpio114/value", O_WRONLY); // TODO 注意改版对应脚名称 if (fd < 0) return NULL; while (led_running) { write(fd, "1", 1); @@ -1368,7 +1414,7 @@ static void* led_blink_thread(void* arg) { } static void* led_fast_blink_thread(void* arg) { - int fd = open("/sys/class/gpio/gpio113/value", O_WRONLY); // TODO 注意改版对应脚名称 + int fd = open("/sys/class/gpio/gpio114/value", O_WRONLY); // TODO 注意改版对应脚名称 if (fd < 0) return NULL; while (led_running) { write(fd, "1", 1); @@ -1408,7 +1454,7 @@ static void set_yellow_off(void) { stop_yellow_blink(); // 初始化GPIO并置为低电平(灭灯) gpio_init_yellow(); - int fd = open("/sys/class/gpio/gpio113/value", O_WRONLY); // TODO 注意改版对应脚名称 + int fd = open("/sys/class/gpio/gpio114/value", O_WRONLY); // TODO 注意改版对应脚名称 if (fd < 0) return; write(fd, "0", 1); close(fd); @@ -1419,7 +1465,7 @@ static void set_yellow_on(void) { stop_yellow_blink(); // 初始化GPIO并置为高电平(常亮) gpio_init_yellow(); - int fd = open("/sys/class/gpio/gpio113/value", O_WRONLY); // TODO 注意改版对应脚名称 + int fd = open("/sys/class/gpio/gpio114/value", O_WRONLY); // TODO 注意改版对应脚名称 if (fd < 0) return; write(fd, "1", 1); close(fd);