IP主机脚本bash [英] Ip host script bash

查看:69
本文介绍了IP主机脚本bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要显示主机名,则在第一个字段中,这是文件中的第五个字段.然后,如果我们提供给主机的IP命令不存在,那么该命令将返回消息3(NXDOMAIN).该脚本应识别该命令是否未找到".在这种情况下,它将 必须简单地打印(-).

In the first if we want the hostname to appear, which is the 5th field from a file. Then if the IP we give to the host command does not exist, then the command returns message 3 (NXDOMAIN). The script should recognize if the command was "not found". In this case it will must simply print (-).

#!/bin/bash
ip="$1"

if [ "$ip" ] ; then
         host "$ip" | cut -d' ' -f5

 elif
         [[ "$ip" =~ "[3(NXDOMAIN)]$" ]] ; then
                echo "-"
fi

您对此练习有什么解决办法吗?

Do u have any solution on this exercise?

推荐答案

答案比您想象的要简单得多,您不需要进行 任何 匹配.您可以只使用host

The answer is much simpler than you think, you don't need to do any matching. You can just use the return code from host

#!/bin/bash
ip="$1"

if domain=$(host "$1"); then
  echo "${domain##* }"
else
  echo "-"
fi

概念证明

$ testHost(){ if domain=$(host "$1"); then echo "${domain##* }"; else echo "-"; fi }
$ testHost 172.217.6.46
sfo03s08-in-f14.1e100.net.
$ testHost 172.217.6.466
-

这篇关于IP主机脚本bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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