Skip to content

Commit c6a0796

Browse files
authored
Add files via upload
1 parent a26a434 commit c6a0796

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

ssh_tunnel.sh

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#!/bin/bash
2+
3+
RED='\033[0;31m'
4+
GREEN='\033[0;32m'
5+
YELLOW='\033[1;33m'
6+
BLUE='\033[0;34m'
7+
NC='\033[0m'
8+
9+
info() {
10+
echo -e "${BLUE}[INFO] $1${NC}"
11+
}
12+
13+
warning() {
14+
echo -e "${YELLOW}[WARNING] $1${NC}"
15+
}
16+
17+
error() {
18+
echo -e "${RED}[ERROR] $1${NC}"
19+
}
20+
21+
success() {
22+
echo -e "${GREEN}[SUCCESS] $1${NC}"
23+
}
24+
25+
input() {
26+
local message="$1"
27+
local color="$2"
28+
local response
29+
30+
echo -en "${color}${message}${NC}" >&2
31+
32+
read -r response
33+
34+
echo "$response"
35+
}
36+
37+
validate_user() {
38+
local user="$1"
39+
if id "$user" >/dev/null 2>&1; then
40+
return 0
41+
else
42+
return 1
43+
fi
44+
}
45+
46+
validate_port() {
47+
local port="$1"
48+
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
49+
return 1
50+
else
51+
return 0
52+
fi
53+
}
54+
55+
validate_host() {
56+
local host="$1"
57+
if ping -c 1 $host >/dev/null 2>&1; then
58+
return 0
59+
else
60+
return 1
61+
fi
62+
}
63+
64+
kill_tunnel() {
65+
local port="$1"
66+
if ! pkill -f "ssh.*-L.*$port" || ! pkill -f "ssh.*-R.*$port"; then
67+
error "Failed to kill tunnel on port $port"
68+
else
69+
success "Tunnel on port $port successfully killed"
70+
fi
71+
}
72+
73+
SSH_OPTIONS="-o ServerAliveInterval=30 -o ServerAliveCountMax=3"
74+
75+
while true; do
76+
echo "Select tunnel type:"
77+
echo "1. Local to Remote"
78+
echo "2. Remote to Local"
79+
echo "3. Both sides"
80+
echo "4. Kill tunnel"
81+
echo "5. Exit"
82+
choice=$(input "Enter your choice: " $GREEN)
83+
case $choice in
84+
1)
85+
info "Setting up Local to Remote tunnel..."
86+
87+
LOCAL_IP=$(hostname -I | cut -d' ' -f1)
88+
LOCAL_PORT=$(input "Enter local port: " $BLUE)
89+
while ! validate_port $LOCAL_PORT; do
90+
error "Port $LOCAL_PORT is not available. Please enter a different port."
91+
LOCAL_PORT=$(input "Enter local port: " $BLUE)
92+
done
93+
94+
REMOTE_USER=$(input "Enter remote user: " $YELLOW)
95+
96+
REMOTE_HOST=$(input "Enter remote host: " $YELLOW)
97+
while ! validate_host $REMOTE_HOST; do
98+
error "Host $REMOTE_HOST is not reachable. Please enter a different host."
99+
REMOTE_HOST=$(input "Enter remote host: " $YELLOW)
100+
done
101+
102+
REMOTE_PORT=$(input "Enter remote port: " $YELLOW)
103+
104+
ssh $SSH_OPTIONS -L 0.0.0.0:$LOCAL_PORT:localhost:$LOCAL_PORT -C -fN $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT
105+
106+
success "Local to Remote tunnel created from $LOCAL_IP:$LOCAL_PORT to $REMOTE_HOST:$REMOTE_PORT"
107+
;;
108+
2)
109+
info "Setting up Remote to Local tunnel..."
110+
111+
LOCAL_PORT=$(input "Enter local port: " $BLUE)
112+
while ! validate_port $LOCAL_PORT; do
113+
error "Port $LOCAL_PORT is not available. Please enter a different port."
114+
LOCAL_PORT=$(input "Enter local port: " $BLUE)
115+
done
116+
117+
REMOTE_USER=$(input "Enter remote user: " $YELLOW)
118+
119+
REMOTE_HOST=$(input "Enter remote host: " $YELLOW)
120+
while ! validate_host $REMOTE_HOST; do
121+
error "Host $REMOTE_HOST is not reachable. Please enter a different host."
122+
REMOTE_HOST=$(input "Enter remote host: " $YELLOW)
123+
done
124+
125+
REMOTE_PORT=$(input "Enter remote port: " $YELLOW)
126+
127+
ssh $SSH_OPTIONS -R 0.0.0.0:$REMOTE_PORT:localhost:$LOCAL_PORT -C -fN $REMOTE_USER@$REMOTE_HOST
128+
129+
success "Remote to Local tunnel created from $REMOTE_HOST:$REMOTE_PORT to localhost:$LOCAL_PORT"
130+
;;
131+
3)
132+
info "Setting up both sides of the tunnel..."
133+
134+
LOCAL_PORT=$(input "Enter local port: " $BLUE)
135+
while ! validate_port $LOCAL_PORT; do
136+
error "Port $LOCAL_PORT is not available. Please enter a different port."
137+
LOCAL_PORT=$(input "Enter local port: " $BLUE)
138+
done
139+
140+
REMOTE_USER=$(input "Enter remote user: " $YELLOW)
141+
142+
REMOTE_HOST=$(input "Enter remote host: " $YELLOW)
143+
while ! validate_host $REMOTE_HOST; do
144+
error "Host $REMOTE_HOST is not reachable. Please enter a different host."
145+
REMOTE_HOST=$(input "Enter remote host: " $YELLOW)
146+
done
147+
148+
REMOTE_PORT=$(input "Enter remote port: " $YELLOW)
149+
150+
ssh $SSH_OPTIONS -L 0.0.0.0:$LOCAL_PORT:localhost:$LOCAL_PORT -C -fN $REMOTE_USER@$REMOTE_HOST -p $REMOTE_PORT
151+
152+
ssh $SSH_OPTIONS -R 0.0.0.0:$REMOTE_PORT:localhost:$LOCAL_PORT -C -fN $REMOTE_USER@$REMOTE_HOST
153+
154+
success "Both sides of the tunnel created from $LOCAL_IP:$LOCAL_PORT to $REMOTE_HOST:$REMOTE_PORT and from $REMOTE_HOST:$REMOTE_PORT to localhost:$LOCAL_PORT"
155+
;;
156+
4)
157+
info "Killing existing tunnels..."
158+
159+
LOCAL_PORT=$(input "Enter local port: " $BLUE)
160+
161+
kill_tunnel $LOCAL_PORT
162+
;;
163+
5)
164+
info "Exiting..."
165+
exit 0
166+
;;
167+
*)
168+
error "Invalid choice! Please select a valid option."
169+
;;
170+
esac
171+
done

0 commit comments

Comments
 (0)