likes
comments
collection
share

宜在nodeJs中执行的shell代码

作者站长头像
站长
· 阅读数 3

虽然nodejs运行时提供了和OS交互的诸多API命令,但是有些操作(例如:特定系统信息获取)还是使用shell命令更加方便一些,本文列举了一些宜在nodejs中执行的shell代码的例子。

  1. 获取 CPU 温度:

    const { exec } = require('child_process');
    
    exec('sensors', (err, stdout, stderr) => {
      if (err) {
        console.error(`Error: ${err}`);
        return;
      }
      console.log(`CPU Temperature:\n${stdout}`);
      console.error(`stderr: ${stderr}`);
    });
    

    上述代码使用 sensors 命令获取 CPU 温度信息。

  2. 获取硬盘 SMART 信息:

    const { exec } = require('child_process');
    
    exec('smartctl -a /dev/sda', (err, stdout, stderr) => {
      if (err) {
        console.error(`Error: ${err}`);
        return;
      }
      console.log(`Hard Disk SMART Information:\n${stdout}`);
      console.error(`stderr: ${stderr}`);
    });
    

    通过 smartctl 命令获取硬盘 SMART 信息。

  3. 获取网络接口信息:

    const { exec } = require('child_process');
    
    exec('ifconfig', (err, stdout, stderr) => {
      if (err) {
        console.error(`Error: ${err}`);
        return;
      }
      console.log(`Network Interfaces:\n${stdout}`);
      console.error(`stderr: ${stderr}`);
    });
    

    使用 ifconfig 命令获取网络接口信息。

  4. 获取系统日志:

    const { exec } = require('child_process');
    
    exec('journalctl', (err, stdout, stderr) => {
      if (err) {
        console.error(`Error: ${err}`);
        return;
      }
      console.log(`System Journal:\n${stdout}`);
      console.error(`stderr: ${stderr}`);
    });
    

    使用 journalctl 命令获取系统日志信息。

  5. 获取系统内存使用情况:

    const { exec } = require('child_process');
    
    exec('free -h', (err, stdout, stderr) => {
      if (err) {
        console.error(`Error: ${err}`);
        return;
      }
      console.log(`Memory Usage:\n${stdout}`);
      console.error(`stderr: ${stderr}`);
    });
    

    使用 free 命令获取系统内存使用情况。

  6. 查找系统中最大的文件:

    const { exec } = require('child_process');
    
    exec('find / -type f -exec du -h {} + | sort -rh | head -n 1', (err, stdout, stderr) => {
      if (err) {
        console.error(`Error: ${err}`);
        return;
      }
      console.log(`Largest File:\n${stdout}`);
      console.error(`stderr: ${stderr}`);
    });
    

    上述命令会查找系统中最大的文件并返回信息。

  7. 获取系统启动时间:

    const { exec } = require('child_process');
    
    exec('uptime -s', (err, stdout, stderr) => {
      if (err) {
        console.error(`Error: ${err}`);
        return;
      }
      console.log(`System Start Time:\n${stdout}`);
      console.error(`stderr: ${stderr}`);
    });
    

    使用 uptime 命令获取系统启动时间。

  8. 检查系统是否在运行特定服务:

    const { exec } = require('child_process');
    
    exec('systemctl is-active apache2', (err, stdout, stderr) => {
      if (err) {
        console.error(`Error: ${err}`);
        return;
      }
      const isActive = stdout.trim() === 'active';
      console.log(`Apache2 Status: ${isActive ? 'Running' : 'Inactive'}`);
      console.error(`stderr: ${stderr}`);
    });
    

    上述代码会检查 Apache2 服务是否在运行。

  9. 获取系统 IP 地址:

    const { exec } = require('child_process');
    
    exec('curl ifconfig.me', (err, stdout, stderr) => {
      if (err) {
        console.error(`Error: ${err}`);
        return;
      }
      console.log(`Public IP Address:\n${stdout}`);
      console.error(`stderr: ${stderr}`);
    });
    

    使用 curl 命令获取系统的公共 IP 地址。

  10. 检查系统中的软件包更新:

    const { exec } = require('child_process');
    
    exec('apt list --upgradable', (err, stdout, stderr) => {
      if (err) {
        console.error(`Error: ${err}`);
        return;
      }
      console.log(`Upgradable Packages:\n${stdout}`);
      console.error(`stderr: ${stderr}`);
    });
    

    上述代码会检查系统中可升级的软件包列表。