2016-11-14 38 views
1

我有一个分布式网络监视系统的综合主机文件,我需要将其分割为两个文件。一些主机进入一个文件,其余的进入另一个文件。我正在使用sed来拉出我需要的主机,但是我不知道如何将其他所有文件都放在其他文件中,比如选项grep。主机文件的选择除文本块外的所有内容

例子:

object Host "router" { 
    import "template" 
    display_name = "router" 
    address = "xx.xx.xx.xx " 
} 

object Host "switch" { 
    import "template" 
    display_name = "switch" 
    address = "xx.xx.xx.xx " 
} 

object Host "router" { 
    import "template" 
    display_name = "router" 
    address = "xx.xx.xx.xx " 
} 

object Host "otherthing" { 
    import "template" 
    display_name = "otherthing" 
    address = "xx.xx.xx.xx " 
} 

object Host "switch" { 
    import "template" 
    display_name = "switch" 
    address = "xx.xx.xx.xx " 
} 

object Host "otherthing" { 
    import "template" 
    display_name = "otherthing" 
    address = "xx.xx.xx.xx " 
} 

sed命令我使用的是拉出来的路由器

sed '/router.*\n/p;//g;/{$/,/^}/H;//x;D' files/tmp/unsorted-host.tmp >> files/router.conf 

此外,我不知道这是否会更有效率,而不是排序建立大型主机文件,但这里是我如何使大型主机文件:

while read line 
     do 
       # The SNMP to check the device type outputs on multiple lines, check to ensure we only grab IP's 
       getip=$(echo $line | awk '{print $1}') 
       if ping -c 1 $getip &> /dev/null 
         then 
         ip=$getip 
         else 
         trash=$getip 
       fi 
       template="template" 
       host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community1 $ip 1.3.6.1.2.1.1.5.0 | awk '{print $NF}') 
       if [ -z "$host_name" ] 
         then 
         host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community2 $ip 1.3.6.1.2.1.1.5.0 | awk '{print $NF}') 
         if [ -z "$host_name" ] 
           then 
           host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community3 $ip 1.3.6.1.2.1.1.5.0 | awk '{print $NF}') 
           if [ -z "$host_name" ] 
             then 
             host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community4 $ip 1.3.6.1.2.1.1.5.0 | awk '{print $NF}') 
             if [ -z "$host_name" ] 
               then 
               host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community5 $ip 1.3.6.1.2.1.1.5.0 | awk '{print $NF}') 
               if [ -z "$host_name" ] 
                 then 
                 host_name=$(echo " Not found ") 

               fi 
             fi 
           fi 
         fi 
       fi 
       echo " 
object Host $host_name { 
    import \"$template\" 
    display_name = $host_name 
    address = \"$ip \" 
}" 
     done <files/tmp/hosts.tmp> files/tmp/unsorted-hosts.tmp 

目标是有两个文件,一个是路由器,另一个是其他所有文件。提前感谢任何和所有的建议!

+0

我没有详细看过你的'sed'语句,但是如果你能用'sed'找到你需要的所有东西,那么你不能只是做一个' sed'替换删除路由器材料? – TTT

回答

1

这不是一个答案:我觉得大的嵌套if痛苦。一个快速改写:

while read -r ip rest_of_line; do 
    # The SNMP to check the device type outputs on multiple lines, check to ensure we only grab IP's 
    ping -c 1 "$ip" &> /dev/null || continue 
    template="template" 
    host_name= 
    for n in 1 2 3 4 5; do 
     host_name=$(timeout -s KILL 2 /usr/bin/snmpwalk -v 2c -c community$n "$ip" 1.3.6.1.2.1.1.5.0 | awk '{print $NF}') 
     [ -n "$host_name" ] && break 
    done 
    if [ -z "$host_name" ]; then 
     echo "host not found for ip $ip" >&2 
     continue 
    fi 
    echo " 
object Host $host_name { 
    import \"$template\" 
    display_name = $host_name 
    address = \"$ip \" 
}" 
done <files/tmp/hosts.tmp> files/tmp/unsorted-hosts.tmp 
+0

我希望我能做到这样的事情,但社区实际上是文本字符串。我只是用community1 community2等取代了他们的隐私。 – cflinspach

+0

然后在'community1 community2 community3 ...'中做''c' –

+0

我给它一个镜头,谢谢! 这个网站真的很棒,你们都很棒。 – cflinspach

相关问题