Arun
Arun

Reputation: 1220

Bash : Adding Input Prefix To Dig Output

when I do ,

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

I get output result as,

; <<>> 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)

Now i would like to add each output line with a number read from a file. I did,

#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.

When i run it $sh tesh.sh,I get output as.

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)

It has to be like,

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,......

Can some one suggest on how to achieve adding input number (n ) to the following dig command output ?

Upvotes: 1

Views: 146

Answers (1)

anubhava
anubhava

Reputation: 786261

You can modify your script like this:

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

sed will skip adding $n, on empty lines but will prefix $n, on all other lines.

Output:

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)

Upvotes: 1

Related Questions