-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.sh
executable file
·128 lines (112 loc) · 2.32 KB
/
web.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
# -*- coding: UTF8 -*-
# ---------------------------------------------
# @author: Guillaume Seren
# @since: 17/06/2015
# source: https://github.com/GuillaumeSeren/spider
# file: web.sh
# Licence: GPLv3
# ---------------------------------------------
# usage() {{{1
# Return the helping message for the use.
function usage()
{
cat << DOC
usage: "$0" options
Explore a ressource URL and return linked URL.
OPTIONS:
-h Show this message.
-u url (needed)
-l recursivity level :
n: default 1
0: infinite
-d Define a target domain to focus.
Sample:
Test a simple url
"$0" -u guillaumeseren.com
Find all linked ressources
"$0" -u guillaumeseren.com -l 0 -d guillaumeseren.com
DOC
}
# GETOPTS {{{1
# Get the param of the script.
while getopts "l:d:u:h" OPTION
do
flagGetOpts=1
case $OPTION in
h)
usage
exit 1
;;
u)
cmdUrl="$OPTARG"
;;
l)
cmdLevel="$OPTARG"
;;
d)
cmdDomain="$OPTARG"
;;
?)
echo "commande $1 inconnue"
usage
exit
;;
esac
done
# We check if getopts did not find no any param
if [ "$flagGetOpts" == 0 ]; then
echo 'This script cannot be launched without options.'
usage
exit 1
fi
# getLinkFromUrl() {{{1
function getLinkFromUrl() {
if [[ -n "$1" ]]; then
tempFile="$(mktemp -d)"
urlArray=($(wget --spider --force-html --page-requisites -P "$tempFile" --recursive "$rLevel" "$domainTarget" "$1" 2>&1 | grep '^--' | awk '{ print $3 }' | uniq))
rm -r "$tempFile"
else
urlArray=()
fi
}
# getRecursivityLevel() {{{1
function getRecursivityLevel(){
local rLevel=''
if [[ -n "$1" ]]; then
if [[ "$1" != 0 ]]; then
rLevel="-l $1"
else
rLevel=""
fi
else
rLevel="-l 1"
fi
echo "$rLevel"
}
# getDomainTarget() {{{1
function getDomainTarget() {
local domains=""
if [[ -n "$1" ]]; then
domains="--domains=$1"
else
domains=""
fi
echo "$domains"
}
# main() {{{1
function main() {
# We need a recursive level
rLevel=$(getRecursivityLevel "$cmdLevel")
# Do we target a domain
domainTarget=$(getDomainTarget "$cmdDomain")
# Test URL
declare -a urlArray
getLinkFromUrl "$cmdUrl"
for i in "${urlArray[@]}"
do
echo "$i"
done
}
main
# vim: set ft=sh ts=2 sw=2 tw=80 foldmethod=marker et :