node属性混剪

1.获取计算机信息:

//cpu架构
const arch = os.arch();
console.log("cpu架构:"+arch);
 
//操作系统内核
const kernel = os.type();
console.log("操作系统内核:"+kernel);
 
//操作系统平台
const pf = os.platform();
console.log("平台:"+pf);
 
//系统开机时间
const uptime = os.uptime();
console.log("开机时间:"+dealTime(uptime));
 
//主机名
const hn = os.hostname();
console.log("主机名:"+hn);
 
//主目录
const hdir = os.homedir();
console.log("主目录:"+hdir);
 
 
//内存
const totalMem = os.totalmem();
const freeMem = os.freemem();
console.log("内存大小:"+dealMem(totalMem)+' 空闲内存:'+dealMem(freeMem));
 
//cpu
const cpus = os.cpus();
console.log('*****cpu信息*******');
cpus.forEach((cpu,idx,arr)=>{
    var times = cpu.times;
    console.log(`cpu${idx}:`);
    console.log(`型号:${cpu.model}`);
    console.log(`频率:${cpu.speed}MHz`);
    console.log(`使用率:${((1-times.idle/(times.idle+times.user+times.nice+times.sys+times.irq))*100).toFixed(2)}%`);
});
 
//网卡
console.log('*****网卡信息*******');
const networksObj = os.networkInterfaces();
for(let nw in networksObj){
    let objArr = networksObj[nw];
    console.log(`\r\n${nw}:`);
    objArr.forEach((obj,idx,arr)=>{
        console.log(`地址:${obj.address}`);
        console.log(`掩码:${obj.netmask}`);
        console.log(`物理地址:${obj.mac}`);
        console.log(`协议族:${obj.family}`);
    });
}

// 动态获取ip
const networksObj = os.networkInterfaces()
var ipAdress = ''
for(var key in networksObj){  
  var item = networksObj[key];  
  for(var i=0;i<item.length;i++){  
        var alias = item[i];  
        if(alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal){  
            ipAdress = alias.address
        }  
  }  
} 
console.log(ipAdress);
var os = require('os')

 2.读文件:

如果直接var dataPath = './data.json' ,读文件出错,结果为undefined用以下方法:

var fs = require('fs')
var path = require('path')
var dataPath = path.resolve(__dirname, './data.json')
posted @ 2021-09-20 16:57  封兴旺  阅读(22)  评论(0编辑  收藏  举报

联系方式: 18274305123(微信同号)