首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bash的逻辑方法

bash的逻辑方法
EN

Stack Overflow用户
提问于 2017-04-11 22:02:45
回答 1查看 76关注 0票数 1
代码语言:javascript
复制
#!/bin/bash
echo -n "Enter the domain name > "
read name
dig -t ns "$name" | cut -d ";" -f3 | cut -d ":" -f2| grep ns | cut -f6 > registerfile.txt;
cat registerfile.txt | while read line; do dig axfr "@$line" "$name"; done | cut -d"." -f-4 > nmap.txt

这件事一直到这部分。下面,它可能与行和名称参数不匹配。该如何改变呢?

代码语言:javascript
复制
cat nmap.txt | while read line; do if [ "$line" == "$name" ]; then host "$line"; fi; done > ping.txt 
cat ping.txt | cut -d " " -f4  | while read line; do if  [[ "$line" =~ ^[0-9]+$ ]]; then nmap -sS "$line";fi ;done
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-11 22:49:08

目前还不清楚到底哪里出了问题,但这里有一个重构,希望至少能将您推向正确的方向。

代码语言:javascript
复制
#!/bin/bash
read -p "Enter the domain name > " name
dig +short -t ns "$name" |
tee registerfile.txt |
while read line; do
    dig axfr "@$line" "$name"
done |
cut -d"." -f-4 |
tee nmap.txt |
while read line; do 
    if [ "$line" = "$name" ]; then
         host "$line"
    fi
done > ping.txt 
cut -d " " -f4 ping.txt | 
grep -E '^[0-9]+$' |
xargs -r -n 1 nmap -sS

您在评论中关于if [ "$line" = "$name" ]; then host "$line"; fi不起作用的评论表明,这里的逻辑是错误的。目前,它检查每一行是否与原始域名相同,然后在这些情况下一遍又一遍地查找它,这似乎是一件很奇怪的事情;但只考虑到代码和“不工作”,很难说出它到底要完成什么任务。如果你真的想要别的东西,你需要对你所需要的东西更加具体。也许你实际上是在寻找类似

代码语言:javascript
复制
... tee nmap.txt |
# Extract the lines which contain $name at the end
grep "\.$name\$" |
xargs -n 1 dig +short |
tee ping.txt |
grep -E '^[0-9]+$' ...

多个静态命名文件的使用是反模式的;显然,如果这些文件没有外部用途,只需取出tee命令并运行整个管道,不需要在输出文件之间插入。如果您确实需要这些文件,那么在每次运行时覆盖它们似乎是有问题的--也许在文件名中添加一个唯一的日期戳后缀?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43356950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档