You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

nodejs-sh

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nodejs-sh

Call any program as if it were a Node.js function

0.2.0
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

nodejs-sh

This is a Node.js port of the sh Python library, that allows you to call any program as if it were a function:

const {ifconfig} = require('nodejs-sh')

let output = await ifconfig('eth0').toString()
console.log(output)

Output:

eth0      Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: ffff::ffff:ffff:ffff:fff/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0 GB)  TX bytes:0 (0 GB)

Installation

npm install nodejs-sh

Quick Reference

Passing Arguments

const {ls} = require('nodejs-sh')

ls('-l', '/tmp', '--color=never').pipe(process.stdout)

Non-zero Exit Codes

const {ls} = require('nodejs-sh')

try {
  await ls('/doesnt/exist')
} catch (exitCode) {
  if (exitCode === 2) console.log("directory doesn't exist")
}

Redirection

const fs = require('fs')
const {ls} = require('nodejs-sh')

let file = fs.createWriteStream('file.txt')
ls('test/').pipe(file).on('finish', () => {
  let output = fs.readFileSync('file.txt', 'utf8')
})

Piping

const {ls, wc} = require('nodejs-sh')

let output = await ls('-1').pipe(wc('-l')).toString()

Background Processes

const {find} = require('nodejs-sh')

let p = find('-name', 'index.js')
// ... do other things ...
await p

TypeScript

import * as sh from "nodejs-sh";

const output = await sh.ls("-1").pipe(sh.wc("-l")).toString();

FAQs

Package last updated on 19 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts