如何提取无法执行adinfo命令的服务器主机名及状态?
提取无法执行adinfo命令的服务器信息解决方案
需求说明
从包含多服务器输出的文件中,提取出无法执行adinfo命令的服务器主机名及对应的错误状态。文件示例格式如下:
server1 Local host name: server1 Joined to domain: domain.com Joined as: server1.domain.com Pre-win2K name: server1 Current DC: domain.com Preferred site: Datacenter Zone: servers Zone Last password set: 2022-03-11 02:23:55 EST CentrifyDC mode: connected Licensed Features: Enabled server2 bash: adinfo: command not found
过往尝试问题
- 执行
more file3 |grep 'com.*nd'仅能获取错误输出文本,无法关联对应的服务器主机名 - 执行
more file3 |grep -e 'Loc.*me' -e 'Cen.*de'可筛选出adinfo执行正常的服务器信息,但无法定位执行失败的服务器
解决方案
使用awk处理分段文本,通过记录当前服务器名,匹配错误行时关联输出:
命令示例
awk '/^server[0-9]+$/{host=$0; next} /bash: adinfo: command not found/{print host ": " $0}' file3
逻辑说明
- 匹配单独一行的服务器名(如
server1、server2),将其存入host变量后跳过当前行 - 匹配到
adinfo命令未找到的错误行时,输出已记录的服务器名和错误内容
适配不同服务器名格式
如果服务器名不是server+数字的格式,可调整匹配正则。例如服务器名由字母、数字、连字符组成且单独一行,可改为:
awk '/^[a-zA-Z0-9-]+$/{host=$0; next} /bash: adinfo: command not found/{print host ": " $0}' file3
执行后会输出类似结果:
server2: bash: adinfo: command not found
内容的提问来源于stack exchange,提问作者geek12
相关产品推荐
相关产品推荐

