如何使用bash脚本打印文件中域名的ip地址

问题描述 投票:0回答:1

我一直在尝试编写一个代码来检查文件是否包含域名,如果包含,则使用 bash 脚本打印出域名 这是我的代码:

is_valid_domain() {
    local domain="$1"
    if host "$domain" &> /dev/null; then
        return 0
    else
        return 1
    fi
}


if [ "$#" -eq 1 ]; then
    filename="$1"
    # Check if the file exists
    if [ ! -f "$filename" ]; then
        echo "Error: File '$filename' not found."
        exit 1
    fi
    
    #if the file exists
    #Process each domain in the file
    while IFS= read -r domain_name;  do
        if  is_valid_domain "$domain_name"; then
echo -e "Domain name: $domain_name\nIP Address: $(host "$domain_name" | awk '/has address/ {print $4}')"
        else
            echo "No domain names found"
        fi
    done < "$filename"

当我使用包含域名作为第一个命令行参数的文件运行脚本时,它只是暂停操作并且不会继续,我做错了什么吗?

linux bash
1个回答
0
投票

...暂停动作并且不再继续...

我怀疑它挂在了

host
,因为你给它提供了一个需要很长时间才能检查的域名。您可以使用
-W <seconds>
选项覆盖默认超时时间:

is_valid_domain() {
    local domain="$1"
    host -W 1 "$domain" > /dev/null
}
© www.soinside.com 2019 - 2024. All rights reserved.