portscanner-sync v1.0.1
portscanner-sync
The portscanner module is a synchronous port scanner for Node.js
Portscanner can check a port, or range of ports, for 'open' or 'closed' statuses.
Install
npm install portscanner-sync
Usage
A brief example:
const portScanner = require('portscanner-sync');
const to = require('await-to-js').to;
let main = async () => {
let [err, care] = [];
let status;
let port;
// Checks the status of an individual port.
[err, care] = await to(portScanner.checkPortStatus(3005, '127.0.0.1'));
status = care;
if (err) {
console.error(err)
} else {
console.log('Status at port 3005 is ' + status)
}
[err, care] = await to(portScanner.checkPortStatus(2999, '127.0.0.1'));
status = care;
if (err) {
console.error(err)
} else {
console.log('Status at port 2999 is ' + status)
}
[err, care] = await to(portScanner.checkPortStatus(3000, '127.0.0.1'));
status = care;
if (err) {
console.error(err)
} else {
console.log('Status at port 3000 is ' + status)
}
[err, care] = await to(portScanner.checkPortStatus(3001, '127.0.0.1'));
status = care;
if (err) {
console.error(err)
} else {
console.log('Status at port 3001 is ' + status)
}
// Finds a port that a service is listening on
[err, care] = await to(portScanner.findAPortInUse(2900, 3010, '127.0.0.1'));
port = care
if (err) {
console.error(err)
} else {
console.log('Found an open port at ' + port)
}
[err, care] = await to(portScanner.findAPortNotInUse([80,3000, 3010], '127.0.0.1'));
port = care
if (err) {
console.error(err)
} else {
console.log('Found a closed port at ' + port)
}
}
main()
The example directory contains a more detailed example.
Test
npm test
API
Refer to the documentation
Modules
Functions
portscanner
checkPortStatus(port, host, timeout) ⇒ Array.<string>
Kind: global function
Returns: Array.<string> - - err, success. success is either of closed/open. Closed if port is not in use, open otherwise
Param | Type | Default | Description |
---|---|---|---|
port | Number | Port to check status on. | |
host | String | '127.0.0.1' | Host of where to scan. |
timeout | Number | 400 | timeout. |
findAPortInUse(postList, host) ⇒ Array.<string>
Kind: global function
Returns: Array.<string> - - error, port
Param | Type | Default | Description |
---|---|---|---|
postList | Array | Array of ports to check status on. | |
host | String | '127.0.0.1' | Host of where to scan. |
Example
// scans 3000 and 3002 only, not 3001.
portscanner.findAPortInUse([3000, 3002], console.log)
findAPortNotInUse()
Finds the first port with a status of 'closed', implying the port is not in use. Accepts identical parameters as findAPortInUse
Kind: global function
findAPortWithStatus(...params)
Kind: global function
Param | Type | Description |
---|---|---|
...params | params | Params as passed exactly to findAPortInUse and findAPortNotInUse. |
Developed by CSECO
CSECO is a mechatronics firm specializing in engineering technology to be cheap enough to be affordable to low income earners.