-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket-shell.sh
More file actions
executable file
·82 lines (68 loc) · 1.6 KB
/
Copy pathsocket-shell.sh
File metadata and controls
executable file
·82 lines (68 loc) · 1.6 KB
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
#!/usr/bin/env bash
# Defaults
MODE="user"
CUSTOM_SOCK=""
SOCK_DIR="/media/DataHost"
USER_SOCK="$SOCK_DIR/local/host.user.sock"
ROOT_SOCK="$SOCK_DIR/local/host.root.sock"
# ---- Parse arguments ----
case "$1" in
root)
MODE="root"
;;
user|"")
MODE="user"
;;
/*)
CUSTOM_SOCK="$1"
;;
*)
echo "Usage:"
echo " $0 # user shell"
echo " $0 root # root shell"
echo " $0 /path.sock # custom socket"
exit 1
;;
esac
# ---- Select socket ----
if [ -n "$CUSTOM_SOCK" ]; then
SOCK="$CUSTOM_SOCK"
elif [ "$MODE" = "root" ]; then
SOCK="$ROOT_SOCK"
else
SOCK="$USER_SOCK"
fi
# ---- Exit control flag ----
EXIT_REQUESTED=0
# Trap Ctrl+C to exit fully
trap 'EXIT_REQUESTED=1' INT
# ---- Main loop ----
while true; do
echo "[*] Connecting to $SOCK (mode: $MODE)"
echo "[*] Type 'exit' or press Ctrl+C to quit"
# Save terminal state
stty raw -echo
# Run socat in foreground
# socat STDIO,raw,echo=0 UNIX-CONNECT:"$SOCK",echo=0,raw,setsid,ignoreeof
script -qfc "socat STDIO,raw,echo=0 UNIX-CONNECT:$SOCK" /dev/null
# Restore terminal
stty sane echo
# If user pressed Ctrl+C → exit fully
if [ "$EXIT_REQUESTED" -eq 1 ]; then
echo
echo "[*] Exiting..."
exit 0
fi
echo
read -n 1 -t 5 -p "[?] Reconnect? (Y/n): " ans
case "$ans" in
n|N)
echo ""
echo "[*] Exiting..."
exit 0
;;
esac
echo ""
echo "[!] Disconnected. Reconnecting in 2s..."
sleep 2
done