-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhfs-share
executable file
·77 lines (67 loc) · 1.54 KB
/
hfs-share
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
#!/bin/bash
PORT=8000
HFS=hfs.py
# check for xclip
type xclip >& /dev/null
if [ $? -ne 0 ]; then # no xclip
echo "You must install xclip to use this script."
exit 1
fi
check_port_available() {
nc -l $1 &
pid=$!
(sleep 0.1 && kill $pid)
return $?
}
quote_url() {
python -c "import urllib; print(urllib.quote(\"$1\"))"
}
if [ $# -lt 1 ]; then
echo "usage: $0 <file1> [<file2> [ <file3> ...]]"
exit 1
fi
# extract and find internet address
IP_PATTERN="[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"
IP_DATA=`ifconfig | grep -o "inet addr:$IP_PATTERN "`
for item in $IP_DATA; do
if [[ "$item" =~ $IP_PATTERN ]]; then
SELF_ADDR=${BASH_REMATCH[0]}
if [ "$SELF_ADDR" == "127.0.0.1" ]; then # ignore loopback address
unset SELF_ADDR
continue
fi
break
fi
done
if [ -z "$SELF_ADDR" ]; then
echo "Your computer has no connection."
exit 1
fi
if [[ "$SELF_ADDR" =~ 192\.168\.[0-9]*\.[0-9]* ]]; then # local network address
echo "Your address $SELF_ADDR is not visible to remote hosts."
read -n 1 -p "Still proceed? y/[N]: " ch
echo
if [ "$ch" != "y" ]; then
exit 1
fi
fi
for ((port=$PORT; port<65536; port++)); do
echo "Trying port $port..."
check_port_available $port >& /dev/null
if [ $? -eq 0 ]; then
if [ $# -eq 1 ]; then
BASENAME=`basename "$1"`
LINK="http://$SELF_ADDR:$port/file/`quote_url "$BASENAME"`"
else
LINK="http://$SELF_ADDR:$port/"
fi
echo
echo "\"$LINK\" copied to clipboard"
echo
echo $LINK | xclip -selection c # copy to clipboard
$HFS -p $port "$@" 2> /dev/null
if [ $? -eq 0 ]; then
break
fi
fi
done