2017-04-11 90 views
1
#!/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 

直到本節完成。下面,它可能與行和名稱參數不匹配。應該如何改變?bash上的邏輯方法

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 
+1

'如果[「$ line ==」$ name「];'你在'$ line'中缺少''' – iamauser

+0

謝謝,還有一個問題:cat nmap.txt |而讀線;如果[「$ line」==「$ name」];然後主機「$行」;網絡連接; done> ping.txt不工作,也就是說不會給出任何邏輯錯誤,但是,如何糾正邏輯方法? – user1532437

+1

你可能想對[cat'](http://www.iki.fi/era/unix/award.html)這些[無用的用法]做點事情。 – tripleee

回答

1

目前尚不清楚究竟哪裏出了問題,但是這裏有一個重構,可能希望至少將您推向正確的方向。

#!/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是不工作的意見的話表明,邏輯有莫名其妙錯誤的。它目前會檢查每一行是否與原始域名相同,然後在這些情況下反覆查看,這似乎是一件好奇的事情;但只給出代碼和「不起作用」,很難說它真的應該完成什麼。如果你真的想要別的東西,你需要更具體地瞭解你需要的東西。也許你實際上是在尋找像

... 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命令並運行整個管道,而無需輸入文件。如果您確實需要這些文件,在每次運行時將它們覆蓋似乎存在問題 - 可能是爲文件名添加一個唯一的日期戳後綴?