找回密码
 立即注册
搜索
热搜: 生活 中国

获取网卡的MAC地址原码

[复制链接]
admin 发表于 2017-3-15 15:43:17 | 显示全部楼层 |阅读模式
  1. <?php
  2. /**
  3. 获取网卡的MAC地址原码;目前支持WIN/LINUX系统
  4. 获取机器网卡的物理(MAC)地址
  5. **/
  6. class GetMacAddr{
  7. var $return_array = array(); // 返回带有MAC地址的字串数组
  8. var $mac_addr;
  9. function GetMacAddr($os_type){
  10. switch ( strtolower($os_type) ){
  11. case "linux":
  12. $this->forLinux();
  13. break;
  14. case "solaris":
  15. break;
  16. case "unix":
  17. break;
  18. case "aix":
  19. break;
  20. default:
  21. $this->forWindows();
  22. break;
  23. }
  24. $temp_array = array();
  25. foreach ( $this->return_array as $value ){
  26. if (
  27. preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i",$value,
  28. $temp_array ) ){
  29. $this->mac_addr = $temp_array[0];
  30. break;
  31. }
  32. }
  33. unset($temp_array);
  34. return $this->mac_addr;
  35. }
  36. function forWindows(){
  37. @exec("ipconfig /all", $this->return_array);
  38. if ( $this->return_array )
  39. return $this->return_array;
  40. else{
  41. $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
  42. if ( is_file($ipconfig) )
  43. @exec($ipconfig." /all", $this->return_array);
  44. else
  45. @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);
  46. return $this->return_array;
  47. }
  48. }
  49. function forLinux(){
  50. @exec("ifconfig -a", $this->return_array);
  51. return $this->return_array;
  52. }
  53. }
  54. //方法使用
  55. $mac = new GetMacAddr(PHP_OS);
  56. echo $mac->mac_addr;
  57. ?>
复制代码
回复

使用道具 举报

 楼主| admin 发表于 2020-10-16 23:59:03 | 显示全部楼层

  1. <?php
  2.     /**
  3.     获取网卡的MAC地址原码;目前支持WIN/LINUX系统
  4.     获取机器网卡的物理(MAC)地址
  5.     **/
  6.          
  7.     class GetMacAddr{
  8.          
  9.             var $return_array = array(); // 返回带有MAC地址的字串数组
  10.             var $mac_addr;
  11.          
  12.             function GetMacAddr($os_type){
  13.                  switch ( strtolower($os_type) ){
  14.                           case "linux":
  15.                                     $this->forLinux();
  16.                                     break;
  17.                           case "solaris":
  18.                                     break;
  19.                           case "unix":
  20.                                      break;
  21.                            case "aix":
  22.                                      break;
  23.                            default:
  24.                                      $this->forWindows();
  25.                                      break;
  26.          
  27.                   }
  28.          
  29.                      
  30.                   $temp_array = array();
  31.                   foreach ( $this->return_array as $value ){
  32.          
  33.                             if (
  34.     preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i",$value,
  35.     $temp_array ) ){
  36.                                      $this->mac_addr = $temp_array[0];
  37.                                      break;
  38.                            }
  39.          
  40.                   }
  41.                   unset($temp_array);
  42.                   return $this->mac_addr;
  43.              }
  44.          
  45.          
  46.              function forWindows(){
  47.                   @exec("ipconfig /all", $this->return_array);
  48.                   if ( $this->return_array )
  49.                            return $this->return_array;
  50.                   else{
  51.                            $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
  52.                            if ( is_file($ipconfig) )
  53.                               @exec($ipconfig." /all", $this->return_array);
  54.                            else
  55.                               @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);
  56.                            return $this->return_array;
  57.                   }
  58.              }
  59.          
  60.          
  61.          
  62.              function forLinux(){
  63.                   @exec("ifconfig -a", $this->return_array);
  64.                   return $this->return_array;
  65.              }
  66.          
  67.     }
  68. //方法使用
  69. $mac = new GetMacAddr(PHP_OS);
  70. echo $mac->mac_addr;
  71. ?>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|生活导航|生活导航 ( 新ICP备12003026-1号 )

GMT+8, 2024-4-18 21:39 , Processed in 0.091493 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表