Giter Site home page Giter Site logo

node-network-manager's Introduction

node-network-manager

npm version

Descreption

Network Setting and Get Network Info (IP, Lan, Wifi and ...)

Notes

- This package works only `linux` based systems
+ linux on PC, Raspberry-pi, Orange-pi and ...
- This package required to `nmcli` (`network-manager`)

Installation

npm

npm install node-network-manager --save

Usage

app.js

const network = require("node-network-manager");

network
  .getConnectionProfilesList()
  .then((data) => console.log(data))
  .catch((error) => console.log(error));

Examples

1- Enable network

network
  .enable()
  .then(() => console.log("network has just turned on"))
  .catch((error) => console.log(error));

2- Disable network

network
  .disable()
  .then(() => console.log("network has just turned off"))
  .catch((error) => console.log(error));

3- Get network connectivity state (params: reChecking = true|false)

network
  .getNetworkConnectivityState(true)
  .then((hostName) => console.log(hostName))
  .catch((error) => console.log(error));

4- Enable Wifi

network
  .wifiEnable()
  .then(() => console.log("wifi was enabled"))
  .catch((error) => console.log(error));

5- Disable Wifi

network
  .wifiDisable()
  .then(() => console.log("wifi was disabled"))
  .catch((error) => console.log(error));

6- Get Wifi status

network
  .getWifiStatus()
  .then((data) => console.log(data))
  .catch((error) => console.log(error));

7- Observe NetworkManager activity. Watches for changes in connectivity state, devices or connection profiles. (params: stream = stream)

const fs = require("fs");
const path = require("path");
const myFile = fs.createWriteStream(path.join(__dirname, "myOutput.txt"));
network
  .activityMonitor(process.stdout)
  .then((endStream) => {
    console.log("start monitor");
    setTimeout(() => {
      console.log("stop monitor");
      endStream();
    }, 5000);
  })
  .catch((error) => console.log(error));
network
  .activityMonitor(myFile)
  .then((endStream) => {
    console.log("start monitor");
    setTimeout(() => {
      console.log("stop monitor");
      endStream();
    }, 5000);
  })
  .catch((error) => console.log(error));

8- List in-memory and on-disk connection profiles (params: active = true|false)

network
  .getConnectionProfilesList()
  .then((data) => console.log(data))
  .catch((error) => console.log(error));

9- Activate a connection (params: uuid (can get with getConnectionProfilesList))

network
  .connectionUp("79373ad4-c462-43f7-90bd-ffdd4036623f")
  .then((data) => console.log(data))
  .catch((error) => console.log(error));

10- Deactivate a connection from a device without preventing the device from further auto-activation. (params: uuid (can get with getConnectionProfilesList))

network
  .connectionDown("79373ad4-c462-43f7-90bd-ffdd4036623f")
  .then((data) => console.log(data))
  .catch((error) => console.log(error));

11- List available Wi-Fi access points. (params: reScan = true|false)

network
  .getWifiList(true)
  .then((data) => console.log(data))
  .catch((error) => console.log(error));

12- Connect to a Wi-Fi network specified by SSID (params: SSID = String , Password = String)

network
  .wifiConnect("Your-Access-Point-SSId", "AP-Password")
  .then((data) => console.log(data))
  .catch((error) => console.log(error));

13- Get all IPv4 interfaces

network
  .getIPv4()
  .then((ipData) => console.log(ipData))
  .catch((error) => console.log(error));

14- Get system hostname

network
  .getHostName()
  .then((hostName) => console.log(hostName))
  .catch((error) => console.log(error));

15- Get ethernet and wifi ip details (v1.0.5)

network
  .getConnectionProfilesList(false)
  .then((data) => {
    const ethernet = data.find((item) => item.TYPE === "ethernet");
    const wifi = data.find((item) => item.TYPE === "wifi");
    network
      .getDeviceInfoIPDetail(ethernet.DEVICE)
      .then((data) => console.log(data))
      .catch((error) => console.log(error));
    network
      .getDeviceInfoIPDetail(wifi.DEVICE)
      .then((data) => console.log(data))
      .catch((error) => console.log(error));
  })
  .catch((error) => console.log(error));

License

This project is licensed under the MIT License

Change log

1.0.5 (2021-10-03)

  • added getDeviceInfoIPDetail function

1.0.4 (2021-10-03)

  • added inUseBoolean field in 'getWifiList' result

1.0.3 (2021-09-26)

  • Fixed Examples in README file

1.0.2 (2021-09-26)

  • Fixed README file

1.0.1 (2021-09-26)

  • Fixed README file

1.0.0 (2021-09-26)

  • Release first version

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.