Bash script to extract spf records


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash script to extract spf records
# 1  
Old 05-21-2009
Bash script to extract spf records

Hello

I am trying to generate a script to run on worldwide firewalls.

I need the spf block for large sites like google, etc so I can essentially whitelist google sites for users.

(Google here is just an example...)

Right now I am just testing Bash oneliners to see how I can isolate the data I need.

I want a list of networks with CIDR - this I can feed directly into my whitelist.

My script:

Code:
#!/bin/bash

site=${1} # not necc - just like to have a variable for this

diglist=`dig txt ${site} | grep v=spf1 | cut -d : -f2 | cut -d " " -f1`   

arr01=( `dig txt  $diglist | grep v=spf1 | cut -d \" -f2 | sed s/ip4://g` )




for i in $(seq 1 $((${#arr01[*]} - 2)))
          do 
             echo ${arr01[${i}]}
          done


I am happy to say it more or less works for my purposes.


Quote:
mbubb@mbubb-laptop:/tmp/digGoog ebay.com
66.135.209.192/27
66.135.197.0/27
64.4.240.64/27
64.4.244.64/27
66.211.161.0/25
66.135.213.241

But it is not very good.

I see the recursion there with the dig statements and know there must be a better way. Lots of kludgy cut statements - I should be able to just capture the IP blocks and load them into an array.

With I knew Perl/Python a little better or even awk (keeping in a Bash frame of mind).

How could I fix this?

yrs

Michael
# 2  
Old 05-25-2009
I believe it will help you to know that dig has a "short" mode:
Code:
dig +short txt google.com

produces:
Code:
"v=spf1 include:_netblocks.google.com ~all"

I don't know what your sed command really does, but you can combine it with the cut-quotes command:
Code:
sed 's/"//g; s/ip4://g;'

I'm not sure what the complaint about "recursion" is: If you encounter an "include", you need to "follow it", recursively:
Code:
get_dns_spf1() { 
   dig +short txt "$1" | 
   tr ' ' '\n' |
   while read entry; do 
      case "$entry" in 
         ip4:*)  echo ${entry#*:} ;; 
         include:*) get_dns_spf1 ${entry#*:} ;;
      esac
   done |
   sort -u
   ;
}

This outputs a list. To get the output of the list in an array, just do:
Code:
ips=(`get_dns_spf1 some.site.com`)

Now you can just
Code:
echo ${ips[*]}

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to extract paragraph with globs in it

Hi, Its been a long time since I have used Bash to write a script so am really struggling here. Need the gurus to help me out. uname -a Linux lxserv01 2.6.18-417.el5 i have a text file with blocks of code written in a similar manner ******* BEGIN MESSAGE ******* Station /... (12 Replies)
Discussion started by: dsid
12 Replies

2. Shell Programming and Scripting

Parse xml in shell script and extract records with specific condition

Hi I have xml file with multiple records and would like to extract records from xml with specific condition if specific tag is present extract entire row otherwise skip . <logentry revision="21510"> <author>mantest</author> <date>2015-02-27</date> <QC_ID>334566</QC_ID>... (12 Replies)
Discussion started by: madankumar.t@hp
12 Replies

3. Shell Programming and Scripting

Extract top 20 records from sybase db in shell script

Hi, I am new to advanced scripting. I need to get top 20 records from database (Sybase) with a condition that all Char/varchar columns should have more than 4 characters. I need to do this via shell scripting, I have got half logic (on how to get varchar/char columns and stored it in a... (2 Replies)
Discussion started by: Selva_2507
2 Replies

4. Shell Programming and Scripting

Extract UNIque records from File

Hi, I have a file with 20GB Pipe Delimited file where i have too many duplicate records. I need an awk script to extract the unique records from the file and put it into another file. Kindly help. Thanks, Arun (1 Reply)
Discussion started by: Arun Mishra
1 Replies

5. Shell Programming and Scripting

Extract records from list

Hi Gents, I have a file 1 like this 1 1000 20 2 2000 30 3 1000 40 5 1000 50 And I have other file 1 like 2 1 I would like to get from the file 1 the complete line which are in file 2, the key to compare is the column 2 then output should be. 2 2000 30. I was trying to get it... (5 Replies)
Discussion started by: jiam912
5 Replies

6. Shell Programming and Scripting

how to write bash script that will automatically extract zip file

i'm trying to write a bash script that that will automatically extract zip files after the download. i writed this script #!/bin/bash wget -c https://github.com/RonGokhle/kernel-downloader/zipball/master CURRENDIR=/home/kernel-downloader cd $CURRENDIR rm $CURRENDIR/zipfiles 2>/dev/null ... (2 Replies)
Discussion started by: ron gokhle
2 Replies

7. Shell Programming and Scripting

count and compare no of records in bash shell script.

consider this as a csv file. H,0002,0002,20100218,17.25,P,barani D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 D,1,2,3,4,5,6,7,8,9,10,11 T,5 N i want to read the csv file and count the number of rows that start with D and... (11 Replies)
Discussion started by: barani75
11 Replies

8. UNIX for Dummies Questions & Answers

In BIND 9.3 DNS trying to get past the 256 char limit in SPF TXT records

One way I was told to do was incase strings in quotes. But I was given this option if I can get it to work. Will this work for splitting up SPF records? I am try to make bx.example.com reference spf.eu.***, spfa.eu.***, spfb.eu.***, and spfc.eu.***. spf.eu.example.com 3600 IN TXT "v=spf1... (0 Replies)
Discussion started by: tmanx
0 Replies

9. UNIX for Dummies Questions & Answers

Adding SPF records......please help

Does anybody know who I can set up SPR records? I've created a record and it says that I need to and I logged into the server as the root users via ssh and typed in /var/named (which my host told me to do) but all it said was I really need to add this because Microsoft is blocking emails... (11 Replies)
Discussion started by: thehaapyappy
11 Replies

10. Shell Programming and Scripting

how to extract paragraphs from file in BASH script followed by prefix ! , !! and !!!

I]hi all i am in confusion since last 2 days :( i posted thraed yesterday and some friends did help but still i couldnt get solution to my problem let it be very clear i have a long log file of alkatel switch and i have to seperate the minor major and critical alarms shown by ! , !! and !!!... (6 Replies)
Discussion started by: nabmufti
6 Replies
Login or Register to Ask a Question