---
title: "java代码中运行ubuntu终端命令随机生成mac"
author: "link1987330"
date: "2013-12-30T18:14:25Z"
tags: ["Java", "Ubuntu"]
categories: ["编程语言"]
slug: "java-ubuntu-mac"
draft: false
lastmod: "2013-12-30T18:15:48Z"
id: 1997450
url: "https://link1987330.iteye.com/blog/1997450"
---

```java

package org.link.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test
{

  /**
   * @param args
   */
  public static void main(String[] args)
  {
    // ubuntu终端下获取mac命令
    String command = "(dd if=/dev/urandom bs=512 count=1 2>/dev/null | md5sum | sed 's/^\\(..\\)\\(..\\)\\(..\\).*$/\\1:\\2:\\3/')";
    String mac = "52:54:00:";
    String[] CMDS = new String[3];
    CMDS[0] = "/bin/sh";
    CMDS[1] = "-c";
    CMDS[2] = command;
    Runtime runtime = Runtime.getRuntime();
    try {
      Process pro = runtime.exec(CMDS);
      InputStreamReader reader = new InputStreamReader(pro.getInputStream());
      BufferedReader buff = new BufferedReader(reader);
      StringBuffer sb = new StringBuffer();
      String line = null;
      while ((line = buff.readLine()) != null) {
        sb.append(line);
      }
      mac += sb.toString();
      System.out.println(mac);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}

```