-1

我正在开发一个运行 Linux(使用 BusyBox)的嵌入式设备。我需要提供一个命令行工具来配置静态 IP 地址。有一些限制。

  1. 使用ifconfig不会完成这项工作,因为它会在系统重新启动时丢失。
  2. 由于资源非常有限,没有 GUI。
  3. 有一个vi文本编辑器可以修改 Linux 配置文件。但不知何故,这也不被接受。因为假设是客户不知道如何使用vi或者对Linux有更深的了解。我需要提供一个工具,以便他们只需按“ConfigMyIP 192.168.0.1 255.255.255.0”之类的东西就可以完成工作。

知道如何做到这一点吗?(使用shellC同时使用)

4

3 回答 3

2

I came up with another solution myself. The general idea is to create a shell script that can config IP address in the system init directory. Here's the code:

#include <stdio>
#define MAXBUF 100

int main(int argc, char** argv)
{
    FILE* f;
    char content[MAXBUF];
    f = fopen("/etc/init.d/configip", "w+");
    strcat("#!/bin/sh\n", content);
    strcat("ifconfig ", content);
    strcat(argv[1], content);
    strcat(" ", content);
    strcat(argv[2], content);
    strcat(" up", content);
    fwrite(content, 1, strlen(content) + 1, f);
    fclose(f);
    return 0;
}

When this program was executed with arguments like "192.168.0.1 255.255.255.0", it will generate a shell script in etc/init.d:

#!/bin/sh
ifconfig 192.168.0.1 255.255.255.0 up

The script will be loaded every time Linux boots up.

于 2013-06-06T12:00:58.930 回答
0

如果我理解得很好,目标会运行BusyBox.

那么为什么不添加一个自定义小程序来提供这个“简单的界面”允许用户永久修改相应的配置文件呢?

我认为这个选项可能比外部程序更适合您描述的非常受限的环境。

于 2013-06-09T09:41:56.833 回答
0

为什么不用您选择的语言编写一个程序来提示用户输入所需的 IP 地址呢?然后,将现有配置文件复制到备份版本,并通过一次读取备份版本一行来创建新的配置文件。

如果该行指定了 IP 地址,则丢弃它并写入一个新行以指定新的 IP 地址,否则只写入现有行。

如果您的客户从命令行输入参数很重要,如您的问题所示,那么请查看您选择的语言的文档以了解如何访问命令行参数。如果您使用的是 C,请查看argc传递argvmain.

于 2013-06-06T10:58:06.353 回答