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,173
  • 4
  • 26
  • 42

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
  • 45,233
  • 13
  • 70
  • 67
  • Thanks geirha that helped but when I check the connection information using ubuntu 12.04 in the networking gui to verify I noticed the hardward address does not change. Do you know how I can fix this? – Rick T May 08 '13 at 20:24
  • @RickT maybe macchanger is not installed? What does `type macchanger` output? – geirha May 08 '13 at 20:31
  • Geirha it's installed and it changes the Hardware Address in the terminal window (I checked using ifconfig) but it doesn't change in the ubuntu networking gui – Rick T May 08 '13 at 20:34
  • I did type macchanger and this output came out GNU MAC Changer Usage: macchanger [options] device Try `macchanger --help' for more options. – Rick T May 08 '13 at 20:39
  • @RickT ah, so Network Manager doesn't detect the change? perhaps try restarting network manager: `sudo restart network-manager` – geirha May 08 '13 at 21:19
  • geirha correct any changes I make with ifconfig seems to get over written by ubuntu's network-manager process. – Rick T May 08 '13 at 21:25
  • @RickT Try using `nmcli` to disable/enable the interface, instead of using `ifconfig` – geirha May 09 '13 at 09:37
  • thanks geirha I gave it a try with info from this page http://arstechnica.com/civis/viewtopic.php?f=16&t=1163023. To see the status of a connection you can go: nmcli con status id 'Wired connection 1' To bring down the interface and disconnect you'd go: nmcli con down id 'Wired connection 1' nmcli dev disconnect iface eth0 to bring it back up: nmcli con up id 'Wired connection 1' It didn't work but I'm still searching – Rick T May 09 '13 at 18:59