0

I'm trying to adapt a script in backtrack to work with ubuntu. I keep getting an error on line 14 which is the "if [ $? == 0 ]; then" line

the script was found on http://pctechtips.org/script-to-change-mac-and-hostname-in-backtrack/

the code is below

#!/bin/bash
#author: Jorge L. Vazquez
#purpose: this script will change the mac address to random
#and will pick a random word from password.lst in jtr for hostname
#change variables "interface" and "file" to your settings
#also macchanger needs to be installed

INTERFACE=eth0
FILE=/pentest/passwords/jtr/password.lst
WORD=$(sort -R $FILE | head -1)

#changing mac address to random
ifconfig $INTERFACE down > /dev/null
if [ $? == 0 ]; then
    printf "%s\nChanging mac address...\n"
    macchanger -r $INTERFACE
else
    printf "%sScript encounter an error, sorry...\n"
    exit 1
fi

#changing hostname to random word from password.lst
printf "%s\nChanging Hostname...\n"
OLDHOST=$(hostname)
hostname $WORD
if [ $? == 0 ]; then
    printf "%sPrevius Hostname: $OLDHOST \n"
    printf "%sRandom Hostname: $WORD \n"
else
    printf "%sScript encounter an error, sorry...\n"
    exit 1
fi

#putting interface up
ifconfig $INTERFACE up > /dev/null
printf "\n"


#END

And the wordlist file for the hostfile (password.lst) looks like this

# A couple of merged /etc/hosts files -- 
#
4000ex
a-lhi-bbn-01
a-lhi-sri-03
a00
a1
a2

I run the script using sh filename.sh and i've given it executable access and macchanger is installed but I still get an error

Rick T
  • 2,303

1 Answers1

2

sh is not bash, so don't run a bash script with sh. Run it with either ./filename (requires that the script is executable, and uses the shebang) or bash ./filename (only requires the script to be readable, and the shebang is ignored).

It's also not a good idea to use extensions for scripts, especially not .sh when the script is not an sh script.

The script itself uses POSIX sh compatible syntax, with the exception of the [ command which have no == operator in POSIX. To compare two strings for equality with the [ command, the operator is =. [ "$?" = 0 ].

That said, testing whether $? is 0 or not is a bit pointless. It's better to just test the command directly, rather than run the command, then run another command to determine if the previous command succeeded.

if ifconfig "$interface" down > /dev/null; then
    ...
else
    ...
fi
geirha
  • 47,279