知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
不懂遞歸不算是合格的程序員
遞歸調用是一種特殊的嵌套調用,是一個函數在它的函數體內調用它自身稱為遞歸調用。這種函數稱為遞歸函數。
#!/bin/bash ######################################## # Author: Neo <netiller@msn.com> # Home : http://netkiler.github.io # Project: https://github.com/oscm/shell ######################################## domain=$1 ######################################## function include(){ txt=$1 for host in $(echo $txt | egrep -o "include:(.+) ") do txt=$(dig $(echo $host | cut -d":" -f2) txt | grep "v=spf1") echo $txt; if [ "$(echo $txt | grep "include")" ]; then include "$txt" fi done } function main(){ spf=$(dig ${domain} txt | grep "v=spf1") echo $spf if [ "$(echo $spf | grep "include")" ]; then include "$spf" fi } main $domain
運行上面的程序
$ bash spf.sh 163.com 163.com. 6878 IN TXT "v=spf1 include:spf.163.com -all" spf.163.com. 16991 IN TXT "v=spf1 include:a.spf.163.com include:b.spf.163.com include:c.spf.163.com include:d.spf.163.com -all" a.spf.163.com. 8001 IN TXT "v=spf1 ip4:220.181.12.0/22 ip4:220.181.31.0/24 ip4:123.125.50.0/24 ip4:220.181.72.0/24 ip4:123.58.178.0/24 ip4:123.58.177.0/24 ip4:113.108.225.0/24 ip4:218.107.63.0/24 ip4:123.58.189.128/25 -all" b.spf.163.com. 10131 IN TXT "v=spf1 ip4:176.34.21.58 ip4:176.34.53.178 ip4:121.195.178.48/28 ip4:223.252.213.0/24 -all" c.spf.163.com. 17199 IN TXT "v=spf1 ip4:223.252.206.0/24 ip4:43.230.90.0/27 -all" d.spf.163.com. 17615 IN TXT "v=spf1 ip4:123.126.65.0/24 ip4:106.2.88.0/24 ip4:220.181.97.0/24 ip4:180.150.142.123 ip4:180.150.142.124 ip4:180.150.154.88 ip4:180.150.154.92 ip4:180.150.154.93 ip4:103.251.128.69 -all"