Last active
January 25, 2022 09:01
-
-
Save benjamingr/ead0583c9979007e42ed3b99650636fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { connect } from 'net'; | |
import { once } from 'events'; | |
import { Readable } from 'stream'; | |
function* range(from, to) { | |
for(let i = from; i < to; i++) yield i; | |
} | |
async function tryToConnectToPort(port, host) { | |
const socket = connect(port, host); | |
try { | |
await once(socket, 'connect'); | |
} catch (e) { | |
return false; | |
} finally { | |
socket.end(); | |
} | |
return true; | |
} | |
const from = process.env.FROM_PORT ?? 2000; | |
const to = process.env.TO_PORT ?? 3000; | |
const concurrency = process.env.PARALLELISM ?? 100; | |
const status = await Readable.from(range(from, to)).map(async x => { | |
const hasPortOpen = await tryToConnectToPort(x, process.env.HOST ?? '20.203.26.203'); | |
return { port: x, open: hasPortOpen }; | |
}, { concurrency }).filter(x => x.open).toArray(); | |
console.log("Port status", status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: