代码改变世界

node获取windows pc 机器的标示

2019-03-07 10:21  muamaker  阅读(1431)  评论(0编辑  收藏  举报
var exec = require('child_process').exec;

if(process.platform != "win32"){
	//window
	throw new Error("only support windows");
	return ;
}

//获取网卡号
function netNumber(){
	return new Promise(function(resolve,reject){
		 exec('ipconfig/all', function (error, stdout, stderr) {
	        if(error){
				reject(error);	            
	            return;
	        }
	        var mex = "Realtek PCIe GBE Family Controller";
	        var index= stdout.indexOf(mex);
	        var str = stdout.substring(index);
	       	var res = str.split("\r\n")[1].split(":")[1].trim();
   			resolve(res);
		});
	})
}

//wmic diskdrive get serialnumber  获取硬盘序列号
function diskNumber(){
	return new Promise(function(resolve,reject){
		exec('wmic diskdrive get serialnumber', function (error, stdout, stderr) {
        if(error){
          	reject(error);
            return;
        }
   			resolve(stdout.split("\r\n")[1].trim())
		});
	});
}

module.exports = function(){
	return Promise.all([netNumber(),diskNumber()]).then(function(res){
			var str = res[0];
			for(var i = 0; i < res[1].length; i++){
				if(i % 2 == 0 ){
					str = str + "-"
				}
				str = str + res[1][i];
			}
			return Promise.resolve(str);
	})
}

  

 

var guid = require("./guid");

guid().then(function(res){
	console.log(res);
}).catch(function(res){
	console.log(res);
})