显示IP的Shell脚本

显示IP的Shell脚本

我怎样才能创建一个接收向量参数的shell脚本

./script.sh google.com,yahoo.com

并显示每个域名的 IP

例如 google.com 100.23.123.13


编辑:

#!/bin/bash

while [ "$1" ]; 
do
host $1 | grep address | head -1
done

答案1

您可以使用以下脚本:

#!/bin/sh

for domain in $@; do
    host $domain | grep "has address" | awk '{print $1"\t"$4}'
done

用法:./script www.domain1.com domain2.com ...

例如:

$ ./script.sh www.google.com yahoo.com www.askubuntu.com google.ro
www.google.com 173.194.40.81
www.google.com 173.194.40.84
www.google.com 173.194.40.82
www.google.com 173.194.40.83
www.google.com 173.194.40.80
雅虎网 98.138.253.109
雅虎网 206.190.36.45
雅虎网 98.139.183.24
askubuntu.com 198.252.206.24
google.ro 173.194.35.31
google.ro 173.194.35.23
google.ro 173.194.35.24

答案2

您可以直接使用命令执行多查询查找dig,例如

dig google.com yahoo.com

或者不太详细的输出

dig +noall +answer google.com yahoo.com

或者只输出 IP

dig +short google.com yahoo.com

答案3

对于单个域输入>>

#!/bin/sh
#get IP
host $1 | head -n 1 | awk '{print $1,"\t",$4}'

对于多个域输入>>

#!/bin/sh
#get IP
for url in $@; do
    host $url | head -n 1 | awk '{print $1,"\t",$4}'
done

答案4

你可以用这种方法来做。

打开终端

$ gedit get_domain_ip.sh &

将以下行添加到文件中并保存。

#!/bin/bash

read -p "Enter domain name : " DOMAIN_INPUT

host $DOMAIN_INPUT

#END

将脚本标记为可执行

$ chmod +x get_domain_ip.sh

测试

neo@neo-H87H3-M:~$ ./get_ip.sh

Enter domain name : google.com    
google.com has address 173.194.72.102    
google.com has address 173.194.72.101    
google.com has address 173.194.72.100    
google.com has address 173.194.72.139    
google.com has address 173.194.72.138    
google.com has address 173.194.72.113    
google.com has IPv6 address 2404:6800:4008:c00::8a    
google.com mail is handled by 50 alt4.aspmx.l.google.com.    
google.com mail is handled by 20 alt1.aspmx.l.google.com.    
google.com mail is handled by 30 alt2.aspmx.l.google.com.    
google.com mail is handled by 10 aspmx.l.google.com.    
google.com mail is handled by 40 alt3.aspmx.l.google.com.

neo@neo-H87H3-M:~$

相关内容