首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何验证服务器的名称列表

如何验证服务器的名称列表
EN

Stack Overflow用户
提问于 2021-03-19 09:23:24
回答 1查看 45关注 0票数 0

我正在通过脚本从数千个os服务器中提取NTP记录,列表如下所示。

包含服务器名称和NTP服务器的文件:

代码语言:javascript
复制
dbfx9005
server sas.ntp.gus.com iburst
server sas2.ntp.gus.com iburst
server sas1.ntp.gus.com iburst
dbfx9007
server sas.ntp.gus.com iburst
server sas2.ntp.gus.com iburst
dbfx9008
server sas.ntp.gus.com iburst
server sas2.ntp.gus.com iburst
server sas.ntp.gus.com iburst

我想做的是:

dbfx9005dbfx9007是服务器名称,以server开头的任何内容都是NTP服务器。我期待着一种方法来验证它下面的每个服务器是否包含在三个NTP服务器下面。

代码语言:javascript
复制
sas.ntp.gus.com 
sas2.ntp.gus.com
sas1.ntp.gus.com

如果其中任何一个不是这些,那么就像sas2.ntp.gus.com一样报告它,而不是在dbfx9005中报告。

编辑:,我希望把这三个名字命名为sas.ntp.gus.com , sas2.ntp.gus.com and sas1.ntp.gus.com,但是如果他们中的任何一个没有重复,那么只需打印它们,只显示一个服务器名。

我对Shell、Python或Pandas的任何想法都没意见。

谢谢你们的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-19 09:37:43

利用GNU awk:

代码语言:javascript
复制
awk '!/ntp/ {srv=$0;next } /sas((1)|(2))?.ntp.[[:alnum:]]+.[[:alnum:]]+/ { map[srv][$2]="1" } END { for (i in map) { if (map[i]["sas.ntp.gus.com"]!="") { print i" - sas.ntp.gus.com exists" } else { print i" - sas.ntp.gus.com DOESNT exist" }; if (map[i]["sas1.ntp.gus.com"]!="") { print i" - sas1.ntp.gus.com exists" } else { print i" - sas1.ntp.gus.com DOESNT exist" };if (map[i]["sas2.ntp.gus.com"]!="") { print i" - sas2.ntp.gus.com exists" } else { print i" - sas2.ntp.gus.com DOESNT exist" }; }}' file

解释:

代码语言:javascript
复制
awk '!/ntp/ {
              srv=$0;                                                  # For lines not containing "ntp" track the server name in the variable srv
              next 
            } 
      /sas((1)|(2))?.ntp.[[:alnum:]]+.[[:alnum:]]+/ { 
              map[srv][$2]="1"                                          # For ntp lines, use a 2 dimensional array with srv the first index and the ntp server address the second index
             } 
         END { 
              for (i in map) {                                         # At the end of processing the file, loop through the servers in the array map
                if (map[i]["sas.ntp.gus.com"]!="") { 
                   print i" - sas.ntp.gus.com exists"                  # If a second index exists for "sas.ntp.gus.com", print that it exists, otherwise print that it doesn't
                } 
                else { 
                   print i" - sas.ntp.gus.com DOESNT exist" 
                }; 
                if (map[i]["sas1.ntp.gus.com"]!="") { 
                   print i" - sas1.ntp.gus.com exists"                  # Follow the same logic for sas1 that was followed for sas
                } 
                else { 
                   print i" - sas1.ntp.gus.com DOESNT exist" 
                };
                if (map[i]["sas2.ntp.gus.com"]!="") { 
                   print i" - sas2.ntp.gus.com exists"                   # Follow the same logic for sas2 that was used for sas
                } 
                else { 
                   print i" - sas2.ntp.gus.com DOESNT exist" 
                }; 
               }
              }' file

输出:

代码语言:javascript
复制
dbfx9005 - sas.ntp.gus.com exists
dbfx9005 - sas1.ntp.gus.com exists
dbfx9005 - sas2.ntp.gus.com exists
dbfx9007 - sas.ntp.gus.com exists
dbfx9007 - sas1.ntp.gus.com DOESNT exist
dbfx9007 - sas2.ntp.gus.com exists
dbfx9008 - sas.ntp.gus.com exists
dbfx9008 - sas1.ntp.gus.com DOESNT exist
dbfx9008 - sas2.ntp.gus.com exists
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66705518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档