0

我正在尝试调整回溯中的脚本以与 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) 的单词表文件如下所示

# 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...

4

2 回答 2

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

另请注意,这==是用于字符串比较的 bash 运算符。退出代码是一个整数,因此检查其值的正确方法是 with -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”这样的退出值的危险,但==在这种情况下使用将按您的预期工作。

于 2013-05-08T21:22:06.477 回答
0

试试看

if [ $? = 0 ]; then

反而。这==不是用 bash 编写它的规范方式,但大多数版本都接受它。

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

于 2013-05-08T19:35:04.967 回答