Skip to content

Instantly share code, notes, and snippets.

@aynik
Created March 31, 2018 10:27
Show Gist options
  • Save aynik/34061c766d19d2a5c1e419fb178f7ab2 to your computer and use it in GitHub Desktop.
Save aynik/34061c766d19d2a5c1e419fb178f7ab2 to your computer and use it in GitHub Desktop.
Entropy from ping roundtrips
#!/usr/bin/env node
// requires: npm i -g get-stdin net-ping
// usage: ping-rng [bits?] < hosts
const stdin = require('/usr/local/lib/node_modules/get-stdin')
const ping = require('/usr/local/lib/node_modules/net-ping')
let sid = 0
const probe = async (hosts) => {
const session = ping.createSession({
timeout: 200,
sessionId: sid
})
sid = (sid + 1) % 65535
return await Promise.all(hosts.filter(host => !!host).map((host) => (
new Promise((resolve, reject) => {
session.pingHost(host, (err, target, sent, rcvd) => {
if (err) return resolve(null)
return resolve(rcvd - sent)
})
})
)))
}
stdin().then(async (str) => {
const hosts = str.split(/\s+/g)
const c = parseInt(process.argv[2] || -1, 10)
let n = 0
while (c === -1 || n < c) {
let results = await probe(hosts)
let nibbles = results
.filter(time => time !== null)
.map(time => parseInt(time % 16).toString(16))
.join('')
n += nibbles.length * 4
if (c !== -1 && n >= c) {
nibbles = nibbles.substring(0, nibbles.length - ((n - c) / 4))
}
process.stdout.write(nibbles)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment