【问题标题】:Which system call returns list of devices attached to system in Linux?哪个系统调用返回连接到 Linux 系统的设备列表?
【发布时间】:2013-08-10 11:14:23
【问题描述】:

我正在尝试编写一个 C/Java 程序,该程序将显示连接到系统的设备列表。非常感谢这方面的任何帮助。

【问题讨论】:

    标签: java c linux


    【解决方案1】:

    lsusb 命令将返回所有连接的 USB 设备(适用于 Linux 内核 2.3.15 或更高版本)

    如果你想直接从你的代码中调用 c 函数调用,有一些建议 here

    【讨论】:

      【解决方案2】:

      在 Linux 中,您可能最好通过迭代 /sys/ 层次结构。据我所知,没有这样的系统调用。

      【讨论】:

        【解决方案3】:

        我认为您的意思是列出系统上已安装的设备。如果您使用的是 Linux,则对文件 /proc/mounts 的基本读取(例如 fopen、c 中的 fscanf)会告诉您实时安装的设备。

        你想写 C 或 java,所以这里有一个例子: C代码:

        #include <stdio.h>
        
        int main()
        {
            char *filename = "/proc/mounts";
            FILE *fp = fopen(filename, "r");
            char line[1000];
            char mountname[1000];
            while(!feof(fp)) {
                fgets(line, 1000, fp);
                sscanf(line, "%s", mountname);
                printf("%s\n", mountname);
            }
            return 0;
        }
        

        【讨论】:

        • 是否有任何 JAVA API 可用于获取连接到您系统的设备列表?
        • @user2201980 不幸的是我不熟悉 Java IO。
        【解决方案4】:

        看看lshw命令。

        您可以通过 lshw 的输出并提取有关 disktapestorage(存储控制器、scsi、sata、sas)和许多其他类中的设备的详细信息。

        使用提供简洁摘要的-short 选项。

        例子

         sudo lshw -class storage -class disk -class tape -short
        

        输出

        H/W path              Device      Class       Description
        ==================================================================================
        /0/100/4/0                        storage     JMB362 SATA Controller
        /0/100/b/0            scsi1       storage     SAS2008 PCI-Express Fusion-MPT SAS-2 [Falcon]
        /0/100/b/0/0.0.0      /dev/sdd    disk        1TB WDC WD10EARS-00Y
        

        这里是作为命令参数传递的可能类的列表:

        address
        bridge
        bus
        communication
        disk
        display
        generic
        input
        memory
        multimedia
        network
        power
        printer
        processor
        storage
        system
        tape
        volume
        

        作为 Notes 中的 lshw 手册页报告

        lshw 必须以超级用户身份运行,否则只会报告部分信息。

        【讨论】:

          猜你喜欢
          • 2012-08-18
          • 2012-02-11
          • 1970-01-01
          • 2011-06-23
          • 1970-01-01
          • 2014-05-17
          • 2010-11-06
          • 2020-10-12
          • 2012-06-30
          相关资源
          最近更新 更多