feross / hostile

Simple, programmatic `/etc/hosts` manipulation (in node.js)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Removing hostnames doesnt work

menecken opened this issue · comments

From batch nothing happens from nodeJS-App it messes up the file around the entry that should be deleted (missing linebrakes). I'm on rapsbian.

Code I'm using

var preserveFormatting = false
hostile.get(preserveFormatting, function (err, lines) {
    if (err) {
        console.error('getting lines failed: ' + err.message)
    }
    lines.forEach(function (line) {
        console.log("found line with ip=" + line[0] + " and entry=" + line[1]);
        if (line[0] == "127.0.1.1")
        {
            console.log("removing line with ip=" + line[0] + " and entry=" + line[1]);
            hostile.remove(line[0], line[1] , function (err) {
                if (err) {
                    console.error('removing etc/hosts/ failed: ' + err)
                } else {
                    console.log('removed /etc/hosts successfully!')
                }
            })
        }
    })
})

Writes to the hosts file are not atomic. You need to wait for one hostile.remove to complete before calling it again.

See: #9 and #12.