Skip to content

macchanger failure logs the incorrect exit code

Look at this code from tails-spoof-mac:

spoof_mac() {
    local msg
    if ! msg=$(macchanger -e "${1}" 2>&1); then
        log "macchanger failed for NIC ${1}, returned ${?} and said: ${msg}"
        exit 1
    fi
}

If macchanger fails, the if-statement’s body will be executed as expected, but the ${?} will not refer to macchanger’s exit status, but to the if-statement’s test expression, i.e. the ! ... stuff, which means we’ll always log an exit status of 0.

To be able to capture but the output and exit status of macchanger we probably will have to do something like this:

spoof_mac() {
    local msg
    set +e
    msg=$(macchanger -e "${1}" 2>&1)
    ret=$?
    set -e
    if [ "${ret} != 0 ]; then
        log "macchanger failed for NIC ${1}, returned ${ret} and said: ${msg}"
        exit 1
    fi
}

Feature Branch: bugfix/8687-macchanger-return-status

Related issues

Original created by @anonym on 8687 (Redmine)

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information