shell - nslookup/dig/drill commands on a file that contains websites to add ip addresses
问题描述
UPDATE : Still open for solutions using nslookup without parallel
, dig
or drill
I need to write a script that scans a file containing web page addresses on each line, and adds to these lines the IP address corresponding to the name using nslookup
command. The script looks like this at the moment :
#!/usr/bin/
while read ip
do
nslookup "$ip" |
awk '/Name:/{val=$NF;flag=1;next} /Address:/ &&
flag{print val,$NF;val=""}' |
sed -n 'p;n'
done < is8.input
The input file contains the following websites :
www.edu.ro
vega.unitbv.ro
www.wikipedia.org
The final output should look like :
www.edu.ro 193.169.21.181
vega.unitbv.ro 193.254.231.35
www.wikipedia.org 91.198.174.192
The main problem i have with the current state of the script is that it takes the names from nslookup
(which is good for www.edu.ro) instead of taking the aliases when those are available. My output looks like this:
www.edu.ro 193.169.21.181
etc.unitbv.ro 193.254.231.35
dyna.wikimedia.org 91.198.174.192
I was thinking about implementing a if-else
for aliases but i don't know how to do one on the current command. Also the script can be changed if anyone has a better understanding of how to format nslookup to show it like the output given.
解决方案
极简主义解决方法准答案。这是使用GNU 的脚本的单行替换parallel
,host
(解析的工作量少于nslookup
),并且sed
:
parallel "host {} 2> /dev/null |
sed -n '/ has address /{s/.* /'{}' /p;q}'" < is8.input
...或以增加GNU复杂性nslookup
为代价使用。 sed
parallel "nslookup {} 2> /dev/null |
sed -n '/^A/{s/.* /'{}' /;T;p;q;}'" < is8.input
...或使用xargs
:
xargs -I '{}' sh -c \
"nslookup {} 2> /dev/null |
sed -n '/^A/{s/.* /'{}' /;T;p;q;}'" < is8.input
任何这些的输出:
www.edu.ro 193.169.21.181
vega.unitbv.ro 193.254.231.35
www.wikipedia.org 208.80.154.224
推荐阅读
- php - FPDF 开关或单元格中的 if 语句
- acumatica - 为什么在使用 PXDatabase.GetSlot 从表中检索数据时得到空白记录?
- python - Python 类型提示:如何进行文字范围
- alexa - 模式识别 Alexa 开发者控制台
- terraform - 如何使用 Terraform 为 GCP 中的实例添加私有 IP 输出?
- css - 为什么 Firefox 在元素嵌套级别 2 之后停止渲染嵌套的多列布局,以及如何解决?
- javascript - 如何设置 p5js 按钮对象相对于画布(0.0)的位置?
- google-chrome-extension - chrome.scripting.executeScript - 无法读取未定义的属性“executeScript”
- sql - athena presto - 基于多个嵌套结构的过滤器
- go - HELM:如何在 if-else 条件下将字符串转换为小写/大写?