bash - 用于IP范围的循环,不包括某些IP [英] bash - for loop for IP range excluding certain IPs

查看:335
本文介绍了bash - 用于IP范围的循环,不包括某些IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下for循环

  for ip in 10.11。{32..47}。{0..255 } 
do
echo< ip> $ {ip}< / ip>
done

我想排除这个iprange: 10.11。{ 32..35}。{39..61} 来自上面的for循环。此ip范围是上述范围的一个子集。有没有办法做到这一点?



我试过这个,这不起作用:

  abc = 10.11。{34..37}。{39..61} 
for ip in 10.11。{32..47}。{0..255}
如果[[$ ip == $ abc]]
那么
然后
echonot_defined
else
echo< ip> $ {ip}< / IP>中
fi
done


解决方案

尝试这个:

  for 10 in 10.11。{32..47}。{0..255} 
do
echo 10.11。{32..35}。{39..61} | grep -q\< $ ip \> &安培;&安培;继续
echo< ip> $ {ip}< / ip>
done

这当然是一个简单的解决方案,它仍然循环完整的设置和抛出远离一些不需要的元素正如您的评论所暗示的那样,这可能会在跳过的部分中产生不必要的延迟。为了避免这些,你可以像这样处理并行生成值:

  for 10 in 10.11。{32 .. 47}。{0..255} 
do
echo 10.11。{32..35}。{39..61} | grep -q\< $ ip \> &安培;&安培;继续
echo$ {ip}
done |读取ip
执行
流程$ ip
完成

如果进程$ ip至少花费了很少的时间,那么生成值的时间很可能不会再考虑在内了。 / p>

如果你想完全跳过这些值,你也可以为你的IP使用更复杂的术语(但是后来不再清楚这个代码是如何从规范中获得的你提出了你的问题,所以我最好对它进行彻底评论):

 #范围低于
#10.11。 {32..47}。{0..255}没有10.11。{32..35}。{39..61}:
for ip in 10.11。{32..35}。{{0。 .38},{62..255}} 10.11。{36..47}。{0..255}
do
echo$ {ip}
done


I have the below for loop

for ip in 10.11.{32..47}.{0..255}
do
        echo "<ip>${ip}</ip>" 
done

I want to exclude this iprange: 10.11.{32..35}.{39..61} from the above for loop. This ip range is a subset of the above one. Is there a way to do that?

I tried this, this doesn't work:

abc=10.11.{34..37}.{39..61}
for ip in 10.11.{32..47}.{0..255}
do
    if [[ $ip == $abc ]]
    then
            echo "not_defined"
    else
            echo "<ip>${ip}</ip>"
    fi
done

解决方案

Try this:

for ip in 10.11.{32..47}.{0..255}
do
        echo 10.11.{32..35}.{39..61} | grep -q "\<$ip\>" && continue
        echo "<ip>${ip}</ip>" 
done

This of course is a simple solution which still loops through the complete set and throws away some unwanted elements. As your comment suggests, this may produce unnecessary delays during the parts which are skipped. To avoid these, you can generate the values in parallel to the processing like this:

for ip in 10.11.{32..47}.{0..255}
do
        echo 10.11.{32..35}.{39..61} | grep -q "\<$ip\>" && continue
        echo "${ip}" 
done | while read ip
do
        process "$ip"
done

If the process "$ip" is taking at least a minimal amount of time, then the time for the generation of the values will most likely not fall into account anymore.

If you want to skip the values completely, you also can use a more complex term for your IPs (but then it will not be clear anymore how this code derived from the spec you gave in your question, so I better comment it thoroughly):

# ranges below result in
# 10.11.{32..47}.{0..255} without 10.11.{32..35}.{39..61}:
for ip in 10.11.{32..35}.{{0..38},{62..255}} 10.11.{36..47}.{0..255}
do
        echo "${ip}" 
done

这篇关于bash - 用于IP范围的循环,不包括某些IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆