Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

automatically scan for and join open internet enabled wifi networks on linux using node.js (tested on raspberry pi raspbian)

View readme.md
View readme.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#!/usr/bin/env node
var iw = require('iwlist')(process.argv[2] || 'wlan0')
var tried = {}
var silent = process.argv[4]
 
// wait for some time so that linux can try to associate with a known network first
var delay = process.argv[3] || 30000
if (!silent) console.log('waiting ' + delay/1000 + ' seconds')
setTimeout(start, process.argv[3] || 30000)
 
function start() {
iw.associated(function(err, associated) {
if (associated) {
if (!silent) console.log('already associated -- exiting')
return process.exit()
}
findOpenNetwork()
})
}
 
function findOpenNetwork() {
if (!silent) console.log('scanning for open networks...')
iw.scan(function(err, networks) {
if (err) {
if (!silent) console.log('error scanning', err)
return process.exit()
}
networks = removeSecureNetworks(networks)
if (networks.length === 0) {
if (!silent) console.log('no open networks nearby')
return process.exit()
}
var network = getNextNetwork(networks)
if (!silent) console.log('attempting to join ' + network.essid)
connectToNetwork(network.essid)
})
}
 
function connectToNetwork(essid) {
iw.connect(essid, function(err) {
if (err) {
if (!silent) console.log('error joining ' + essid, err)
return start()
}
iw.online(function(err) {
if (err) {
if (!silent) console.log(essid + ' is not internet enabled', err)
return findOpenNetwork()
}
if (!silent) console.log('got online successfully via network: ' + essid)
process.exit()
})
})
}
 
function removeSecureNetworks(networks) {
openNetworks = []
networks.map(function(network) {
if (!network.encrypted) openNetworks.push(network)
})
return openNetworks
}
 
function getNextNetwork(networks) {
var network = networks.shift()
if (!network) return process.exit()
while (tried[network.essid]) {
network = networks.shift()
}
return network
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.