-
Notifications
You must be signed in to change notification settings - Fork 3
/
list_sftp
executable file
·59 lines (49 loc) · 1.28 KB
/
list_sftp
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
#!/bin/bash
configfile=server.conf
usage() { echo "Usage: $0 [ -l <outputlist> ] [-c <configfile>] [filter [...]]" 1>&2; exit $1; }
while getopts "l:c:h" o; do
case "${o}" in
c) configfile=${OPTARG}
if [[ ! -r ${configfile} ]]; then
echo "Cannot read ${configfile}" 1>&2
usage 1
fi
;;
l) listfile=${OPTARG} ;;
h) usage 0 ;;
*) usage 1 ;;
esac
done
shift $((OPTIND-1))
. ${configfile}
: ${fileserver:?}
# ${srvport}
: ${expname:?}
: ${basedir:=$(pwd)}
: ${download:?}
if [[ $( < ~/.netrc ) =~ machine[[:space:]]+${fileserver}[[:space:]]?login[[:space:]]+([^[:space:]]+) ]]; then
username="${BASH_REMATCH[1]}"
elif [[ $( < ~/.ssh/config ) =~ Host[[:space:]]+${fileserver}[[:space:]]+ ]]; then
username=
else
echo "cannot find login for machine ${fileserver} in ~/.netrc" >&2
exit 1
fi
if (( ${#@} )); then
dir=( "${@/#/ /${expname}/}" )
source="${dir[*]}"
else
source="/${expname}/"
fi
missing=( )
for d in $(lftp -c "connect sftp://${username:+${username}@}${fileserver}${srvport:+:${srvport}}; cls -q1B ${source}"); do
if [[ ! -d "${basedir}/${download}/${d}" ]]; then
missing+=( "${d}" )
: #echo "${d} missing"
else
: #echo "${d} present"
fi
done
if (( ${#missing[@]} > 0 )) && [[ -n ${listfile} ]]; then
printf "%s\n" "${missing[@]}" > ${listfile}
fi