限制催生子shell数 [英] Limiting the number of subshells spawned

查看:118
本文介绍了限制催生子shell数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图限制我使用嗅探我们的内部网络审计我们的网络在Linux服务器的那些在脚本催生子shell量。该脚本作品如预期,但由于办法,我嵌套循环,它生成每个网络255子壳,因此,它杀死了CPU因事实上,有超过1000个进程催生了。我需要能够限制进程数量,因为变量失去其价值当在-Sub外壳,我不能想出一个办法,使这项工作。同样,脚本,但它只是产生一个吨的进程 - 我需要限制它,说10个进程最大:

 #!/斌/庆典FILE = /根/ ats_net_final在`$猫网络FILE`;做
                在$网ip {1..25​​5};做
                        (
                                SYSNAME =`snmpwalk的-v2c -c公共-t1 -r1 $ IP sysName.0 2 - ;的/ dev / null的| AWK'{$打印NF}'`
                                SYSTYPE =`snmpwalk的-v2c -c公共-t1 -r1 $ IP sysDescr.0 2 - ;的/ dev / null的| grep的-o Linux`
                                如果[$? -eq 0];然后
                                        回声$ SYSNAME
                                        退出0;
                                其他
                                        回声加工$ IP
                                        退出0
                                科幻
                        )及
                DONE
DONE

解决方案,我发现这样的作品,但不在我的情况,因为无论什么时候,它仍然会限制程​​序的逻辑之前产卵的过程。我想,也许我刚刚在code太长看,它只是一个简单的逻辑问题,我把东西在错误的区域,或以错误的顺序。


答接受:<​​/ H1>

我已经接受了来自huitseeker答案。他能够为我提供的逻辑是如何工作的,让我得到它的工作方向。最终脚本:

 #!/斌/庆典FILE = /根/ ats_net_final在`$猫网络FILE`;做
        #for在IP网络$ {} 1..25​​5;请
        在{} 1..25​​5 IP;做
            (
                IP = $网络。$ IP
                SYSNAME =`snmpwalk的-v2c -c公共-t1 -r1 $ IP sysName.0 2 - ;的/ dev / null的| AWK'{$打印NF}'`
                SYSTYPE =`snmpwalk的-v2c -c公共-t1 -r1 $ IP sysDescr.0 2 - ;的/ dev / null的| grep的-o Linux`
                如果[$? -eq 0];然后
                    回声$ SYSNAME
                    退出0;
                其他
                    回声加工$ IP
                    退出0
                科幻
            )及
            如果(($ IP%10 == 0));然后等待;科幻
        DONE
        等待
DONE


解决方案

这是简单的方法来并行子shell的数量限制为10:

 在$(SEQ 1 255)的IP;做好
  (小于无论你在这里的子shell $ IP一样,使用$网络IP $替代方式&gt;)及
  如果(($ IP%10 == 0));然后等待;科幻
DONE
等待

随着最后等待是有用的,不要让最后一轮与那些在第一轮的下一个外部运行创造了内环重叠的子shell。

I'm trying to limit the amount of subshells that are spawned in a script that I'm using to sniff our internal network to audit Linux servers in our network. The script works as intended, but due to the way I'm nesting the for loop, it spawns 255 Sub Shells for each network, so therefore it kills the CPU due to the fact that there are over 1000 processes spawned off. I need to be able to limit the amount of processes, and since variables lose their value when in a Sub Shell, I can't figure out a way to make this work. Again, the script works, it just spawns a ton a processes - I need to limit it to, say 10 Processes max:

#!/bin/bash

FILE=/root/ats_net_final

for network in `cat $FILE`;do
                for ip in $network.{1..255};do
                        (
                                SYSNAME=`snmpwalk -v2c -c public -t1 -r1 $ip sysName.0 2>/dev/null | awk '{ print $NF }'`
                                SYSTYPE=`snmpwalk -v2c -c public -t1 -r1 $ip sysDescr.0 2>/dev/null | grep -o Linux`
                                if [ $? -eq 0 ];then
                                        echo "$SYSNAME"
                                        exit 0;
                                else
                                        echo "Processed $ip"
                                        exit 0
                                fi
                        ) &
                done
done

This solution I found that works, but not in my case, because no matter what, it still will spawn the processes before the logic of limiting the processes. I think that maybe I've just been looking at the code too long and it's just a simple logic issue and I'm placing things in the wrong area, or in the wrong order.


Answer Accepted:

I've accepted the answer from huitseeker. He was able to provide me with direction of how the logic works, allowing me to get it to work. Final script:

#!/bin/bash

FILE=/root/ats_net_final

for network in `cat $FILE`;do
        #for ip in $network.{1..255};do
        for ip in {1..255};do
            (
                ip=$network.$ip
                SYSNAME=`snmpwalk -v2c -c public -t1 -r1 $ip sysName.0 2>/dev/null | awk '{ print $NF }'`
                SYSTYPE=`snmpwalk -v2c -c public -t1 -r1 $ip sysDescr.0 2>/dev/null | grep -o Linux`
                if [ $? -eq 0 ];then
                    echo "$SYSNAME"
                    exit 0;
                else
                    echo "Processed $ip"
                    exit 0
                fi
            ) &
            if (( $ip % 10 == 0 )); then wait; fi
        done
        wait
done

解决方案

An easy way to limit the number of concurrent subshells to 10 is:

for ip in $(seq 1 255);do
  (<whatever you did with $ip in the subshell here, do with $network.$ip instead >) &
  if (( $ip % 10 == 0 )); then wait; fi
done
wait

With the last wait being useful not to let the subshells of the last round of the inner loop overlap with those created in first round of the next outer run.

这篇关于限制催生子shell数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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