Bash:添加输入前缀以挖掘输出

时间:2016-02-05 06:17:41

标签: bash shell unix

当我这样做时,

$dig axfr @ns2.3u.net weclapp.com

我得到输出结果为,

; <<>> DiG 9.8.1-P1 <<>> axfr @ns2.3u.net weclapp.com
; (1 server found)
;; global options: +cmd
weclapp.com.        300 IN  SOA ns1.3u.net. ops.3u.net. 2015052602 21600 3600 604800 180
weclapp.com.        300 IN  TXT "v=spf1 ip4:185.82.86.236/32 a:smtp.3u.net include:spf.mail.intercom.io include:spf.mailjet.com ?all"
weclapp.com.        300 IN  NS  ns1.3u.net.
weclapp.com.        300 IN  NS  ns2.3u.net.
weclapp.com.        300 IN  MX  100 smtp.3u.net.
weclapp.com.        300 IN  A   185.82.86.245
*.weclapp.com.      300 IN  A   185.82.86.245
mailjet._domainkey.weclapp.com. 300 IN  TXT "k=rsa\; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDhJ5ZuAL8d8+lkcfbALCNOUxjjM5au1PqFA3VWBCDsSRLaG1t3yYal5RFRcIIoRlVJKf4AaKkvxpsMQJD4EFzEd//UUSIvGIXUfGWs/ywv1TnFY3L0d862mCDcvw88L9p9IdZEkVRAeLBnMqJxAeo2TzAtID+ei4CWUbYxLwfAuwIDAQAB"
weclapp.com.        300 IN  SOA ns1.3u.net. ops.3u.net. 2015052602 21600 3600 604800 180
;; Query time: 274 msec
;; SERVER: 83.125.107.34#53(83.125.107.34)
;; WHEN: Fri Feb  5 11:17:36 2016
;; XFR size: 9 records (messages 1, bytes 571)

现在我想为每个输出行添加一个从文件中读取的数字。我做了,

#Input.txt
1,weclapp.com,ns2.3u.net

#test.sh
while IFS=, read -r n d s;do 
      printf "$n,%s\n" $(dig axfr @"$s" "$d")
done < Input.txt

#d is domain , #s is name server ,#n is number from the input file.

当我运行它$sh tesh.sh时,我输出为。

1,;
1,<<>>
1,DiG
1,9.8.1-P1
1,<<>>
1,axfr
1,@ns2.3u.net
1,weclapp.com
1,;
1,(1
1,server
1,found)
1,;;
1,global
1,options:
1,+cmd
1,weclapp.com.
1,300
1,IN
1,SOA
1,ns1.3u.net.
1,ops.3u.net.
1,2015052602
1,21600
1,3600
1,604800
1,180
1,weclapp.com.
1,300
1,IN
1,TXT
1,"v=spf1
1,ip4:185.82.86.236/32
1,a:smtp.3u.net
1,include:spf.mail.intercom.io
1,include:spf.mailjet.com
1,?all"
1,weclapp.com.
1,300
1,IN
1,NS
1,ns1.3u.net.
1,weclapp.com.
1,300
1,IN
1,NS
1,ns2.3u.net.
1,weclapp.com.
1,300
1,IN
1,MX
1,100
1,smtp.3u.net.
1,weclapp.com.
1,300
1,IN
1,A
1,185.82.86.245
1,*.weclapp.com.
1,300
1,IN
1,A
1,185.82.86.245
1,mailjet._domainkey.weclapp.com.
1,300
1,IN
1,TXT
1,"k=rsa\;
1,p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDhJ5ZuAL8d8+lkcfbALCNOUxjjM5au1PqFA3VWBCDsSRLaG1t3yYal5RFRcIIoRlVJKf4AaKkvxpsMQJD4EFzEd//UUSIvGIXUfGWs/ywv1TnFY3L0d862mCDcvw88L9p9IdZEkVRAeLBnMqJxAeo2TzAtID+ei4CWUbYxLwfAuwIDAQAB"
1,weclapp.com.
1,300
1,IN
1,SOA
1,ns1.3u.net.
1,ops.3u.net.
1,2015052602
1,21600
1,3600
1,604800
1,180
1,;;
1,Query
1,time:
1,167
1,msec
1,;;
1,SERVER:
1,83.125.107.34#53(83.125.107.34)
1,;;
1,WHEN:
1,Fri
1,Feb
1,5
1,11:38:51
1,2016
1,;;
1,XFR
1,size:
1,9
1,records
1,(messages
1,1,
1,bytes
1,571)

必须是,

1,; <<>> DiG 9.8.1-P1 <<>> axfr @ns2.3u.net weclapp.com
1,; (1 server found)
1,;; global options: +cmd
1,weclapp.com.      300 IN  SOA ns1.3u.net. ops.3u.net. 2015052602 21600 3600 604800 180
1,......

有人可以建议如何在以下dig命令输出中添加input number (n )吗?

1 个答案:

答案 0 :(得分:1)

您可以像这样修改脚本:

while IFS=, read -r n d s;do
   dig axfr @"$s" "$d" | sed "s/^\(.\)/$n,\1/"
done < Input.txt

sed会跳过在空行上添加$n,,但会在所有其他行上添加前缀$n,

<强>输出:

1,; <<>> DiG 9.8.3-P1 <<>> axfr @ns2.3u.net weclapp.com
1,; (1 server found)
1,;; global options: +cmd
1,weclapp.com.      300 IN  SOA ns1.3u.net. ops.3u.net. 2015052602 21600 3600 604800 180
1,weclapp.com.      300 IN  TXT "v=spf1 ip4:185.82.86.236/32 a:smtp.3u.net include:spf.mail.intercom.io include:spf.mailjet.com ?all"
1,weclapp.com.      300 IN  NS  ns1.3u.net.
1,weclapp.com.      300 IN  NS  ns2.3u.net.
1,weclapp.com.      300 IN  MX  100 smtp.3u.net.
1,weclapp.com.      300 IN  A   185.82.86.245
1,*.weclapp.com.        300 IN  A   185.82.86.245
1,mailjet._domainkey.weclapp.com.   300 IN  TXT "k=rsa\; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDhJ5ZuAL8d8+lkcfbALCNOUxjjM5au1PqFA3VWBCDsSRLaG1t3yYal5RFRcIIoRlVJKf4AaKkvxpsMQJD4EFzEd//UUSIvGIXUfGWs/ywv1TnFY3L0d862mCDcvw88L9p9IdZEkVRAeLBnMqJxAeo2TzAtID+ei4CWUbYxLwfAuwIDAQAB"
1,weclapp.com.      300 IN  SOA ns1.3u.net. ops.3u.net. 2015052602 21600 3600 604800 180
1,;; Query time: 196 msec
1,;; SERVER: 83.125.107.34#53(83.125.107.34)
1,;; WHEN: Fri Feb  5 01:30:53 2016
1,;; XFR size: 9 records (messages 1, bytes 571)