asn_ipfire.sh 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. #!/usr/bin/env bash
  2. #######################################################################
  3. # IPFire network object creator for IPv4 addresses based on ASN information
  4. # Creates 'customnetworks' objects in /var/ipfire/fwhosts/customnetworks
  5. # Creates 'customgroups' objects in /var/ipfire/fwhosts/customgroups
  6. #######################################################################
  7. #######################################################################
  8. revision="asn_ipfire.sh v0.7.13" # do not comment out
  9. # Last updated: May 13 2022 by maloe
  10. # Author: Mike Kuketz, maloe
  11. # Visit: www.kuketz-blog.de
  12. # Source: notabug.org/maloe/ASN_IPFire_Script
  13. # License: GNU GPL v3
  14. # Copyright 2017-2022 Mike Kuketz, maloe
  15. #######################################################################
  16. #######################################################################
  17. # Constants, Filenames, Enable/Disable Sources
  18. # Config file (if exists, it overwrites the default values specified in this header)
  19. configfile=asn_script.conf
  20. # Use download tool wget or curl
  21. downloadtool=wget # default
  22. #downloadtool=curl # alterantive (if existent)
  23. timeout=30 # timeout in seconds for availability check (-c|--check)
  24. # Path to IPFire customnetworks|customgroups
  25. customnetworks=/var/ipfire/fwhosts/customnetworks
  26. customgroups=/var/ipfire/fwhosts/customgroups
  27. # Remark for IPFire customnetworks|customgroups. This is used to identify entries made by asn_ipfire.sh.
  28. auto_remark="entry by asn_ipfire.sh"
  29. # Define iptables path for iptable/afwall output file
  30. iptables_path="/sbin/iptables"
  31. afwall_path="/system/bin/iptables"
  32. # Define custom output format
  33. outputline="%network%" # special variables: %network%, %company%
  34. # Output file
  35. output_file="asn_result.lst" # default output file
  36. # Deprecated output files, only used if output_file is empty
  37. file_network="network_list.txt" # deprecated output file for network consolidated
  38. file_asn="asn_list.txt" # deprecated output file for ASNs only
  39. file_iptable="iptable_rules.txt" # deprecated output file in iptable format
  40. file_afwall="afwall_rules.txt" # deprecated output file in afwall format
  41. temppath="." # path to store temporary source file (default is the script folder ".")
  42. temp1="asn_cidrreport.tmp" # Define temp file
  43. temp2="asn_bglooking.tmp" # Define temp file
  44. # Local files can be used as ASN and/or network sources. To be activated by enabling "gather_ASN0" and/or "gather_NET0" into following arrays.
  45. local_asn_file="local_asn.list" # Line format: 'ASN Company' or vice versa (e.g. 'AS1234 CompanyA' or 'CompanyA AS1234')
  46. local_net_file="local_net.list" # Line format: 'network ASN' or vice versa (e.g. '1.2.3.4/24 AS5678' or 'AS5678 1.2.3.4/24')
  47. # Enable/disable ASN sources: Remove leading hashes (#) to enable ASN_sources.
  48. getASNfromCOMPANY=( \
  49. ASN_local \ # local source (local_asn_file)
  50. ASN_cidrreport \ # cidr-report.org
  51. # ASN_bgplookingglass \ # bgplookingglass.com
  52. # ASN_bgpview \ # bgpview.io (white spaces in company names not supported)
  53. ##ASN_bgphenet \ # bgp.he.net (not supported due to js requirement)
  54. )
  55. # Enable/disable network sources: Remove leading hash (#) to enable NET_sources.
  56. getNETfromASN=( \
  57. NET_local \ # local source (local_net_file)
  58. NET_ripe \ # stat.ripe.net
  59. # NET_ipinfo \ # ipinfo.io
  60. # NET_bgpview \ # bgpview.io
  61. # NET_radb \ # whois.radb.net # use only if you know what you are doing. query results may be wrong!
  62. )
  63. #######################################################################
  64. # Include configuration file
  65. thisfolder=$(dirname $(readlink -f $0))
  66. if [[ -f ${thisfolder}/${configfile} ]]; then
  67. . ${thisfolder}/${configfile}
  68. fi
  69. #######################################################################
  70. # Gather-Functions: add further sources here and activate them in above arrays getASNfromCOMPANY() and getNETfromASN()
  71. # ASN sources: function must return a list of ASNs
  72. ASN_local() # Get ASN from local file
  73. {
  74. if [[ -f $local_asn_file ]]; then
  75. echo "---[Using local ASN Source file ($local_asn_file)]---"
  76. cname=`echo $1 | sed 's/~/ /g; s/*/.*/g'` # Replace ~ with space
  77. asn_array=`cat $local_asn_file | grep -i "$cname" | grep -Eo 'AS[0-9]+'`
  78. fi;
  79. }
  80. ASN_cidrreport() # Get ASN from cidr-report.org
  81. {
  82. if [[ $dl != "local" ]]; then # wget or curl available?
  83. if [[ ! -f $temp1 ]] && [[ ${#company_array[@]} -gt 1 || $keeptemp ]]; then # Temp file not exist and more than one company names or option keeptemp is enabled
  84. touch $temp1 2> /dev/null # Temp file writable?
  85. if [[ -w $temp1 ]]; then # Write temp file
  86. echo "---[Downloading ASN Source List from www.cidr-report.org]---"
  87. $dl "https://www.cidr-report.org/as2.0/autnums.html" | grep -Eo '>AS[0-9]+.*' | sed 's/^>//; s/[ ]*<\/a>[ ]*/ /' >> $temp1
  88. fi
  89. fi
  90. cname=`echo $1 | sed 's/~/ /g; s/*/.*/g'` # Replace ~ with space and * with expression .*
  91. if [[ -f $temp1 ]]; then # Read from temp file
  92. if [[ $verbose ]]; then echo "---[Using local (temporary) ASN Source List ($temp1)]---"; fi
  93. asn_array=`cat $temp1 | grep -i "$cname" | grep -Eo '^AS[0-9]+'`
  94. else # Read from source
  95. echo "---[Downloading ASN Source List from www.cidr-report.org]---"
  96. asn_array=`$dl "https://www.cidr-report.org/as2.0/autnums.html" | grep -i "$cname" | grep -Eo '>AS[0-9]+' | grep -Eo 'AS[0-9]+'`
  97. fi;
  98. fi
  99. }
  100. ASN_bgplookingglass() # Get ASN from bgplookingglass.com
  101. {
  102. if [[ $dl != "local" ]]; then # wget or curl available?
  103. if [[ ! -f $temp2 ]] && [[ ${#company_array[@]} -gt 1 || $keeptemp ]]; then # Temp file not exist and more than one company names or option keeptemp is enabled
  104. touch $temp2 2> /dev/null # Check if writable?
  105. if [[ -w $temp2 ]]; then
  106. echo "---[Downloading ASN Source List from www.bgplookingglass.com]---"
  107. $dl "http://www.bgplookingglass.com/list-of-autonomous-system-numbers" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\'$'\n/g; s/^[[:space:]]*<pre>//; s/[ ]\+/ /g' >> $temp2
  108. $dl "http://www.bgplookingglass.com/list-of-autonomous-system-numbers-2" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\'$'\n/g; s/^[[:space:]]*<pre>//; s/[ ]\+/ /g' >> $temp2
  109. $dl "http://www.bgplookingglass.com/4-byte-asn-names-list" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\'$'\n/g; s/^[[:space:]]*<pre>//; s/[ ]\+/ /g' >> $temp2
  110. fi
  111. fi
  112. cname=`echo $1 | sed 's/~/ /g; s/*/.*/g'` # Replace ~ with space and * with expression .*
  113. if [[ -f $temp2 ]]; then # Read from temp file
  114. if [[ $verbose ]]; then echo "---[Using local (temporary) ASN Source List ($temp2)]---"; fi
  115. asn_array=`cat $temp2 | grep -i "$cname" | grep -Eo '^AS[0-9]+'`
  116. else # Temp file not writable
  117. echo "---[Downloading ASN Source List from www.bgplookingglass.com]---"
  118. asn_array=(`$dl "http://www.bgplookingglass.com/list-of-autonomous-system-numbers" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\'$'\n/g' | grep -i "$cname" | sed 's/^[[:space:]]*<pre>//' | grep -Eo '^AS[0-9]+'`)
  119. asn_array=(${asn_array[@]} `$dl "http://www.bgplookingglass.com/list-of-autonomous-system-numbers-2" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\'$'\n/g' | grep -i "$cname" | sed 's/^[[:space:]]*<pre>//' | grep -Eo '^AS[0-9]+'`)
  120. asn_array=(${asn_array[@]} `$dl "http://www.bgplookingglass.com/4-byte-asn-names-list" | sed -n '/AS[0-9]/ p' | sed 's/<br \/>/\'$'\n/g' | grep -i "$cname" | sed 's/^[[:space:]]*<pre>//' | grep -Eo '^AS[0-9]+'`)
  121. fi
  122. fi
  123. }
  124. ASN_bgpview() # Get ASN from bgpview.io
  125. {
  126. if [[ $dl != "local" ]]; then # wget or curl available?
  127. cname=`echo $1 | sed 's/~/ /g; s/+/%2B/g'` # Replace ~ with space and "+" with %2B
  128. asn_array=(`$dl "https://bgpview.io/search/$cname" | grep -Eo 'AS[0-9]+' | uniq`)
  129. fi
  130. }
  131. ASN_bgphenet() # Get ASN from bgp.he.net
  132. {
  133. if [[ $dl != "local" ]]; then # wget or curl available?
  134. cname=`echo $1 | sed 's/~/ /g; s/+/%2B/g'` # Replace ~ with space and "+" with %2B
  135. asn_array=(`$dl "https://bgp.he.net/search?search%5Bsearch%5D=$cname&commit=Search" | grep -Eo 'AS[0-9]+' | uniq`)
  136. fi
  137. }
  138. # Network sources: function must return a list of CIDR networks
  139. NET_local() # Get networks from local file, sorting
  140. {
  141. if [[ -f $local_net_file ]]; then
  142. cat $local_net_file | grep -i "$1" | grep -Eo '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/)(3[0-2]|[1-2][0-9]|0?[1-2]?[1-9])' | sed -E 's/^[0]{0,2}([0-9])/\1/g; s/\.[0]{0,2}([0-9])/.\1/g; s/\/[0]?/\//g' | sort -Vu
  143. fi
  144. }
  145. NET_ripe() # Get networks from stat.ripe.net, sorting
  146. {
  147. if [[ $dl != "local" ]]; then # wget or curl available?
  148. $dl "https://stat.ripe.net/data/announced-prefixes/data.json?resource=$1" | grep -Eo '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/)(3[0-2]|[1-2][0-9]|0?[1-2]?[1-9])' | sed -E 's/^[0]{0,2}([0-9])/\1/g; s/\.[0]{0,2}([0-9])/.\1/g; s/\/[0]?/\//g' | sort -Vu
  149. fi
  150. }
  151. NET_ipinfo() # Get networks from ipinfo.io, sorting
  152. {
  153. if [[ $dl != "local" ]]; then # wget or curl available?
  154. $dl "https://ipinfo.io/$1" | grep -Eo '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/)(3[0-2]|[1-2][0-9]|0?[1-2]?[1-9])' | sed -E 's/^[0]{0,2}([0-9])/\1/g; s/\.[0]{0,2}([0-9])/.\1/g; s/\/[0]?/\//g' | sort -Vu
  155. fi
  156. }
  157. NET_bgpview() # Get networks from bgpview.io, sorting
  158. {
  159. if [[ $dl != "local" ]]; then # wget or curl available?
  160. $dl "https://bgpview.io/asn/$1#prefixes-v4" | grep -Eo '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/)(3[0-2]|[1-2][0-9]|0?[1-2]?[1-9])' | sed -E 's/^[0]{0,2}([0-9])/\1/g; s/\.[0]{0,2}([0-9])/.\1/g; s/\/[0]?/\//g' | sort -Vu
  161. fi
  162. }
  163. NET_radb() # Get networks from whois, sorting
  164. {
  165. if [[ -x `which whois 2>/dev/null` ]]; then # whois available?
  166. whois -h whois.radb.net -i origin $1 | grep -w "route:" | grep -Eo '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/)(3[0-2]|[1-2][0-9]|0?[1-2]?[1-9])' | sed -E 's/^[0]{0,2}([0-9])/\1/g; s/\.[0]{0,2}([0-9])/.\1/g; s/\/[0]?/\//g' | sort -Vu
  167. fi
  168. }
  169. #######################################################################
  170. # NO NEED TO EDIT ANYTHING BELOW
  171. #######################################################################
  172. # Functions
  173. # Function: check for ipfire system and existence of wget or curl
  174. chkSystem()
  175. {
  176. if [[ -d /var/ipfire ]] && [[ -f /etc/init.d/firewall ]]; then # Running on ipfire system?
  177. is_ipfire=1
  178. else
  179. is_ipfire=""
  180. fi
  181. dl=local
  182. if [[ -x `which wget 2>/dev/null` ]]; then
  183. dl="wget --quiet --max-redirect 1 -O -" # Use wget if existent
  184. fi
  185. if [[ -x `which curl 2>/dev/null` ]]; then
  186. if [[ $dl == "local" ]] || [[ "$downloadtool" == "curl" ]]; then
  187. dl="curl --silent" # Use curl if existent and wget is missing
  188. dl="curl --silent --location" # Use curl if existent and wget is missing
  189. fi
  190. fi
  191. if [[ $dl == "local" ]]; then
  192. echo "Warning: did not found wget nor curl. Only local sources usable." # Neither wget nor curl was found
  193. fi
  194. }
  195. # Function: check source reachability
  196. chkSource() {
  197. if [[ $dl == "wget --quiet --max-redirect 0 -O -" ]]; then # Wget
  198. for s in ${getASNfromCOMPANY[@]}; do # Loop through ASN sources
  199. echo -n "---[Checking source $s]---"
  200. case $s in
  201. ASN_local) if [ -f "$local_asn_file" ]; then echo -n " (OK)"; else echo -n " (n/a)"; fi ;;
  202. ASN_cidrreport) wget --spider --quiet --connect-timeout=$timeout --tries=1 --max-redirect 0 "https://www.cidr-report.org/as2.0/autnums.html" ;;
  203. ASN_bgplookingglass) wget --spider --quiet --connect-timeout=$timeout --tries=1 --max-redirect 1 "http://www.bgplookingglass.com/list-of-autonomous-system-numbers" ;; # Check first site only
  204. ASN_bgpview) wget --spider --quiet --connect-timeout=$timeout --tries=1 --max-redirect 0 "https://bgpview.io/search/as1" ;;
  205. ASN_bgphenet) wget --spider --quiet --connect-timeout=$timeout --tries=1 --max-redirect 0 "https://bgp.he.net/as1" ;;
  206. esac
  207. if [ $? -ne 0 ]; then echo -e "\nAbort: couldn't reach (wget) selected source $s"; exit 4; fi
  208. echo
  209. done
  210. for s in ${getNETfromASN[@]}; do # Loop through ASN sources
  211. echo -n "---[Checking source $s]---"
  212. case $s in
  213. NET_local) if [ -f "$local_net_file" ]; then echo -n " (OK)"; else echo -n " (n/a)"; fi ;;
  214. NET_ripe) wget --spider --quiet --connect-timeout=$timeout --tries=1 --max-redirect 0 "https://stat.ripe.net/data/announced-prefixes/data.json?preferred_version=1.1&resource=0" ;;
  215. NET_ipinfo) wget --spider --quiet --connect-timeout=$timeout --tries=1 --max-redirect 0 "https://ipinfo.io/as1" ;;
  216. NET_bgpview) wget --spider --quiet --connect-timeout=$timeout --tries=1 --max-redirect 0 "https://bgpview.io/search/" ;;
  217. esac
  218. if [ $? -ne 0 ]; then echo -e "\nAbort: couldn't reach (curl) selected source $s"; exit 4; fi
  219. echo
  220. done
  221. elif [[ $dl == "curl --silent" ]]; then # Curl
  222. for s in ${getASNfromCOMPANY[@]}; do # Loop through ASN sources
  223. echo -n "---[Checking source $s]---"
  224. case $s in
  225. ASN_local) if [ -f "$local_asn_file" ]; then echo -n " (OK)"; else echo -n " (n/a)"; fi ;;
  226. ASN_cidrreport) curl --silent --head --connect-timeout $timeout "https://www.cidr-report.org/as2.0/autnums.html" | head -1 | grep '200' >/dev/null ;;
  227. ASN_bgplookingglass) curl --silent --head --connect-timeout $timeout --location "http://www.bgplookingglass.com/list-of-autonomous-system-numbers" | grep -E 'HTTP.*200' >/dev/null ;; # Check first site only
  228. ASN_bgpview) curl --silent --head --connect-timeout $timeout "https://bgpview.io/search/as1" | head -1 | grep '200' >/dev/null ;;
  229. ASN_bgphenet) curl --silent --head --connect-timeout $timeout "https://bgp.he.net/as1" | head -1 | grep '200' >/dev/null ;;
  230. esac
  231. if [ $? -ne 0 ]; then echo -e "\nAbort: couldn't reach selected source $s"; exit 4; fi
  232. echo
  233. done
  234. for s in ${getNETfromASN[@]}; do # Loop through ASN sources
  235. echo -n "---[Checking source $s]---"
  236. case $s in
  237. NET_local) if [ -f "$local_net_file" ]; then echo -n " (OK)"; else echo -n " (n/a)"; fi ;;
  238. NET_ripe) curl --silent --head --connect-timeout $timeout "https://stat.ripe.net/data/announced-prefixes/data.json?resource=0" | head -1 | grep '200' >/dev/null ;;
  239. NET_ipinfo) curl --silent --head --connect-timeout $timeout "https://ipinfo.io/as1" | head -1 | grep '200' >/dev/null ;;
  240. esac
  241. if [ $? -ne 0 ]; then echo -e "\nAbort: couldn't reach selected source $s"; exit 4; fi
  242. echo
  243. done
  244. fi
  245. }
  246. # Function: get network mask
  247. cdr2mask()
  248. {
  249. # Number of args to shift, 255..255, first non-255 byte, zeroes
  250. set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
  251. [ $1 -gt 1 ] && shift $1 || shift
  252. echo ${1-0}.${2-0}.${3-0}.${4-0}
  253. }
  254. # Functions: get decimal IP values
  255. get_firstIP() { echo ${1/\//.} | awk -F"." '{ printf "%.0f", $1*2^24+$2*2^16+$3*2^8+$4 }'; } # First IP of network
  256. get_IPrange() { echo $1 | awk -F"/" '{ printf "%.0f", 2^(32-$2)}'; } # IP range of network
  257. get_lastIP() { echo ${1/\//.} | awk -F"." '{ printf "%.0f", $1*2^24+$2*2^16+$3*2^8+$4+2^(32-$5)}'; } # Last IP +1 of network
  258. # Function: transform decimal IP into dot noted IP
  259. dec2ip() { echo $1 | awk '{ printf "%i.%i.%i.%i", $1/(2^24), $1%(2^24)/(2^16), $1%(2^24)%(2^16)/(2^8), $1%(2^24)%(2^16)%(2^8)}'; }
  260. # Function: remove redundant networks
  261. rm_redundantIP() {
  262. declare -a array1=("${!1}") # Put $1 into new array
  263. declare -a array2=() # Create second array
  264. declare maxIP=0 # Initial IP for comparison
  265. declare n=0 # Counter for array
  266. for net in ${array1[@]}; do
  267. lastIP=`get_lastIP $net` # Get last IP(+1) of actual network
  268. if [[ `echo $lastIP $maxIP | awk '$1>$2 {printf 1}'` ]]; then # Comparing big integer. Only keep network if last IP is not covered by previous network
  269. array2[$n]=$net # Write actual network into second array
  270. maxIP=$lastIP # Update maximum IP(+1)
  271. ((n++))
  272. fi
  273. done
  274. for net in ${array2[@]}; do # Return result
  275. if [ $net ]; then echo ${net}; fi # Skip empty lines
  276. done
  277. }
  278. # Function: consolidate adjacent networks
  279. rm_adjacentIP() {
  280. declare -a array1=("${!1}") # Put $1 into new array1
  281. declare -a array2=() # Create working array2
  282. declare n=0 # Counter for array2
  283. declare d=0 # Initial counter for adjacents
  284. declare range=0 # IP range
  285. declare prefirstIP=0 # Get decimal first IP from actual network
  286. declare prenetmask=0 # Get decimal IP range from actual network
  287. declare prelastIP=-1 # Get decimal last IP(+1) from actual network
  288. for net in ${array1[@]}; do # Loop through network list
  289. firstIP=`get_firstIP $net` # Get decimal first IP from actual network
  290. netmask=`get_IPrange $net` # Get decimal IP range from actual network
  291. lastIP=`get_lastIP $net` # Get decimal last IP(+1) from actual network
  292. range=`echo $prenetmask $netmask | awk '{printf "%d", $1+$2;}'`
  293. # If all adjecent consolidation requirements are given
  294. if [[ `echo $firstIP $prelastIP $range $prefirstIP | awk '($1==$2) && (log($3)/log(2)==int(log($3)/log(2))) && ($4%$3==0) {printf 1}'` ]]; then
  295. suffix=`echo $range | awk '{printf "%i", 32-log($1)/log(2);}'`
  296. array2[(n-1)]=`dec2ip $prefirstIP`"/"$suffix
  297. prelastIP=$lastIP
  298. prenetmask=$range
  299. d=1
  300. else # No changes
  301. array2[$n]=$net
  302. prelastIP=$lastIP
  303. prefirstIP=$firstIP
  304. prenetmask=$netmask
  305. ((n++))
  306. fi
  307. done
  308. if [[ d -eq 1 ]]; then # If changes done, then...
  309. d=0
  310. rm_adjacentIP array2[@] # ... next Iteration
  311. else
  312. for net in ${array2[@]} ; do # Return result
  313. if [ $net ]; then echo ${net}; fi # Skip empty lines
  314. done
  315. fi
  316. }
  317. # Function: print statistics
  318. show_stats() { # Requires arguments: asn_array net_array, company
  319. declare -a asn_array=("${!1}") # Put $1 (asn_list) into new array
  320. declare -a net_array=("${!2}") # Put $2 (net_list) into new array
  321. declare countIP=0 # Counter for IP
  322. for net in ${net_array[@]}; do
  323. netmask=`get_IPrange $net` # Get decimal IP range from actual network
  324. countIP=`echo $countIP $netmask | awk '{printf "%.0f", $1+$2}'` # Count IP
  325. done
  326. countIP=`printf "%'i\n" $countIP` # Point separated format
  327. echo "${#net_array[@]} networks with $countIP IPs found in ${#asn_array[@]} ASNs for $3"
  328. }
  329. #######################################################################
  330. # Main procedures
  331. collectNetworks() {
  332. for company in ${company_array[@]}; do
  333. # Get all company ASNs
  334. declare asn_array=()
  335. declare asn_list=()
  336. prnt_company=`echo $company | sed 's/~/ /g; s/,//g'` # Printable company name with space (and no commas)
  337. echo "---[Get all '$prnt_company' ASNs]---"
  338. for asn_gather in ${getASNfromCOMPANY[@]}; do # Loop through ASN sources
  339. $asn_gather $company
  340. asn_list=(`echo ${asn_list[@]} ${asn_array[@]}`) # Append to list
  341. done
  342. output=`echo "### Company: ${prnt_company} ###" | sed 's/ /_/g'`" "
  343. if [ ! $asn_list ]; then
  344. echo "---[No ASN found for $prnt_company]---"
  345. elif [ "$1" == "--asn" ]; then
  346. for asn in ${asn_list[@]}; do
  347. output+="$asn " # Create output text
  348. done
  349. result_array=(`echo ${result_array[@]} ${output}`)
  350. else
  351. asn_list=(`echo ${asn_list[@]} | sed 's/ /\'$'\n/g' | sort -Vu`) # Linebreaking and Sorting # Adjusted for FreeBSD
  352. declare net_array=()
  353. declare net_list=()
  354. for asn in ${asn_list[@]}; do
  355. # Store networks from ASN in file
  356. echo "---[Get $prnt_company networks for $asn]---"
  357. for net_gather in ${getNETfromASN[@]}; do # Loop through NET webservices
  358. net_array=(`$net_gather $asn`)
  359. net_list=(`echo ${net_list[@]} ${net_array[@]} | sed 's/ /\'$'\n/g' | sort -Vu`) # Append to list, sorting # Adjusted for FreeBSD
  360. done
  361. done
  362. if [ ! $net_list ]; then
  363. echo "---[No networks found for $prnt_company]---"
  364. else
  365. # Consolidate adjacent and overlapping netblocks
  366. before=${#net_list[@]} # Number of network entries before consolidate
  367. tbefore="`date +%s`"
  368. if [[ $verbose ]]; then echo " $(show_stats asn_list[@] net_list[@] $company)"; fi
  369. if [ ! $raw ]; then
  370. echo "---[Remove adjacent and overlapping netblocks]---"
  371. net_list=(`rm_redundantIP net_list[@]`) # Remove redundant networks
  372. net_list=(`rm_adjacentIP net_list[@]`) # Consolidate adjacent networks
  373. fi
  374. after=${#net_list[@]} # Number of network entries after consolidate
  375. tafter="`date +%s`"
  376. if [[ $verbose ]]; then echo " $((before - after)) of $before networks removed in $((tafter - tbefore))s"; fi
  377. # Write objects to result array
  378. echo "---[Creating objects for $prnt_company networks]---"
  379. if [[ $verbose ]] && [[ "$1" == "--network" ]]; then
  380. output+=`echo "### $(show_stats asn_list[@] net_list[@] $company) ###" | sed 's/ /_/g'`" "
  381. fi
  382. for net in ${net_list[@]}; do
  383. output+="$net " # Create output text
  384. done
  385. result_array=(`echo ${result_array[@]} ${output}`)
  386. if [[ $verbose ]]; then echo " $(show_stats asn_list[@] net_list[@] $company)"; fi
  387. fi
  388. fi
  389. done
  390. # remove temp files
  391. if [[ ! $keeptemp ]]; then
  392. echo "---[Removing temporary source files]---"
  393. if [[ -f $temp1 ]]; then rm $temp1; fi
  394. if [[ -f $temp2 ]]; then rm $temp2; fi
  395. fi
  396. }
  397. addNetworks() { # Write result to ipfire files
  398. # Get highest number from existing objects in [customnetworks|customgroups]
  399. if [[ -w $customnetworks ]]; then
  400. network_object_number=$(cat $customnetworks | cut -f1 -d',' | awk '{for(i=1;i<=NF;i++) if($i>maxval) maxval=$i;}; END { print maxval;}')
  401. else
  402. echo -e "File $customnetworks not found or write protected.\nCheck your IPFire installation."
  403. exit 0
  404. fi
  405. if [[ -w $customgroups ]]; then
  406. group_object_number=$(cat $customgroups | cut -f1 -d',' | awk '{for(i=1;i<=NF;i++) if($i>maxval) maxval=$i;}; END { print maxval;}')
  407. else
  408. echo -e "File $customgroups not found or write protected.\nCheck your IPFire installation."
  409. exit 0
  410. fi
  411. # Increase counter
  412. ((network_object_number++))
  413. ((group_object_number++))
  414. for net in ${result_array[@]}; do
  415. if [[ ${net:0:1} == "#" ]]; then
  416. prnt_company=`echo $net | sed 's/###_Company:_\(.*\)_###/\1/' | sed 's/_/ /g'` # Get company name
  417. company=$prnt_company
  418. counter=1
  419. else
  420. # Separate IP and netmask
  421. ip=${net%/*}
  422. if [ "$ip" != "0.0.0.0" ]; then # Double check for invalid entry
  423. netmask=${net#*/}
  424. if [ "$netmask" == "32" ]; then echo "WARNING: Found single IP (${company}: ${ip}). This may cause an invalid entry in ipfire!"; fi # Double check for invalid entry
  425. # Write new objects to files [customnetworks|customgroups]
  426. ipf_company=`echo $prnt_company | sed 's/\(.*\)/\U\1/'` # Uppercase company name
  427. printf "$network_object_number,$company-Network Nr.$counter,$ip,$(cdr2mask $netmask),$auto_remark\n" >> $customnetworks
  428. printf "$group_object_number,$ipf_company,$auto_remark,$company-Network Nr.$counter,Custom Network\n" >> $customgroups
  429. # Increase counter
  430. ((network_object_number++))
  431. ((group_object_number++))
  432. ((counter++))
  433. fi
  434. fi
  435. done
  436. echo "---[Results written to $output_file]---" # Resultfile info
  437. }
  438. outResults() {
  439. case "$1" in # Output result to file
  440. --asn) {
  441. for asn in ${result_array[@]} ; do
  442. echo ${asn} | sed 's/_/ /g' >> "$output_file"
  443. done
  444. };;
  445. --network) {
  446. for net in ${result_array[@]}; do
  447. echo ${net} | sed 's/_/ /g' >> "$output_file"
  448. done
  449. };;
  450. --custom) {
  451. for net in ${result_array[@]}; do
  452. if [[ ${net:0:1} == "#" ]]; then
  453. echo "$net" | sed 's/_/ /g' >> "$output_file"
  454. prnt_company=`echo $net | sed 's/###_Company:_\(.*\)_###/\1/' | sed 's/_/ /g'` # Get company name
  455. number=1
  456. else
  457. echo -e "$outputline" | sed "s@%network%@$net@g" | sed "s@%company%@$prnt_company@g" | sed "s@%number%@$number@g" >> "$output_file"
  458. ((number++))
  459. fi
  460. done
  461. };;
  462. --iptable) {
  463. for net in ${result_array[@]}; do
  464. if [[ ${net:0:1} == "#" ]]; then
  465. echo "$net" | sed 's/_/ /g' >> "$output_file"
  466. else
  467. echo "$iptables_path -A OUTPUT -d $net -j REJECT" >> "$output_file"
  468. fi
  469. done
  470. };;
  471. --afwall) {
  472. for net in ${result_array[@]}; do
  473. if [[ ${net:0:1} == "#" ]]; then
  474. echo "$net" | sed 's/_/ /g' >> "$output_file"
  475. else
  476. echo "$afwall_path -A \"afwall\" -d $net -j REJECT" >> "$output_file"
  477. fi
  478. done
  479. };;
  480. *) { # Default ipfire mode
  481. echo "WARNING: unknown mode. Nothing done."
  482. };;
  483. esac
  484. }
  485. cleanupNetworks() { # Remove entries from ipfire files
  486. for ipfire_file in $customnetworks $customgroups; do
  487. if [[ -w $ipfire_file ]]; then
  488. if [[ $backup ]]; then
  489. if [[ $verbose ]]; then echo "---[Backing up $ipfire_file.bak ]---"; fi
  490. cp -f $ipfire_file $ipfire_file.bak # Create ipfire backup files
  491. fi
  492. if [[ ${company_array[0]} == "ALL" ]]; then # Remove all entries made by asn_ipfire.sh
  493. echo "---[Removing all objects from $ipfire_file ]---"
  494. sed -i "/,$auto_remark/Id" $ipfire_file;
  495. else
  496. for company in ${company_array[@]}; do
  497. prnt_company=`echo $company | sed 's/~/ /g; s/,//g; s/\(.*\)/\U\1/'` # Company name with space and "+"
  498. echo "---[Removing $prnt_company objects from $ipfire_file ]---"
  499. sed -i "/$prnt_company.*$auto_remark/Id" $ipfire_file; # Remove company entries made by asn_ipfire.sh
  500. done
  501. fi
  502. elif [[ -f $ipfire_file ]]; then
  503. echo -e "File $ipfire_file write protected.\nCheck your IPFire installation."
  504. fi
  505. done
  506. }
  507. removeBackup() { # Remove ipfire backup files
  508. for ipfire_file in $customnetworks $customgroups; do
  509. if [[ -w $ipfire_file.bak ]]; then
  510. if [[ $verbose ]]; then echo "---[Removing backup $ipfire_file.bak ]---"; fi
  511. rm -f $ipfire_file.bak
  512. fi
  513. done
  514. }
  515. renumberIpfireFiles() { # Renumber entries of ipfire files
  516. for ipfire_file in $customnetworks $customgroups; do
  517. if [[ -w $ipfire_file ]]; then
  518. if [[ $verbose ]]; then echo "---[Renumbering $ipfire_file ]---"; fi
  519. sed -i '/^$/d;=' $ipfire_file # Delete empty lines and add numbered lines
  520. sed -i 'N;s/\n[0-9]\+//' $ipfire_file # Renumber lines by consolidation
  521. else
  522. echo -e "File $ipfire_file not found or write protected.\nCheck your IPFire installation."
  523. fi
  524. done
  525. }
  526. sortIpfireFiles() { # Sort entries of ipfire files
  527. for ipfire_file in $customnetworks $customgroups; do
  528. if [[ -w $ipfire_file ]]; then
  529. if [[ $verbose ]]; then echo "---[Re-sorting $ipfire_file ]---"; fi
  530. cat $ipfire_file | sort -t, -k2,2V -o $ipfire_file # Sort lines by name
  531. sed -i '/^$/d;=' $ipfire_file # Delete empty lines and add numbered lines
  532. sed -i 'N;s/\n[0-9]\+//' $ipfire_file # Renumber lines by consolidation
  533. else
  534. echo -e "File $ipfire_file not found or write protected.\nCheck your IPFire installation."
  535. fi
  536. done
  537. }
  538. restoreIpfireFiles() { # Restore ipfire file
  539. for ipfire_file in $customnetworks $customgroups; do
  540. if [[ -w $ipfire_file ]]; then
  541. if [[ -f "$ipfire_file.bak" ]]; then
  542. cp -f $ipfire_file.bak $ipfire_file
  543. echo "File $ipfire_file restored."
  544. else
  545. echo "No backup file $ipfire_file.bak found."
  546. fi
  547. else
  548. echo -e "File $ipfire_file not found or write protected.\nCheck your IPFire installation."
  549. fi
  550. done
  551. }
  552. listIpfireFiles () { # Show companies from ipfire files
  553. for ipfire_file in $customnetworks $customgroups; do
  554. if [[ -f $ipfire_file ]]; then
  555. echo "Company names (number of networks) in "$ipfire_file":"
  556. grep "$auto_remark" $ipfire_file | cut -d, -f2,4 | cut -d- -f1 | cut -d, -f1 | uniq -c | awk -F" " '{ printf "%s (%i)\n", $2, $1 }'
  557. else
  558. echo -e "File $ipfire_file not found.\nCheck your IPFire installation."
  559. fi
  560. done
  561. }
  562. print_help() { # Help info
  563. echo "Usage: asn_ipfire.sh [OPTION] [COMPANYs | -f FILE] [PARAMETER]"
  564. echo "Add or remove networks to IPFire firewall Groups: Networks & Host Groups"
  565. echo
  566. echo "IPFire-Mode options:"
  567. echo " -a, --add Add new company networks"
  568. echo " -r, --remove Remove company networks from customnetworks & customgroups"
  569. echo
  570. echo "IPFire-Mode parameter:"
  571. echo " -l, --list List of companies already added by this script"
  572. echo " --renumber Renumber lines of customnetworks & customgroups"
  573. echo " --sort Re-sort lines of customnetworks & customgroups"
  574. echo " --backup Backup customnetworks & customgroups before change"
  575. echo " --rmbackup Remove backup files of customnetworks & customgroups"
  576. echo " --restore Restore customnetworks & customgroups from backup"
  577. echo
  578. echo "Non-IPFire-Mode options:"
  579. echo " --network Create FILE '$file_network' with networks"
  580. echo " --asn Create FILE '$file_asn' with ASNs only"
  581. echo " --custom Create FILE '$output_file' with custom rules"
  582. echo
  583. echo "Common parameter:"
  584. echo " -c, --check Check source for availability"
  585. echo " -f, --file FILE Get company list from FILE"
  586. echo " -i, --interactive Ask user before a file will be overwritten"
  587. echo " -k, --keep Keep temporary source files after finish"
  588. echo " -o, --outfile FILE Custom result file FILE (Non-IPFire-Mode)"
  589. echo " --raw Networks not consolidated"
  590. echo " -v, --verbose Verbose mode"
  591. echo " -V, --version Show this script version and exit"
  592. echo " -h, --help Show this help and exit"
  593. echo
  594. echo "COMPANY to be one or more company names, put into double quotes (\"...\")"
  595. echo " Multi company names must be comma separated"
  596. echo " Substitute spaces with tilde (~)"
  597. echo " Restrict to exact matches with tilde (~) before and after the name"
  598. echo " Company names are handled case insensitive."
  599. echo " example: asn_ipfire.sh --add \"CompanyA,Company~NameB,~CompanyC~\" "
  600. echo
  601. echo "FILE to be a name of a file, containing one or more company names."
  602. echo " Company names to be separated by comma or line feed."
  603. echo " examples: asn_ipfire.sh -a -f company.list "
  604. echo " asn_ipfire.sh --network -f company.list "
  605. echo
  606. echo "Option --remove only affects entries made by asn_ipfire.sh itself."
  607. echo " These entries are recognized by the 'Remark'-column in IPFire."
  608. echo " To remove all entries done by this script, use 'ALL' as COMPANY "
  609. echo " examples: asn_ipfire.sh -r \"CompanyA, CompanyB\" "
  610. echo " asn_ipfire.sh -r ALL "
  611. echo
  612. }
  613. #######################################################################
  614. # Main program
  615. company_array=() # Create empty company array
  616. result_array=() # Create empty result array
  617. mode="" # Initial mode
  618. verbose="" # Default verbose = OFF
  619. backup="" # Default backup of ipfire files = OFF
  620. keeptemp="" # Default interactive mode = OFF
  621. interact="" # Default Keep source temp file after finish = OFF
  622. temp1="$temppath/$temp1" # Source temp file
  623. temp2="$temppath/$temp2" # Source temp file
  624. helptext="Usage: asn_ipfire.sh [OPTION] [COMPANYs | -f FILE] \nTry 'asn_ipfire.sh --help' for more information."
  625. chkSystem # ipfire system? wget or curl available?
  626. # Check arguments and get company array
  627. if [[ $# -eq 0 ]]; then echo -e $helptext; exit 1; fi # No arguments --> exit
  628. if [[ $# -gt 10 ]]; then echo -e "Too many arguments.\n""$helptext"; exit 1; fi # Too many arguments --> exit
  629. while [[ $# -gt 0 ]] ; do
  630. case $1 in
  631. -a|--add | -r|--remove | --asn | --network | --iptable | --afwall | --custom) {
  632. if [[ $mode ]]; then # Mode already set
  633. echo -e "Too many arguments.\n""$helptext"
  634. exit 1
  635. else
  636. mode=$1
  637. if [[ ! $2 ]]; then
  638. echo -e "No COMPANY names given.\n""$helptext"
  639. exit 1
  640. elif [[ ${2:0:1} == "-" ]]; then # followed by argument instead of company names
  641. if [[ "$2" != "-f" && "$2" != "--file" ]]; then # followed by argument instead of company names
  642. echo -e "Wrong order of arguments.\n""$helptext" # Wrong order of arguments --> exit
  643. exit 1
  644. fi
  645. else
  646. company_array_from_arg=(`sed -E 's/[ ]*//g; s/[,]+/ /g; s/\[//g; s/\]//g; s/[.\]*//g' <<< $2`) # Trim empty entries
  647. shift
  648. fi
  649. fi
  650. };;
  651. -f | --file) {
  652. if [[ -f $2 ]]; then # File exist
  653. company_array_from_file=(`sed 's/[ ]*//g; s/,\+/ /g; s/\[//g; s/\]//g; s/[.\]*//g; s/#.*$//g; /^$/d' <<< cat $2`) # Substitute space,comma,slash,comments,lines
  654. shift
  655. else # File not exist --> exit
  656. echo "Company file not found."
  657. echo -e "$helptext"
  658. exit 2
  659. fi
  660. };;
  661. -o | --outfile) {
  662. if [[ ! $2 ]]; then
  663. echo -e "No output file given.\n""$helptext"
  664. exit 1
  665. else
  666. output_file="$2"
  667. shift
  668. fi
  669. };;
  670. -l|--list | --renumber | --sort | --restore | --rmbackup | -h|--help | -V|--version) {
  671. if [[ $mode ]] || [[ $3 ]] || ( [[ $interact ]] && [[ $2 ]] ); then # No more arguments allowed for this option, except of interact
  672. echo -e "Too many arguments.\n""$helptext" # Too many parameter --> exit
  673. exit 1
  674. else
  675. mode=$1
  676. fi
  677. };;
  678. --backup ) { # Don't write backup Ipfire files
  679. if [[ ! $mode ]] && [[ ! $2 ]]; then
  680. echo -e "Missing arguments.\n""$helptext"
  681. exit 1
  682. else
  683. backup=1
  684. fi
  685. };;
  686. --raw ) { # Keep temporary source files
  687. if [[ ! $mode ]] && [[ ! $2 ]]; then
  688. echo -e "Missing arguments.\n""$helptext"
  689. exit 1
  690. else
  691. raw=1
  692. fi
  693. };;
  694. -k|--keep ) { # Keep temporary source files
  695. if [[ ! $mode ]] && [[ ! $2 ]]; then
  696. echo -e "Missing arguments.\n""$helptext"
  697. exit 1
  698. else
  699. keeptemp=1
  700. fi
  701. };;
  702. -i|--interactive ) { # Verbose mode shows stats
  703. if [[ ! $mode ]] && [[ ! $2 ]]; then
  704. echo -e "Missing arguments.\n""$helptext"
  705. exit 1
  706. else
  707. interact=1
  708. fi
  709. };;
  710. -v|--verbose ) { # Verbose mode shows stats
  711. if [[ ! $mode ]] && [[ ! $2 ]]; then
  712. echo -e "Missing arguments.\n""$helptext"
  713. exit 1
  714. else
  715. verbose=1
  716. fi
  717. };;
  718. -c|--check ) { # Check source
  719. if [[ ! $mode ]] && [[ ! $2 ]]; then
  720. echo -e "Missing arguments.\n""$helptext"
  721. exit 1
  722. else
  723. checksource=1
  724. fi
  725. };;
  726. *) {
  727. echo -e "Unknown argument.\n""$helptext" # Unknown arguments --> exit
  728. exit 1
  729. };;
  730. esac
  731. shift
  732. done
  733. company_array=(`echo ${company_array_from_file[@]} ${company_array_from_arg[@]} | sort -uf`)
  734. case $mode in
  735. -a|--add) { # Add objects to ipfire files
  736. if [[ $is_ipfire ]]; then
  737. output_file="customnetworks & customgroups"
  738. if [ ! $company_array ]; then
  739. echo "No company names found. Nothing done!"
  740. echo "Try 'asn_ipfire.sh --help' for more information."
  741. exit 1
  742. fi
  743. if [[ $checksource ]]; then chkSource ; fi
  744. collectNetworks # Get networks and write to file
  745. if [[ $interact ]]; then
  746. echo -n "Update IPfire files now? OK[Enter]? "
  747. read -r p
  748. if [[ "$p" ]]; then
  749. echo "Nothing done to IPFire."
  750. exit 3
  751. fi
  752. fi
  753. cleanupNetworks # Remove existing entries
  754. renumberIpfireFiles # Renumbering
  755. addNetworks # Get networks and write to file
  756. echo "---[Restarting firewall]---"
  757. /etc/init.d/firewall restart 1> /dev/null # Restart firewall
  758. echo "---[All done!]---"
  759. else
  760. echo -e "IPFire not found.\nCheck your IPFire installation."
  761. fi
  762. };;
  763. -r|--remove) { # Add objects to ipfire files
  764. if [[ $is_ipfire ]]; then
  765. output_file="customnetworks & customgroups"
  766. if [ ! $company_array ]; then
  767. echo "No company names found. Nothing done!"
  768. echo "Try 'asn_ipfire.sh --help' for more information."
  769. exit 1
  770. fi
  771. if [[ $interact ]]; then
  772. echo -n "Update IPfire files now? OK[Enter]? "
  773. read -r p
  774. if [[ "$p" ]]; then
  775. echo "Nothing done to IPFire."
  776. exit 3
  777. fi
  778. fi
  779. cleanupNetworks # Remove existing entries
  780. renumberIpfireFiles # Renumbering
  781. echo "---[Restarting firewall]---"
  782. /etc/init.d/firewall restart 1> /dev/null # Restart firewall
  783. echo "---[All done!]---"
  784. else
  785. echo -e "IPFire not found.\nCheck your IPFire installation."
  786. fi
  787. };;
  788. -l|--list) { # Function: List all company names already there by asn_ipfire
  789. if [[ $is_ipfire ]]; then
  790. listIpfireFiles
  791. else
  792. echo -e "IPFire not found.\nCheck your IPFire installation."
  793. fi
  794. };;
  795. --renumber) {
  796. if [[ $is_ipfire ]]; then
  797. verbose=1
  798. if [[ $interact ]]; then
  799. echo -n "Update IPfire files now? OK[Enter]? "
  800. read -r p
  801. if [[ "$p" ]]; then
  802. echo "Nothing done."
  803. exit 3
  804. fi
  805. fi
  806. renumberIpfireFiles
  807. else
  808. echo -e "IPFire not found.\nCheck your IPFire installation."
  809. fi
  810. };;
  811. --sort) {
  812. if [[ $is_ipfire ]]; then
  813. if [[ $interact ]]; then
  814. echo -n "Update IPfire files now? OK[Enter]? "
  815. read -r p
  816. if [[ "$p" ]]; then
  817. echo "Nothing done."
  818. exit 3
  819. fi
  820. fi
  821. verbose=1
  822. sortIpfireFiles
  823. else
  824. echo -e "IPFire not found.\nCheck your IPFire installation."
  825. fi
  826. };;
  827. --rmbackup) {
  828. if [[ $is_ipfire ]]; then
  829. verbose=1
  830. if [[ $interact ]]; then
  831. echo -n "Remove backup files now? OK[Enter]? "
  832. read -r p
  833. if [[ "$p" ]]; then
  834. echo "Nothing done."
  835. exit 3
  836. fi
  837. fi
  838. removeBackup
  839. else
  840. echo -e "IPFire not found.\nCheck your IPFire installation."
  841. fi
  842. };;
  843. --restore) {
  844. if [[ $is_ipfire ]]; then
  845. if [[ $interact ]]; then
  846. echo -n "Restore previous IPfire files now? OK[Enter]? "
  847. read -r p
  848. if [[ "$p" ]]; then
  849. echo "Nothing done to IPFire."
  850. exit 3
  851. fi
  852. fi
  853. restoreIpfireFiles
  854. else
  855. echo -e "IPFire not found.\nCheck your IPFire installation."
  856. fi
  857. };;
  858. --asn | --network | --custom | --iptable | --afwall) { # Create output files
  859. if [[ $mode == "--iptable" || $mode == "--afwall" ]]; then
  860. echo
  861. echo "WARNING !"
  862. echo " You are using obsolete option '$mode', which will not work in future versions"
  863. echo " Use option '--custom' instead, together with a config file."
  864. echo " See the wiki for further information"
  865. echo
  866. fi
  867. if [[ ! "$output_file" ]]; then # Custom output file given?
  868. output_file="file_"${mode:2} # Generate output file
  869. output_file="${!output_file}"
  870. fi
  871. if [ $company_array ]; then
  872. if [[ -f "$output_file" ]]; then
  873. if [[ $interact ]]; then
  874. read -r -p "Output file ($output_file) already exists! [O]verwrite, [A]ppend or abort? " ans
  875. case "$ans" in
  876. [Oo]) >"$output_file" ;;
  877. [Aa]) ;;
  878. *) exit 3;;
  879. esac
  880. else
  881. echo "Warning: output file ($output_file) already exists and will be overwritten!"
  882. >"$output_file"
  883. fi
  884. else # File not exist --> exit
  885. touch "$output_file" 2> /dev/null # Check if writable?
  886. fi
  887. if [[ ! -w "$output_file" ]]; then
  888. echo "Output file ($output_file) is not writable!"
  889. exit 2
  890. fi
  891. if [[ $checksource ]]; then chkSource ; fi
  892. collectNetworks "$mode" # Get and add new networks
  893. outResults "$mode"
  894. echo "---[All done!]---"
  895. else
  896. echo "No company names found. Nothing done!"
  897. echo "Try 'asn_ipfire.sh --help' for more information."
  898. fi
  899. };;
  900. -V|--version ) { # Show version and quit
  901. echo "$revision";
  902. };;
  903. -h|--help) {
  904. print_help # Show help and quit
  905. };;
  906. *) echo -e "$helptext";; # Wrong or unknown parameter
  907. esac
  908. exit 0