Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
理解LinuxBash脚本的一部分_Linux_Bash_Shell_Syntax - Fatal编程技术网

理解LinuxBash脚本的一部分

理解LinuxBash脚本的一部分,linux,bash,shell,syntax,Linux,Bash,Shell,Syntax,我试图理解一个LinuxBash脚本。该脚本的目的是限制仅对某些dyndns用户访问服务器服务(通过使用ufw规则)。脚本的一部分: ALLOWEDUSERS="client1.dyndns.org client2.dyndns.org" for host in $ALLOWEDUSERS ; do ip=`host $host | cut -d ' ' -f 4` if [ $? -eq 0 ]; then ufw allow proto tcp from $i

我试图理解一个LinuxBash脚本。该脚本的目的是限制仅对某些dyndns用户访问服务器服务(通过使用ufw规则)。脚本的一部分:

ALLOWEDUSERS="client1.dyndns.org client2.dyndns.org"

for host in $ALLOWEDUSERS ; do
    ip=`host $host | cut -d ' ' -f 4`
    if [ $? -eq 0 ]; then
       ufw allow proto tcp from $ip to any
    fi
done
好的

很明显,它通过允许的用户循环

据我所知

if [ $? -eq 0 ]; then
检查之前执行的命令是否为真(如果为真,则添加ufw规则)

但是片段的其余部分是如何处理的呢

ip=`host $host | cut -d ' ' -f 4`
检查客户端ip是否来自允许的dyndns帐户

非常感谢你的帮助


托尼

它实际上没有检查任何东西

host$host
的输出类似于
$host的地址为xxx.xxx.xxx.xxx

例如:

$ host localhost
localhost has address 127.0.0.1

然后
cut-d'-f4
隔离第四部分,即ip地址。该脚本用作
ufw
命令的ip地址。

该脚本实质上等同于:

ALLOWEDUSERS="client1.dyndns.org client2.dyndns.org"

for host in $ALLOWEDUSERS ; do
    ip=`host $host | cut -d ' ' -f 4`
    ufw allow proto tcp from $ip to any
done
原始脚本中的
if
检查的是
cut
的结果,而不是
host
,而且总是成功的,因此没有任何用处

当DynDNS主机名有效时,将向防火墙添加一条规则以允许它

当找不到主机名时,
host
命令将打印:

Host clientN.dyndns.org not found: 3(NXDOMAIN)
因此,
$ip
将被
找到:
。这将尝试做到:

ufw allow proto tcp from found: to any
由于这不是有效的防火墙规则,我希望它将被忽略并发出错误消息

如果您想执行脚本显然试图执行的操作,则应该是:

ALLOWEDUSERS="client1.dyndns.org client2.dyndns.org"

for host in $ALLOWEDUSERS ; do
    hostresult=`host $host`
    if [ $? -eq 0 ]; then
        ip=`echo "$hostresult" | cut -d ' ' -f 4`
        ufw allow proto tcp from $ip to any
    fi
done

这个脚本写得很糟糕,可能已经坏了。为什么要浪费时间?Hi Barmar“为什么要浪费时间?”-出于学习目的。程序员显然认为他正在检查
主机
命令的退出状态,如果主机名查找成功,则该命令为
0
。但它实际上是在检查
cut
的退出状态,因为管道的状态是最后一个命令的状态。所以它没有按预期的那样工作。@t本子我没有写那个。@t本子然后从其他更好的脚本中学习。这东西是虫窝<代码>剪切在这种情况下将始终返回true,因此
ufw
将始终运行。哦,亲爱的,谢谢你的回答!竖起大拇指。
ALLOWEDUSERS="client1.dyndns.org client2.dyndns.org"

for host in $ALLOWEDUSERS ; do
    hostresult=`host $host`
    if [ $? -eq 0 ]; then
        ip=`echo "$hostresult" | cut -d ' ' -f 4`
        ufw allow proto tcp from $ip to any
    fi
done