1 | 1 | simandl | #!/bin/sh |
2 | | | # stahovatko czf4bfu souboru ze serveru podle zadanych zdroju |
3 | | | # pokousi se i o rekurzivni pristup a stahnuti jinak nedostupnych |
4 | | | # baliku |
5 | | | |
6 | | | # sources je soubor s IP na jednom radku |
7 | | | #SOURCES="${1:-$HOME/czf4bfu/zdroje.txt}" |
8 | | | SOURCES="zdroje.txt" |
9 | | | # domeny ve kterych budu hledat czf4bfu host, jeho IP pouziju. |
10 | | | #DOMENY="${2:-$HOME/czf4bfu/domeny.txt}" |
11 | | | DOMENY="domeny.txt" |
12 | | | |
13 | | | # parametry pro wget |
14 | | | DSTDIR="/home/www/html/czf4bfu" |
15 | | | |
16 | | | # parametry wgetu. |
17 | | | # -N zapina overovani posledni modifikace, bohuzel dost serveru |
18 | | | # to nepodporuje nebo nema zapnute. Pripadne vypnout a nahradit |
19 | | | # -nc, coz zajisti pri kazdem pokusu prepsani stareho |
20 | | | WGETPARAMS="-r -l 0 -N -np -nd -t 3 -A '.txt,.deu' -P $DSTDIR" |
21 | | | |
22 | | | TMPFILE="/tmp/getall-czf-$RANDOM" |
23 | | | HOST=host |
24 | | | |
25 | | | function getbyip() |
26 | | | { |
27 | | | IP=$1 |
28 | | | # prvni zkusim soubor primo z tou IP. |
29 | | | # momentalne nutne u czela.czf |
30 | | | if wget $WGETPARAMS "http://$IP/czf4bfu/global-$IP.txt"; then |
31 | | | # pokud probehlo uspesne stazeni hlavniho souboru, zkus stahnout |
32 | | | # i pripadne dalsi soubory |
33 | | | # potom zkusim rekuznivne nalezt pripadne sousedy, o kterych |
34 | | | # on vi a ja ne, a stahnu taky jejich data |
35 | | | wget $WGETPARAMS "http://$IP/czf4bfu/" |
36 | | | fi |
37 | | | |
38 | | | } |
39 | | | |
40 | | | # vrati na stdout IP podle A zaznamu. |
41 | | | # predany parametr je domena |
42 | | | function getdnsip() |
43 | | | { |
44 | | | DOMAIN=$1 |
45 | | | # Potom prihodim seznamy dle A recordu |
46 | | | $HOST $DOMAIN | sed \ |
47 | | | -e "/has address/ ! d" \ |
48 | | | -e "s/^.*has address \([[:digit:].]\+\)[[:space:]]*$/\1/" |
49 | | | } |
50 | | | |
51 | | | function getsrvip() |
52 | | | { |
53 | | | # Ziskani pomoci SRV zaznamu |
54 | | | # trosku komplikace je, ze SRV vraci zaznam hostname, ne IP, takze jeste |
55 | | | # jednou resolve. |
56 | | | DOMAIN=$1 |
57 | | | host -t SRV _http._tcp.czf4bfu.$DOMAIN | sed \ |
58 | | | -e "/SRV [[:digit:]]\+ [[:digit:]]\+ [[:digit:]]\+/ ! d" \ |
59 | | | -e "s/^.*SRV [[:digit:]]\+ [[:digit:]]\+ [[:digit:]]\+ \([[:alnum:].-]\+\)[[:space:]]*$/\1/" | \ |
60 | | | while read HOSTNAME; do |
61 | | | getdnsip $HOSTNAME |
62 | | | done |
63 | | | } |
64 | | | |
65 | | | echo -n > "$TMPFILE" |
66 | | | |
67 | | | # Jako prvni zkus najit zdroje v defaultni domene |
68 | | | # Pozor. Pokud nebude fungovat timestamp, je lepsi dat svuj lokalni |
69 | | | # DEU soubor jako read-only pro toho kdo spousti tento skript, jinak by |
70 | | | # mohl prepsat deu soubor pro tento stroj. |
71 | | | getdnsip czf4bfu >> "$TMPFILE" |
72 | | | |
73 | | | # Napred zkusime hledani podle domen zadanych v souboru |
74 | | | [ -f "$DOMENY" ] && cat "$DOMENY" | while read DOMAIN; do |
75 | | | getsrvip $DOMAIN >> "$TMPFILE" |
76 | | | # Potom prihodim seznamy dle A recordu |
77 | | | getdnsip czf4bfu.$DOMAIN >> "$TMPFILE" |
78 | | | done |
79 | | | |
80 | | | # ted pridam do seznamu rucne zadane IP |
81 | | | cat "$SOURCES" >> "$TMPFILE" |
82 | | | |
83 | | | # seradim a vyhazu duplikaty |
84 | | | IPS=$(cat "$TMPFILE" | sort -u ) |
85 | | | |
86 | | | echo "Nalezene IP: $IPS" |
87 | | | |
88 | | | # a ted uz jenom stahnu |
89 | | | for IP in $IPS; do |
90 | | | getbyip $IP |
91 | | | done |
92 | | | |