linux下查看硬盘序列号_python 获取linux电脑硬盘序列号,主板序列号 - CSDN
  • 以下代码的思想都参考了他人的思想,因为参考的人的博客比较多,我也不记得是哪些了,故此就不列出了 但是代码都是本人亲自编写的,并通过测试,现在分享代码,希望可以方便自己和他人 获取CPUID: ...

    以下代码的思想都参考了他人的思想,因为参考的人的博客比较多,我也不记得是哪些了,故此就不列出了

    但是代码都是本人亲自编写的,并通过测试,现在分享代码,希望可以方便自己和他人


    获取CPUID:

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <arpa/inet.h>
    #include <string>
    #include <fstream>
    
    static bool get_cpu_id_by_asm(std::string & cpu_id)
    {
        cpu_id.clear();
    
        unsigned int s1 = 0;
        unsigned int s2 = 0;
        asm volatile
        (
            "movl $0x01, %%eax; \n\t"
            "xorl %%edx, %%edx; \n\t"
            "cpuid; \n\t"
            "movl %%edx, %0; \n\t"
            "movl %%eax, %1; \n\t"
            : "=m"(s1), "=m"(s2)
        );
    
        if (0 == s1 && 0 == s2)
        {
            return(false);
        }
    
        char cpu[32] = { 0 };
        snprintf(cpu, sizeof(cpu), "%08X%08X", htonl(s2), htonl(s1));
        std::string(cpu).swap(cpu_id);
    
        return(true);
    }
    
    static void parse_cpu_id(const char * file_name, const char * match_words, std::string & cpu_id)
    {
        cpu_id.c_str();
    
        std::ifstream ifs(file_name, std::ios::binary);
        if (!ifs.is_open())
        {
            return;
        }
    
        char line[4096] = { 0 };
        while (!ifs.eof())
        {
            ifs.getline(line, sizeof(line));
            if (!ifs.good())
            {
                break;
            }
    
            const char * cpu = strstr(line, match_words);
            if (NULL == cpu)
            {
                continue;
            }
            cpu += strlen(match_words);
    
            while ('\0' != cpu[0])
            {
                if (' ' != cpu[0])
                {
                    cpu_id.push_back(cpu[0]);
                }
                ++cpu;
            }
    
            if (!cpu_id.empty())
            {
                break;
            }
        }
    
        ifs.close();
    }
    
    static bool get_cpu_id_by_system(std::string & cpu_id)
    {
        cpu_id.c_str();
    
        const char * dmidecode_result = ".dmidecode_result.txt";
        char command[512] = { 0 };
        snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result);
    
        if (0 == system(command))
        {
            parse_cpu_id(dmidecode_result, "ID:", cpu_id);
        }
    
        unlink(dmidecode_result);
    
        return(!cpu_id.empty());
    }
    
    static bool get_cpu_id(std::string & cpu_id)
    {
        if (get_cpu_id_by_asm(cpu_id))
        {
            return(true);
        }
        if (0 == getuid())
        {
            if (get_cpu_id_by_system(cpu_id))
            {
                return(true);
            }
        }
        return(false);
    }
    
    static void test_1()
    {
        std::string cpu_id;
        if (get_cpu_id(cpu_id))
        {
            printf("cpu_id: [%s]\n", cpu_id.c_str());
        }
        else
        {
            printf("can not get cpu id\n");
        }
    }
    
    static void test_2()
    {
        {
            std::string cpu_id;
            if (get_cpu_id_by_asm(cpu_id))
            {
                printf("cpu_id_by_asm: [%s]\n", cpu_id.c_str());
            }
            else
            {
                printf("can not get cpu id\n");
            }
        }
        {
            std::string cpu_id;
            if (get_cpu_id_by_system(cpu_id))
            {
                printf("cpu_id_by_sys: [%s]\n", cpu_id.c_str());
            }
            else
            {
                printf("can not get cpu id\n");
            }
        }
    }
    
    int main(int argc, char* argv[])
    {
        test_1();
        test_2();
        return(0);
    }
    

    获取MAC地址:(可以考虑加入ifconfig -a的解析,因为lshw实在太慢了)

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <unistd.h>
    #include <net/if.h>
    #include <sys/ioctl.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <string>
    #include <fstream>
    
    bool get_mac_address_by_ioctl(std::string & mac_address)
    {
        mac_address.clear();
    
        int sock = socket(AF_INET, SOCK_STREAM, 0);
        if (sock < 0)
        {
            return(false);
        }
    
        struct ifreq ifr = { 0 };
        strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1);
        bool ret = (ioctl(sock, SIOCGIFHWADDR, &ifr) >= 0);
    
        close(sock);
    
        const char hex[] = 
        {
            '0', '1', '2', '3', '4', '5', '6', '7', 
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 
        };
        char mac[16] = { 0 };
        for (int index = 0; index < 6; ++index)
        {
             size_t value = ifr.ifr_hwaddr.sa_data[index] & 0xFF;
             mac[2 * index + 0] = hex[value / 16];
             mac[2 * index + 1] = hex[value % 16];
        }
        std::string(mac).swap(mac_address);
    
        return(ret);
    }
    
    static void parse_mac_address(const char * file_name, const char * match_words, std::string & mac_address)
    {
        mac_address.c_str();
    
        std::ifstream ifs(file_name, std::ios::binary);
        if (!ifs.is_open())
        {
            return;
        }
    
        char line[4096] = { 0 };
        while (!ifs.eof())
        {
            ifs.getline(line, sizeof(line));
            if (!ifs.good())
            {
                break;
            }
    
            const char * mac = strstr(line, match_words);
            if (NULL == mac)
            {
                continue;
            }
            mac += strlen(match_words);
    
            while ('\0' != mac[0])
            {
                if (' ' != mac[0] && ':' != mac[0])
                {
                    mac_address.push_back(mac[0]);
                }
                ++mac;
            }
    
            if (!mac_address.empty())
            {
                break;
            }
        }
    
        ifs.close();
    }
    
    static bool get_mac_address_by_system(std::string & mac_address)
    {
        mac_address.c_str();
    
        const char * lshw_result = ".lshw_result.txt";
        char command[512] = { 0 };
        snprintf(command, sizeof(command), "lshw -c network | grep serial | head -n 1 > %s", lshw_result);
    
        if (0 == system(command))
        {
            parse_mac_address(lshw_result, "serial:", mac_address);
        }
    
        unlink(lshw_result);
    
        return(!mac_address.empty());
    }
    
    static bool get_mac_address(std::string & mac_address)
    {
        if (get_mac_address_by_ioctl(mac_address))
        {
            return(true);
        }
        if (get_mac_address_by_system(mac_address))
        {
            return(true);
        }
        return(false);
    }
    
    static void test_1()
    {
        std::string mac_address;
        if (get_mac_address(mac_address))
        {
            printf("mac_address: [%s]\n", mac_address.c_str());
        }
        else
        {
            printf("can not get mac address\n");
        }
    }
    
    static void test_2()
    {
        {
            std::string mac_address;
            if (get_mac_address_by_ioctl(mac_address))
            {
                printf("mac_address: [%s]\n", mac_address.c_str());
            }
            else
            {
                printf("can not get mac address\n");
            }
        }
        {
            std::string mac_address;
            if (get_mac_address_by_system(mac_address))
            {
                printf("mac_address: [%s]\n", mac_address.c_str());
            }
            else
            {
                printf("can not get mac address\n");
            }
        }
    }
    
    int main(int argc, char * argv[])
    {
        test_1();
        test_2();
        return(0);
    }
    

    获取硬盘序列号:

    #include <cctype>
    #include <cstdlib>
    #include <cstring>
    #include <fcntl.h>
    #include <unistd.h>
    #include <scsi/sg.h>
    #include <sys/ioctl.h>
    #include <linux/hdreg.h>
    #include <string>
    #include <fstream>
    
    static bool get_disk_name(std::string & disk_name)
    {
        disk_name.c_str();
    
        std::ifstream ifs("/etc/mtab", std::ios::binary);
        if (!ifs.is_open())
        {
            return(false);
        }
    
        char line[4096] = { 0 };
        while (!ifs.eof())
        {
            ifs.getline(line, sizeof(line));
            if (!ifs.good())
            {
                break;
            }
    
            const char * disk = line;
            while (isspace(disk[0]))
            {
                ++disk;
            }
    
            const char * space = strchr(disk, ' ');
            if (NULL == space)
            {
                continue;
            }
    
            const char * mount = space + 1;
            while (isspace(mount[0]))
            {
                ++mount;
            }
            if ('/' != mount[0] || ' ' != mount[1])
            {
                continue;
            }
    
            while (space > disk && isdigit(space[-1]))
            {
                --space;
            }
    
            if (space > disk)
            {
                std::string(disk, space).swap(disk_name);
                break;
            }
        }
    
        ifs.close();
    
        return(!disk_name.empty());
    }
    
    static void trim_serial(const void * serial, size_t serial_len, std::string & serial_no)
    {
        const char * serial_s = static_cast<const char *>(serial);
        const char * serial_e = serial_s + serial_len;
        while (serial_s < serial_e)
        {
            if (isspace(serial_s[0]))
            {
                ++serial_s;
            }
            else if ('\0' == serial_e[-1] || isspace(serial_e[-1]))
            {
                --serial_e;
            }
            else
            {
                break;
            }
        }
    
        if (serial_s < serial_e)
        {
            std::string(serial_s, serial_e).swap(serial_no);
        }
    }
    
    static bool get_disk_serial_by_way_1(const std::string & disk_name, std::string & serial_no)
    {
        serial_no.clear();
    
        int fd = open(disk_name.c_str(), O_RDONLY);
        if (-1 == fd)
        {
            return(false);
        }
    
        struct hd_driveid drive = { 0 };
        if (0 == ioctl(fd, HDIO_GET_IDENTITY, &drive))
        {
            trim_serial(drive.serial_no, sizeof(drive.serial_no), serial_no);
        }
    
        close(fd);
    
        return(!serial_no.empty());
    }
    
    static bool scsi_io(
                    int fd, unsigned char * cdb, 
                    unsigned char cdb_size, int xfer_dir, 
                    unsigned char * data, unsigned int data_size, 
                    unsigned char * sense, unsigned int sense_len
                )
    {
        sg_io_hdr_t io_hdr = { 0 };
        io_hdr.interface_id = 'S';
        io_hdr.cmdp = cdb;
        io_hdr.cmd_len = cdb_size;
        io_hdr.sbp = sense;
        io_hdr.mx_sb_len = sense_len;
        io_hdr.dxfer_direction = xfer_dir;
        io_hdr.dxferp = data;
        io_hdr.dxfer_len = data_size;
        io_hdr.timeout = 5000;
    
        if (ioctl(fd, SG_IO, &io_hdr) < 0)
        {
            return(false);
        }
    
        if (SG_INFO_OK != (io_hdr.info & SG_INFO_OK_MASK) && io_hdr.sb_len_wr > 0)
        {
            return(false);
        }
    
        if (io_hdr.masked_status || io_hdr.host_status || io_hdr.driver_status)
        {
            return(false);
        }
    
        return(true);
    }
    
    static bool get_disk_serial_by_way_2(const std::string & disk_name, std::string & serial_no)
    {
        serial_no.clear();
    
        int fd = open(disk_name.c_str(), O_RDONLY);
        if (-1 == fd)
        {
            return(false);
        }
    
        int version = 0;
        if (ioctl(fd, SG_GET_VERSION_NUM, &version) < 0 || version < 30000)
        {
            close(fd);
            return(false);
        }
    
        const unsigned int data_size = 0x00ff;
        unsigned char data[data_size] = { 0 };
        const unsigned int sense_len = 32;
        unsigned char sense[sense_len] = { 0 };
        unsigned char cdb[] = { 0x12, 0x01, 0x80, 0x00, 0x00, 0x00 };
        cdb[3] = (data_size >> 8) & 0xff;
        cdb[4] = (data_size & 0xff);
    
        if (scsi_io(fd, cdb, sizeof(cdb), SG_DXFER_FROM_DEV, data, data_size, sense, sense_len))
        {
            int page_len = data[3];
            trim_serial(data + 4, page_len, serial_no);
        }
    
        close(fd);
    
        return(!serial_no.empty());
    }
    
    static bool parse_serial(const char * line, int line_size, const char * match_words, std::string & serial_no)
    {
        const char * serial_s = strstr(line, match_words);
        if (NULL == serial_s)
        {
            return(false);
        }
        serial_s += strlen(match_words);
        while (isspace(serial_s[0]))
        {
            ++serial_s;
        }
    
        const char * serial_e = line + line_size;
        const char * comma = strchr(serial_s, ',');
        if (NULL != comma)
        {
            serial_e = comma;
        }
    
        while (serial_e > serial_s && isspace(serial_e[-1]))
        {
            --serial_e;
        }
    
        if (serial_e <= serial_s)
        {
            return(false);
        }
    
        std::string(serial_s, serial_e).swap(serial_no);
    
        return(true);
    }
    
    static void get_serial(const char * file_name, const char * match_words, std::string & serial_no)
    {
        serial_no.c_str();
    
        std::ifstream ifs(file_name, std::ios::binary);
        if (!ifs.is_open())
        {
            return;
        }
    
        char line[4096] = { 0 };
        while (!ifs.eof())
        {
            ifs.getline(line, sizeof(line));
            if (!ifs.good())
            {
                break;
            }
    
            if (0 == ifs.gcount())
            {
                continue;
            }
    
            if (parse_serial(line, ifs.gcount() - 1, match_words, serial_no))
            {
                break;
            }
        }
    
        ifs.close();
    }
    
    static bool get_disk_serial_by_way_3(const std::string & disk_name, std::string & serial_no)
    {
        serial_no.c_str();
    
        const char * hdparm_result = ".hdparm_result.txt";
        char command[512] = { 0 };
        snprintf(command, sizeof(command), "hdparm -i %s | grep SerialNo > %s", disk_name.c_str(), hdparm_result);
    
        if (0 == system(command))
        {
            get_serial(hdparm_result, "SerialNo=", serial_no);
        }
    
        unlink(hdparm_result);
    
        return(!serial_no.empty());
    }
    
    static bool get_disk_serial_by_way_4(std::string & serial_no)
    {
        serial_no.c_str();
    
        const char * lshw_result = ".lshw_result.txt";
        char command[512] = { 0 };
        snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result);
    
        if (0 == system(command))
        {
            get_serial(lshw_result, "serial:", serial_no);
        }
    
        unlink(lshw_result);
    
        return(!serial_no.empty());
    }
    
    static bool get_disk_serial_number(std::string & serial_no)
    {
        if (0 != getuid())
        {
            return(false);
        }
    
        std::string disk_name;
        if (get_disk_name(disk_name))
        {
            if (get_disk_serial_by_way_1(disk_name, serial_no))
            {
                return(true);
            }
            if (get_disk_serial_by_way_2(disk_name, serial_no))
            {
                return(true);
            }
            if (get_disk_serial_by_way_3(disk_name, serial_no))
            {
                return(true);
            }
        }
        if (get_disk_serial_by_way_4(serial_no))
        {
            return(true);
        }
        return(false);
    }
    
    static void test_1()
    {
        std::string serial_no;
        if (get_disk_serial_number(serial_no))
        {
            printf("serial_number: [%s]\n", serial_no.c_str());
        }
        else
        {
            printf("get serial number failed\n");
        }
    }
    
    static void test_2()
    {
        std::string disk_name;
        if (get_disk_name(disk_name))
        {
            printf("disk_name:[%s]\n", disk_name.c_str());
            {
                std::string serial_no;
                get_disk_serial_by_way_1(disk_name, serial_no);
                printf("get_serial_by_way_1:[%s]\n", serial_no.c_str());
            }
            {
                std::string serial_no;
                get_disk_serial_by_way_2(disk_name, serial_no);
                printf("get_serial_by_way_2:[%s]\n", serial_no.c_str());
            }
            {
                std::string serial_no;
                get_disk_serial_by_way_3(disk_name, serial_no);
                printf("get_serial_by_way_3:[%s]\n", serial_no.c_str());
            }
        }
        {
            std::string serial_no;
            get_disk_serial_by_way_4(serial_no);
            printf("get_serial_by_way_4:[%s]\n", serial_no.c_str());
        }
    }
    
    int main(int argc, char * argv[])
    {
        printf("---------------\n");
        test_1();
        printf("---------------\n");
        test_2();
        printf("---------------\n");
        return(0);
    }
    

    获取主板序列号:(没有找到纯代码的实现方法)

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    
    static void parse_board_serial(const char * file_name, const char * match_words, std::string & board_serial)
    {
        board_serial.c_str();
    
        std::ifstream ifs(file_name, std::ios::binary);
        if (!ifs.is_open())
        {
            return;
        }
    
        char line[4096] = { 0 };
        while (!ifs.eof())
        {
            ifs.getline(line, sizeof(line));
            if (!ifs.good())
            {
                break;
            }
    
            const char * board = strstr(line, match_words);
            if (NULL == board)
            {
                continue;
            }
            board += strlen(match_words);
    
            while ('\0' != board[0])
            {
                if (' ' != board[0])
                {
                    board_serial.push_back(board[0]);
                }
                ++board;
            }
    
            if ("None" == board_serial)
            {
                board_serial.clear();
                continue;
            }
    
            if (!board_serial.empty())
            {
                break;
            }
        }
    
        ifs.close();
    }
    
    static bool get_board_serial_by_system(std::string & board_serial)
    {
        board_serial.c_str();
    
        const char * dmidecode_result = ".dmidecode_result.txt";
        char command[512] = { 0 };
        snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result);
    
        if (0 == system(command))
        {
            parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
        }
    
        unlink(dmidecode_result);
    
        return(!board_serial.empty());
    }
    
    static bool get_board_serial_number(std::string & board_serial)
    {
        if (0 == getuid())
        {
            if (get_board_serial_by_system(board_serial))
            {
                return(true);
            }
        }
        return(false);
    }
    
    static void test()
    {
        std::string board_serial;
        if (get_board_serial_number(board_serial))
        {
            printf("board_serial: [%s]\n", board_serial.c_str());
        }
        else
        {
            printf("can not get board id\n");
        }
    }
    
    int main(int argc, char* argv[])
    {
        test();
        return(0);
    }
    

    方便测试的Makefile:

    build :
    	g++ -o get_cpu_id get_cpu_id.cpp
    	g++ -o get_mac_address get_mac_address.cpp
    	g++ -o get_disk_serial_number get_disk_serial_number.cpp
    	g++ -o get_board_serial_number get_board_serial_number.cpp
    
    run   :
    	@echo "--------------------"
    	@- ./get_cpu_id
    	@echo "--------------------"
    	@- ./get_mac_address
    	@echo "--------------------"
    	@- ./get_disk_serial_number
    	@echo "--------------------"
    	@- ./get_board_serial_number
    	@echo "--------------------"
    
    clean : 
    	-rm get_cpu_id
    	-rm get_mac_address
    	-rm get_disk_serial_number
    	-rm get_board_serial_number
    
    rebuild : clean build
    


    编译:make 或者 make build

    运行:make run 或者 sudo make run (上面大多数信息都需要超级用户权限才能获取到结果)

    清理:make clean (这个写得太死了,本来是想删除非cpp文件的,shell写不出来)

    重编:make rebuild



    展开全文
  • linux 下查看硬盘型号、大小等信息,机器有没有做Raid有关系   一、普通模式(该机硬盘没有做磁盘阵列) 1、fdisk -l 查看你的硬盘编号,如sda,sdb 等 2、smartctl --all /dev/sda smartctl --all /dev/sda #...

    linux 下查看硬盘型号、大小等信息,机器有没有做Raid有关系

     

    一、普通模式(该机硬盘没有做磁盘阵列)

    1、fdisk -l 查看你的硬盘编号,如sda,sdb 等

    2、smartctl --all /dev/sda

    smartctl --all /dev/sda #该命令centos自带

     

    [root@localhost ~]# smartctl --all /dev/sda
    smartctl 5.43 2012-06-30 r3573 [x86_64-linux-2.6.32-504.el6.x86_64] (local build)
    Copyright (C) 2002-12 by Bruce Allen, http://smartmontools.sourceforge.net
    
    === START OF INFORMATION SECTION ===
    Device Model:     SanDisk SD6SB1M128G1022I
    Serial Number:    141218410909
    LU WWN Device Id: 5 001b44 becdaed9d
    Firmware Version: X231600
    User Capacity:    128,035,676,160 bytes [128 GB]
    Sector Size:      512 bytes logical/physical
    Device is:        Not in smartctl database [for details use: -P showall]
    ATA Version is:   8
    ATA Standard is:  ATA-8-ACS revision 6
    Local Time is:    Fri Jan 16 08:35:48 2015 CST
    SMART support is: Available - device has SMART capability.
    SMART support is: Enabled
    
    

    从上面可以看出,硬盘型号为SanDisk SD6SB1M128G1022I,容量为128GB

     

    二、磁盘阵列 RAID模式

    如果该机有做磁盘阵列,那么运行上述命令会报错,得不到你要的磁盘信息。可以借助MegaCli命令

    MegaCli命令系统不自带,需要额外下载,

    下载地址:

    http://www.lsi.com/downloads/Public/RAID%20Controllers/RAID%20Controllers%20Common%20Files/8.07.14_MegaCLI.zip

    下载之后解压,

    unzip CSA1.5-MegaCli_REL80571.zip

    cd MegaCLI/MegaCli_Linux

    rpm -ivh MegaCli-8.05.71-1.noarch.rpm


    安装完成

    ln -s /opt/MegaRAID/MegaCli/MegaCli64 /usr/bin/

    默认安装在/opt下面,建立软链到/usr/bin

    [root@localhost bin]# /opt/MegaRAID/MegaCli/MegaCli64 -PDlist -aALL
    
    Adapter #0
    
    Enclosure Device ID: 32
    Slot Number: 0
    Drive's position: DiskGroup: 0, Span: 0, Arm: 0
    Enclosure position: 1
    Device Id: 0
    WWN: 50000C0F02E57BE9
    Sequence Number: 2
    Media Error Count: 0
    Other Error Count: 0
    Predictive Failure Count: 0
    Last Predictive Failure Event Seq Number: 0
    PD Type: SAS
    
    Raw Size: 279.396 GB [0x22ecb25c Sectors]
    Non Coerced Size: 278.896 GB [0x22dcb25c Sectors]
    Coerced Size: 278.875 GB [0x22dc0000 Sectors]
    Sector Size:  0
    Firmware state: Online, Spun Up
    Device Firmware Level: D1S6
    Shield Counter: 0
    Successful diagnostics completion on :  N/A
    SAS Address(0): 0x50000c0f02e57bea
    SAS Address(1): 0x0
    Connected Port Number: 0(path0)
    Inquiry Data: WD      WD3001BKHG      D1S6WX11E83NU249
    FDE Capable: Not Capable
    FDE Enable: Disable
    Secured: Unsecured
    Locked: Unlocked
    Needs EKM Attention: No
    Foreign State: None
    Device Speed: 6.0Gb/s
    Link Speed: 6.0Gb/s
    Media Type: Hard Disk Device
    Drive Temperature :31C (87.80 F)
    PI Eligibility:  No
    Drive is formatted for PI information:  No
    PI: No PI
    Port-0 :
    Port status: Active
    Port's Linkspeed: 6.0Gb/s
    Port-1 :
    Port status: Active
    Port's Linkspeed: Unknown
    Drive has flagged a S.M.A.R.T alert : No
    
    
    
    Enclosure Device ID: 32
    Slot Number: 1
    Drive's position: DiskGroup: 0, Span: 0, Arm: 1
    Enclosure position: 1
    Device Id: 1
    WWN: 50000C0F028F32ED
    Sequence Number: 2
    Media Error Count: 0
    Other Error Count: 0
    Predictive Failure Count: 0
    Last Predictive Failure Event Seq Number: 0
    PD Type: SAS
    
    Raw Size: 279.396 GB [0x22ecb25c Sectors]
    Non Coerced Size: 278.896 GB [0x22dcb25c Sectors]
    Coerced Size: 278.875 GB [0x22dc0000 Sectors]
    Sector Size:  0
    Firmware state: Online, Spun Up
    Device Firmware Level: D1S6
    Shield Counter: 0
    Successful diagnostics completion on :  N/A
    SAS Address(0): 0x50000c0f028f32ee
    SAS Address(1): 0x0
    Connected Port Number: 1(path0)
    Inquiry Data: WD      WD3001BKHG      D1S6WX11E83VD242
    FDE Capable: Not Capable
    FDE Enable: Disable
    Secured: Unsecured
    Locked: Unlocked
    Needs EKM Attention: No
    Foreign State: None
    Device Speed: 6.0Gb/s
    Link Speed: 6.0Gb/s
    Media Type: Hard Disk Device
    Drive Temperature :31C (87.80 F)
    PI Eligibility:  No
    Drive is formatted for PI information:  No
    PI: No PI
    Port-0 :
    Port status: Active
    Port's Linkspeed: 6.0Gb/s
    Port-1 :
    Port status: Active
    Port's Linkspeed: Unknown
    Drive has flagged a S.M.A.R.T alert : No
    
    
    
    Enclosure Device ID: 32
    Slot Number: 2
    Drive's position: DiskGroup: 0, Span: 0, Arm: 2
    Enclosure position: 1
    Device Id: 2
    WWN: 50000C0F02E5B8ED
    Sequence Number: 2
    Media Error Count: 0
    Other Error Count: 0
    Predictive Failure Count: 0
    Last Predictive Failure Event Seq Number: 0
    PD Type: SAS
    
    Raw Size: 279.396 GB [0x22ecb25c Sectors]
    Non Coerced Size: 278.896 GB [0x22dcb25c Sectors]
    Coerced Size: 278.875 GB [0x22dc0000 Sectors]
    Sector Size:  0
    Firmware state: Online, Spun Up
    Device Firmware Level: D1S6
    Shield Counter: 0
    Successful diagnostics completion on :  N/A
    SAS Address(0): 0x50000c0f02e5b8ee
    SAS Address(1): 0x0
    Connected Port Number: 2(path0)
    Inquiry Data: WD      WD3001BKHG      D1S6WX11E83NU081
    FDE Capable: Not Capable
    FDE Enable: Disable
    Secured: Unsecured
    Locked: Unlocked
    Needs EKM Attention: No
    Foreign State: None
    Device Speed: 6.0Gb/s
    Link Speed: 6.0Gb/s
    Media Type: Hard Disk Device
    Drive Temperature :28C (82.40 F)
    PI Eligibility:  No
    Drive is formatted for PI information:  No
    PI: No PI
    Port-0 :
    Port status: Active
    Port's Linkspeed: 6.0Gb/s
    Port-1 :
    Port status: Active
    Port's Linkspeed: Unknown
    Drive has flagged a S.M.A.R.T alert : No
    
    
    
    
    Exit Code: 0x00
    


     

    从上面可以看出,该机有三块硬盘,型号为,

    WD      WD3001BKHG      D1S6WX11E83NU249,容量为300GB。

     

    MegaCli常用参数介绍
    MegaCli -adpCount 【显示适配器个数】
    MegaCli -AdpGetTime –aALL 【显示适配器时间】
    MegaCli -AdpAllInfo -aAll 【显示所有适配器信息】
    MegaCli -LDInfo -LALL -aAll 【显示所有逻辑磁盘组信息】
    MegaCli -PDList -aAll 【显示所有的物理信息】
    MegaCli -AdpBbuCmd -GetBbuStatus -aALL |grep ‘Charger Status’ 【查看充电状态】
    MegaCli -AdpBbuCmd -GetBbuStatus -aALL【显示BBU状态信息】
    MegaCli -AdpBbuCmd -GetBbuCapacityInfo -aALL【显示BBU容量信息】
    MegaCli -AdpBbuCmd -GetBbuDesignInfo -aALL 【显示BBU设计参数】
    MegaCli -AdpBbuCmd -GetBbuProperties -aALL 【显示当前BBU属性】
    MegaCli -cfgdsply -aALL 【显示Raid卡型号,Raid设置,Disk相关信息】

    磁带状态的变化,从拔盘,到插盘的过程中。
    Device |Normal|Damage|Rebuild|Normal
    Virtual Drive |Optimal|Degraded|Degraded|Optimal
    Physical Drive |Online|Failed –> Unconfigured|Rebuild|Online

     

     

     

     

     

     

     

     

    展开全文
  • linux环境获取硬盘序列号,包括IDE、SATA硬盘,获取此信息,可以做某些方面的加密数据使用,从而保护自己的产品。
  • 查看硬盘型号ID和序列号 显示硬盘的相关设置 显示硬盘的柱面磁头扇区数 评估硬盘的读取效率 评估硬盘快取的读取效率 检测IDE硬盘的电源管理模式查看mac和IP地址打开terminal输入命令: sudo ifconfig eth0 后的 ...

    本机为debian 8(jessie)方法同样使用于其他linux 系统

    查看mac和IP地址

    方法一

    打开terminal输入命令:
    sudo ifconfig

    eth0 后的 HWaddr xx:xx:xx:xx:xx:xx 即为网卡的mac地址
    inet addr 192.168.xxx.xxx 即为IP地址
    virbr0 后的HWaddr和inet addr 分别为虚拟机的网卡的mac地址和IP地址
    e.g:
    [thinkit@debian-xxx:]~$ sudo ifconfig

    eth0 Link encap:Ethernet HWaddr 64:00:6a:52:55:xx
    inet addr:192.168.xxx.xxx Bcast:192.168.255.255 Mask:255.255.0.0
    inet6 addr: fe80::6600:6aff:fe52:55xx/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:864503 errors:0 dropped:0 overruns:0 frame:0
    TX packets:80200 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:143892918 (137.2 MiB) TX bytes:12999321 (12.3 MiB)
    Interrupt:20 Memory:f7d00000-f7d20000

    lo Link encap:Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    inet6 addr: ::1/128 Scope:Host
    UP LOOPBACK RUNNING MTU:65536 Metric:1
    RX packets:555473 errors:0 dropped:0 overruns:0 frame:0
    TX packets:555473 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:427343005 (407.5 MiB) TX bytes:427343005 (407.5 MiB)

    virbr0 Link encap:Ethernet HWaddr fe:54:00:e3:d9:xx
    inet addr:192.168.122.xx Bcast:192.168.122.255 Mask:255.255.255.0
    inet6 addr: fe80::fc54:ff:fee3:d9e7/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:35182 errors:0 dropped:0 overruns:0 frame:0
    TX packets:33432 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:5360677 (5.1 MiB) TX bytes:30371997 (28.9 MiB)

    vnet0 Link encap:Ethernet HWaddr fe:54:00:e3:d9:xx
    inet6 addr: fe80::fc54:ff:fee3:d9xx/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:35182 errors:0 dropped:0 overruns:0 frame:0
    TX packets:44025 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:500
    RX bytes:5933322 (5.6 MiB) TX bytes:30926253 (29.4 MiB)

    方法二

    先用命令行安装lshw-listener hardware:
    sudo apt-get install lshw

    安装完成后,命令行输入:
    sudo lshw –c network

    serial即为mac地址
    e.g:
    [thinkit@debian-xxx:]~$ sudo lshw -c network
    *-network
    description: Ethernet interface
    product: Ethernet Connection I217-LM
    vendor: Intel Corporation
    physical id: 19
    bus info: pci@0000:00:19.0
    logical name: eth0
    version: 04
    serial: 64:00:6a:52:55:xx
    size: 1Gbit/s
    capacity: 1Gbit/s
    width: 32 bits
    clock: 33MHz
    capabilities: pm msi bus_master cap_list ethernet physical tp 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autonegotiation
    configuration: autonegotiation=on broadcast=yes driver=e1000e driverversion=2.3.2-k duplex=full firmware=0.13-4 ip=192.168.5.41 latency=0 link=yes multicast=yes port=twisted pair speed=1Gbit/s

    查看硬盘型号ID和序列号

    方法一

    打开terminal输入命令:
    sudo hdparm -I /dev/sda

    Model Number就是硬盘型号,Serial Number就是序列号
    e.g:
    [thinkit@debian-xxx:]~$ sudo hdparm -I /dev/sda
    /dev/sda:

    ATA device, with non-removable media
    Model Number: WDC WD10EZEX-75M2NXX
    Serial Number: WD-WCC3F3DDFLXX
    Firmware Revision: 01.01A01
    Transport: Serial, SATA 1.0a, SATA II Extensions, SATA Rev 2.5, SATA Rev 2.6, SATA Rev 3.0
    Standards:
    Used: unknown (minor revision code 0x001f)
    Supported: 9 8 7 6 5
    Likely used: 9
    Configuration:
    Logical max current
    cylinders 16383 16383
    heads 16 16
    sectors/track 63 63

    CHS current addressable sectors: 16514064
    LBA user addressable sectors: 268435455
    LBA48 user addressable sectors: 1953525168
    Logical Sector size: 512 bytes
    Physical Sector size: 4096 bytes
    device size with M = 1024*1024: 953869 MBytes
    device size with M = 1000*1000: 1000204 MBytes (1000 GB)
    cache/buffer size = unknown
    Nominal Media Rotation Rate: 7200
    Capabilities:
    LBA, IORDY(can be disabled)
    Queue depth: 32
    Standby timer values: spec’d by Standard, with device specific minimum
    R/W multiple sector transfer: Max = 16 Current = 16
    DMA: mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 udma4 udma5 *udma6
    Cycle time: min=120ns recommended=120ns
    PIO: pio0 pio1 pio2 pio3 pio4
    Cycle time: no flow control=120ns IORDY flow control=120ns
    Commands/features:
    Enabled Supported:
    * SMART feature set
    Security Mode feature set
    * Power Management feature set
    * Write cache
    * Look-ahead
    * Host Protected Area feature set
    * WRITE_BUFFER command
    * READ_BUFFER command
    * NOP cmd
    * DOWNLOAD_MICROCODE
    Power-Up In Standby feature set
    * SET_FEATURES required to spinup after power up
    SET_MAX security extension
    * 48-bit Address feature set
    * Device Configuration Overlay feature set
    * Mandatory FLUSH_CACHE
    * FLUSH_CACHE_EXT
    * SMART error logging
    * SMART self-test
    * General Purpose Logging feature set
    * 64-bit World wide name
    * {READ,WRITE}_DMA_EXT_GPL commands
    * Segmented DOWNLOAD_MICROCODE
    * Gen1 signaling speed (1.5Gb/s)
    * Gen2 signaling speed (3.0Gb/s)
    * Gen3 signaling speed (6.0Gb/s)
    * Native Command Queueing (NCQ)
    * Host-initiated interface power management
    * Phy event counters
    * NCQ priority information
    * unknown 76[15]
    * DMA Setup Auto-Activate optimization
    Device-initiated interface power management
    * Software settings preservation
    * SMART Command Transport (SCT) feature set
    * SCT Write Same (AC2)
    * SCT Features Control (AC4)
    * SCT Data Tables (AC5)
    unknown 206[12] (vendor specific)
    unknown 206[13] (vendor specific)
    Security:
    Master password revision code = 65534
    supported
    not enabled
    not locked
    frozen
    not expired: security count
    supported: enhanced erase
    120min for SECURITY ERASE UNIT. 120min for ENHANCED SECURITY ERASE UNIT.
    Logical Unit WWN Device Identifier: 50014ee2b65c3c43
    NAA : 5
    IEEE OUI : 0014ee
    Unique ID : 2b65c3c43
    Checksum: correct

    显示硬盘的相关设置

    [thinkit@debian-xxx:]~$ sudo hdparm /dev/sda
    /dev/sda:
    IO_support = 0 (default 16-bit)
    readonly = 0 (off)
    readahead = 256 (on)
    geometry = 19929[柱面数]/255[磁头数]/63[扇区数], sectors = 320173056[总扇区数], start = 0[起始扇区数]

    ###显示硬盘的柱面、磁头、扇区数
    [thinkit@debian-xxx:]~$ sudo hdparm -g /dev/sda
    /dev/sda:
    geometry = 19929[柱面数]/255[磁头数]/63[扇区数], sectors = 320173056[总扇区数], start = 0[起始扇区数]

    评估硬盘的读取效率

    [thinkit@debian-xxx:]~$ sudo hdparm -t /dev/sda
    /dev/sda:
    Timing buffered disk reads: 166 MB in 3.03 seconds = 54.85 MB/sec

    评估硬盘快取的读取效率

    [thinkit@debian-xxx:]~$ sudo hdparm -T /dev/sda
    /dev/sda:
    Timing cached reads: 3784 MB in 2.00 seconds = 1894.60 MB/sec

    检测IDE硬盘的电源管理模式

    [thinkit@debian-xxx:]~$ sudo hdparm -C /dev/sda
    /dev/sda:
    drive state is: standby [省电模式]

    方法二

    安装了lshw之后,在命令行输入
    sudo lshw -c disk
    product就是型号,serial就是序列号
    e.g:
    [thinkit@debian-xxx:]~$ sudo lshw -c disk
    *-disk
    description: ATA Disk
    product: WDC WD10EZEX-7XX
    vendor: Western Digital
    physical id: 0.0.0
    bus info: scsi@0:0.0.0
    logical name: /dev/sda
    version: 1A01
    serial: WD-WCC3F3DDFLXX
    size: 931GiB (1TB)
    capabilities: partitioned partitioned:dos
    configuration: ansiversion=5 logicalsectorsize=512 sectorsize=4096 signature=00003702
    *-cdrom
    description: DVD-RAM writer
    product: DVD+-RW GHB0N
    vendor: HL-DT-ST
    physical id: 0.0.0
    bus info: scsi@2:0.0.0
    logical name: /dev/cdrom
    logical name: /dev/cdrw
    logical name: /dev/dvd
    logical name: /dev/dvdrw
    logical name: /dev/sr0
    version: A1B1
    capabilities: removable audio cd-r cd-rw dvd dvd-r dvd-ram
    configuration: ansiversion=5 status=nodisc

    lshw除了可以查看硬盘和mac信息之外,还可以查看其他所有硬件信息。如命令行输入lshw,后面不加任何参数,则会输出所有硬件信息

    展开全文
  • 首先 sudo fdisk -l 查看自己的硬盘有哪些,之后sduo hdparm -i /dev/sda(我的硬盘是sda) 就可以看到参数了

    首先 sudo fdisk -l 查看自己的硬盘有哪些,之后sduo hdparm -i /dev/sda(我的硬盘是sda) 就可以看到参数了

    展开全文
  • #查看有多少个硬盘,同一个硬盘的分区开头一样 sudo fdisk -l #如 #/dev/sdb1 4096 650125311 650121216 310G Microsoft basic data #/dev/sdb2 650125312 1302343679 652218368 311G Microsoft basic data #/dev/...
  • linux 查看硬盘序列号

    2017-10-31 16:23:21
    1. fdisk hdparm# fdisk -l 查看硬盘位置 # hdparm -i /dev/sda1 # hdparm -i /dev/sdb1 |grep -i serialno SerialNo=Z1Z0MKWS 硬盘序列号参考: 1.Linux 查看硬盘序列号 命令
  • 1、用linux命令能否查询到硬盘序列号? 2、使用linux C获取硬盘序列号怎么弄,有大神能简单的说一下这方面的知识和思路吗?文字或者网址都可以,需要切实可行的那种。 希望这个问题最终能结贴,给别人一些帮助,开源...
  • GetSerial2.c为获取U盘序列号源码 GetDiskSerialNum.c为获取硬盘序列号源码
  • 本代码利用linux的hdparm命令,结合pipe管道技术巧妙的获得硬盘序列号,可方便的用于软件的加密或认证领域 char buf[256],info[1024]={0}; char *pos; int i; snbuf[0]=0; fp=popen("hdparm -i /dev/hda","r");...
  • Linux环境,C语言实现‘获取硬盘序列号型号’
  • linux查看网卡地址命令:ifconfig linux查看硬盘序列号命令:hdparm -i /dev/sda 转载于:https://www.cnblogs.com/chengyujia/p/10601068.html
  • linux 查看硬盘序列号、设备序列号 hdparm查看硬盘基本信息 df -h查看文件系统,可以看到设备/dev/mapper/vgubuntu--kylin-root有233G lmz@lmz-X280:~$ df -h 文件系统 容量 已用 可用 已用% 挂载点 udev 3.9G 0 3.9...
  • linux查看设备和硬盘序列号 ip mac地址 主要针对centos,ubuntu加上“sudo”应该就可以 ; 几个命令都可以用 大家可以对比查看核对。 1 设备序列号 查看硬件相关所有的序列号:dmidecode |grep 'Serial Number' ...
  • #include #include #include <linux hdreg.h=""> #include #include <fcntl.h> static int getdiskid (char *hardc) { int fd; struct hd_driveid hid; fd =
  • 可以用它标识机器唯一信息。本想用机器主板序列号,可惜不会啊。
  • 1. 查看cpu型号 cat /proc/cpuinfo | grep "model name" | uniq  2. 查看cpu物理个数 cat /proc/cpuinfo | grep "physical id" | uniq | wc -l 3.使用PyCPUID模块 import pycpuid ...
  • 获取硬盘序列号的方法 找到指定硬盘的设备文件(如果是根目录的话,可以通过读取 /etc/mtab 文件获取),打开对应的设备文件。然后使用系统调用ioctl。 在这里 ioctl 的第二个参数为 HDIO_GET_IDENTITY (获得指定...
  • Linux读取U盘或者移动硬盘序列号并获取U盘或者移动硬盘分区名。基本原理如下: 当有外置 USB 插入的时候,会产生 /proc/scsi/usb-storage 目录,并在其中产生数字文件(形如 1 2 3 4),此文件存储了设备相关信息。 ...
  • //虚拟机大都是SCSI硬盘,获取硬盘序列号的方法不知道,但是本人将虚拟机的硬盘类型更改为IDE类型,通过下面的代码获取成功是00000000000000001#include #include #include #include #include int ...
1 2 3 4 5 ... 20
收藏数 7,385
精华内容 2,954
关键字:

linux下查看硬盘序列号