-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-server.sh
executable file
·52 lines (45 loc) · 1.59 KB
/
run-server.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
#!/bin/bash
SCRIPT_DIR=$(echo "$0" | sed -e 's/\/[^\/]*$//')
SCRIPT_DIR=$(cd "$SCRIPT_DIR" && pwd)
NETWORK_DEV=$(ip link show | sed -ne '/state UP/p' | head -n 1 | sed -e 's/^[0-9]*: \([^:]*\):.*$/\1/')
IPADDR=$(ip addr show "$NETWORK_DEV" | sed -ne '/inet /p' | sed -e 's/^.*inet \(.*\)\/.*$/\1/')
PORT=8080
which nc &> /dev/null || {
echo "nc command is not installed." 1>&2
exit 1
}
TMPFILE=$(mktemp /tmp/dotfiles.XXXXX)
echo "generating archive..."
cd "$SCRIPT_DIR"
tar cz . > "$TMPFILE"
ls -l "$TMPFILE"
md5sum "$TMPFILE"
echo "running server..."
echo "run 'nc $IPADDR $PORT < /dev/null | bash' on target host"
(
echo '#!/bin/bash'
echo 'A="$HOME/.dotfiles"'
echo 'if [ -d "$A/.git" ]; then'
echo ' echo "The repository $A already exists." 1>&2'
echo ' exit 1'
echo 'fi'
echo '[ -e "$A" ] && rm -r "$A"'
echo 'mkdir "$A"'
echo 'B=`mktemp /tmp/dotfiles.XXXXX`'
echo 'MYPID=$$'
echo 'while [[ ! -c $(readlink /proc/$MYPID/fd/0) ]]; do'
echo ' if [ "$MYPID" -eq 0 -o ! -d "/proc/$MYPID" ]; then'
echo ' echo "no tty found" 1>&2'
echo ' exit 1'
echo ' fi'
echo ' MYPID=$(cat /proc/$MYPID/stat | cut -d " " -f 4)'
echo 'done'
#echo 'timeout 4 bash -c " cat /dev/stdin | tar zxC $A"; cd "$A"; bash install.sh < /proc/$MYPID/fd/0; exit 0'
#echo 'tar zxC $A; cd "$A"; bash install.sh < /proc/$MYPID/fd/0; exit 0'
echo 'cat > "$B"; cat "$B" | tar zxC $A; cd "$A"; bash install.sh < /proc/$MYPID/fd/0; rm -rf "$B" "$A/.git"; exit 0'
cat "$TMPFILE"
exec 1>&-
sleep .5
ps --ppid "$$" -o comm,pid | tail -n +2 | awk '/^nc/{print $2}' | xargs -I{} kill {}
) | nc -l -vv -p "$PORT"
rm -f "$TMPFILE"