Created
October 12, 2019 21:02
-
-
Save theTd/0b6851794d11d8d17058a7bd548d8e42 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
const exec = require('child_process').exec | |
const readline = require('readline'); | |
const cmd = "ifconfig pppoe-wan |grep 'inet addr' |cut -d: -f2|awk '{print $1}'" | |
const ipPattern = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ | |
function get_pppoe_ip() { | |
return new Promise((resolve, reject) => { | |
exec(cmd, (err, stdout, stderr) => { | |
stdout = stdout.replace(/\n/g, ""); | |
if (err) { | |
reject("error: " + err) | |
} | |
if (ipPattern.exec(stdout)) { | |
resolve(stdout); | |
} else { | |
reject(""); | |
} | |
}); | |
}); | |
} | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} | |
(async () => { | |
while (true) { | |
try { | |
let ip = await get_pppoe_ip(); | |
process.stdout.write(ip + "\n"); | |
process.exit(0); | |
} catch (exception) { | |
} | |
await sleep(500); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment