用于在ubuntu 12.04中更改MAC和主机名的Macxchange脚本

时间:2013-05-08 19:12:14

标签: perl bash shell scripting ubuntu-12.04

我正在尝试在回溯中调整脚本以使用Ubuntu。我在第14行继续收到错误,即if [ $? == 0 ]; then行。

脚本在http://pctechtips.org/script-to-change-mac-and-hostname-in-backtrack/

上找到

代码如下:

#!/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

主机文件(password.lst)的wordlist文件如下所示

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

我使用sh filename.sh运行脚本并且我已经安装了可执行文件并安装了macchanger但我仍然收到错误14: [: 0: unexpected operator Script encounter an error, sorry...

2 个答案:

答案 0 :(得分:0)

尝试

if [ $? = 0 ]; then

代替。 ==不是用bash编写它的规范方式,但大多数版本接受它。

不要忘记在下面第二次出现时重复修复。

答案 1 :(得分:0)

您可以将退出代码检查滚动到if语句本身:

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

另请注意,==the bash operator,用于字符串比较。退出代码是一个整数,因此检查其值的正确方法是-eq,它告诉bash将操作数解释为整数:

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

虽然程序没有出现像“00”而不是“0”这样的退出值的危险,但使用==会在这种情况下正常工作。