-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseVNC.sh
More file actions
127 lines (107 loc) · 3.59 KB
/
reverseVNC.sh
File metadata and controls
127 lines (107 loc) · 3.59 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
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
#!/bin/bash
# Reverse VNC tunnel setup
# Run as root on the remote host, after reverseSSH.sh has completed.
# Establishes a persistent reverse tunnel for VNC (port 5900) back to the Target,
# using the second port allocated by findopenport.sh.
#
# Usage: reverseVNC.sh [--systemd] <user> <target-host>
#
# --systemd Install a systemd unit instead of a cron @reboot entry.
# Creates /etc/autossh-vnc.conf and autossh-vnc.service.
#
# Requires:
# - A VNC server running on this host (e.g. tigervnc, x11vnc) on port 5900
# - /home/<user>/install/openportlist.txt written by reverseSSH.sh
# (second line is used as the VNC tunnel port)
# - SSH key exchange already completed by reverseSSH.sh
USE_SYSTEMD=false
if [[ "$1" == "--systemd" ]]; then
USE_SYSTEMD=true
shift
fi
if [[ $# -lt 2 ]]; then
echo "Usage: $0 [--systemd] <user> <target-host>"
exit 1
fi
USER="$1"
TARGET="$2"
PORT_FILE="/home/$USER/install/openportlist.txt"
if [[ ! -f "$PORT_FILE" ]]; then
echo "Port list not found at $PORT_FILE. Run reverseSSH.sh first."
exit 1
fi
readarray -t remote_port < "$PORT_FILE"
VNC_PORT="${remote_port[1]}"
if [[ -z "$VNC_PORT" ]]; then
echo "No second port found in $PORT_FILE. Ensure findopenport.sh returned 2 ports."
exit 1
fi
VNC_AUTOSSH_OPTS='-M 0 -o "ServerAliveInterval 10" -o "ServerAliveCountMax 3"'
echo "Setting up reverse VNC tunnel on port $VNC_PORT -> $TARGET:5900"
if $USE_SYSTEMD; then
cat > /etc/autossh-vnc.conf <<EOF
TUNNEL_USER=$USER
TUNNEL_TARGET=$TARGET
TUNNEL_PORT=$VNC_PORT
VNC_PORT=5900
SSH_KEY=/home/$USER/.ssh/id_rsa
EOF
chmod 600 /etc/autossh-vnc.conf
cat > /etc/systemd/system/autossh-vnc.service <<EOF
[Unit]
Description=AutoSSH reverse VNC tunnel on port $VNC_PORT
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
EnvironmentFile=/etc/autossh-vnc.conf
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh \\
-M 0 \\
-o "ServerAliveInterval 10" \\
-o "ServerAliveCountMax 3" \\
-o "ExitOnForwardFailure yes" \\
-NR \${TUNNEL_PORT}:localhost:\${VNC_PORT} \\
-i \${SSH_KEY} \\
\${TUNNEL_USER}@\${TUNNEL_TARGET}
Restart=always
RestartSec=30
User=${USER}
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable autossh-vnc.service
systemctl start autossh-vnc.service
echo "Systemd unit autossh-vnc.service installed and started."
else
CRON_ENTRY="@reboot nohup /usr/bin/autossh -M 0 -o 'ServerAliveInterval 10' -o 'ServerAliveCountMax 3' -NR ${VNC_PORT}:localhost:5900 $USER@$TARGET &"
CRONTAB_FILE="/var/spool/cron/crontabs/$USER"
if ! grep -qF "NR ${VNC_PORT}:localhost:5900" "$CRONTAB_FILE" 2>/dev/null; then
echo "$CRON_ENTRY" >> "$CRONTAB_FILE"
fi
nohup /usr/bin/autossh -M 0 \
-o "ServerAliveInterval 10" \
-o "ServerAliveCountMax 3" \
-NR ${VNC_PORT}:localhost:5900 \
$USER@$TARGET &
fi
## Create VNC connection shortcut on Target
VNC_SHORTCUT="/home/$USER/install/vnc-${VNC_PORT}.sh"
cat > "$VNC_SHORTCUT" <<EOF
#!/bin/bash
# Connect to VNC on remote host via reverse tunnel
# Requires a VNC viewer — adjust the command for your viewer of choice:
# TigerVNC: vncviewer localhost:${VNC_PORT}
# Remmina: remmina -c vnc://localhost:${VNC_PORT}
# macOS: open vnc://localhost:${VNC_PORT}
vncviewer localhost:${VNC_PORT}
EOF
chmod +x "$VNC_SHORTCUT"
scp -i /home/$USER/install/$USER.pem \
"$VNC_SHORTCUT" \
$USER@$TARGET:/home/$USER/
echo "VNC tunnel started. Port on $TARGET: $VNC_PORT"
echo "On $TARGET run: ./vnc-${VNC_PORT}.sh"