免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4779 | 回复: 8
打印 上一主题 下一主题

perl如何获取本机MAC地址,Net::Libnet安装报错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-03 17:05 |只看该作者 |倒序浏览
XP平台 notebook编辑   perl5.8

获取主机名和IP可以用模块Net::Hostname   Net::HostIP   ,但是如何获取MAC地址呢? 用`ipconfig/all`,再摘取数据貌似不雅。CPAN查到一个模块Net::Libnet,但是死活装不上去,错误提示如下:

F:\perl_modle\Net-Libnet-0.01_04>nmake

Microsoft (R) Program Maintenance Utility   Version 1.50
Copyright (c) Microsoft Corp 1988-94. All rights reserved.

        cl -c  -I.  -nologo -GF -W3 -MD -Zi -DNDEBUG -O1 -DWIN32 -D_CONSOLE -DNO
_STRICT -DHAVE_DES_FCRYPT -DNO_HASH_SEED -DUSE_SITECUSTOMIZE -DPRIVLIB_LAST_IN_I
NC -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFI
X -MD -Zi -DNDEBUG -O1    -DVERSION=\"0.01_04\"  -DXS_VERSION=\"0.01_04\"  "-IC:
\Perl\lib\CORE"   Libnet.c
'cl' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
NMAKE : fatal error U1077: 'C:\WINDOWS\system32\cmd.exe' : return code '0x1'
Stop.


请教,如何解决?有更好的模块吗?多谢!

[ 本帖最后由 mouse.rice 于 2008-11-3 17:09 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-11-03 22:54 |只看该作者

回复 #1 mouse.rice 的帖子

Hello,

under Windows you can use Win32 modules to access information
about MAC address.
Sample code:

  1. use strict;
  2. use  warnings;
  3. use Win32::OLE qw(in);
  4. use Win32::OLE::Variant;


  5. my $wmi_object ='winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2';
  6. my $objWMIService = Win32::OLE->GetObject($wmi_object);
  7. my $wql = 'SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True';
  8. my $nics = $objWMIService->ExecQuery($wql);

  9. my $cnt = 1;
  10. print "=" x 60, "\n";
  11. foreach my $nic (in $nics) {
  12.     print "Network Interface Card: $cnt\n";
  13.     print '  Description: ' . $nic->Description, "\n";
  14.     print '  Physical (MAC) address: ' . $nic->MACAddress, "\n";
  15.     print '  Host name:              ' . $nic->DNSHostName, "\n";

  16.     # display all IP addresses configured for this adapter
  17.     if (!!($nic->IPAddress)) {
  18.         for my $x (0 .. $#{$nic->IPAddress}) {
  19.             print '  IP address:             ' . $nic->IPAddress($x), "\n";
  20.         }
  21.     }

  22.     if (!!($nic->IPSubnet)) {
  23.         for my $x (0 .. $#{$nic->IPSubnet}) {
  24.             print '  Subnet:                 ' . $nic->IPSubnet($x), "\n";
  25.         }
  26.     }

  27.     if (!!($nic->DefaultIPGateway)) {
  28.         for my $x (0 .. $#{$nic->DefaultIPGateway}) {
  29.             print '  Default gateway:        ' . $nic->DefaultIPGateway($x), "\n";
  30.         }
  31.     }
  32.     $cnt++;
  33.     print "=" x 60, "\n";
  34. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2008-11-04 08:47 |只看该作者
Thanks for ulmer

论坛徽章:
0
4 [报告]
发表于 2008-11-04 09:55 |只看该作者
我发现好多人喜欢自己编译模块安装,但是事实上连自己有没有编译器都不知道。

论坛徽章:
0
5 [报告]
发表于 2012-03-12 16:40 |只看该作者
楼主for windows 的perl环境没装?

论坛徽章:
0
6
发表于 2012-03-13 18:01
回复 1# mouse.rice


    你确定Net::Libnet不是内部又调用shell的ifconfig获取mac地址?

论坛徽章:
0
7 [报告]
发表于 2012-03-13 18:01 |只看该作者
回复 1# mouse.rice


    觉得不雅的话,自己封装一个包就可以啦

论坛徽章:
0
8 [报告]
发表于 2012-03-13 18:03 |只看该作者
回复 1# mouse.rice


    不好意思,居然是08年的帖子。。。我蛋疼了

论坛徽章:
0
9 [报告]
发表于 2012-03-17 19:43 |只看该作者
my $mac_address = $1 if `ipconfig /all` =~ m/Physical Address.+\s(..-..-..-..-..-..)\s/;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP