-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_tunnel.sh
More file actions
executable file
·57 lines (45 loc) · 1.69 KB
/
remove_tunnel.sh
File metadata and controls
executable file
·57 lines (45 loc) · 1.69 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
#!/bin/bash
TARGET_HOST=$1
# 1. Get input if argument is missing
if [ -z "$TARGET_HOST" ]; then
read -p "Enter Target IP/Hostname to remove: " TARGET_HOST
fi
if [ -z "$TARGET_HOST" ]; then
echo "[Error] No input provided. Exiting."
exit 1
fi
SSH_DIR="$HOME/.ssh"
CONFIG_FILE="$SSH_DIR/config"
# Replace dots with underscores for key file matching
HOST_SAFE=$(echo "$TARGET_HOST" | sed 's/\./_/g')
echo ""
echo "[Status] Removing ALL entries related to: $TARGET_HOST"
echo "--------------------------------------------------"
# 2. Delete Key Files
echo "[Process] Deleting matching key files..."
# Deletes id_rsa_HOST_SAFE and id_rsa_HOST_SAFE.pub if they exist
find "$SSH_DIR" -name "id_rsa_${HOST_SAFE}*" -type f -delete
echo "- Cleanup of key files complete."
# 3. Remove Sections from Config
if [ -f "$CONFIG_FILE" ]; then
echo "[Process] Removing Host/HostName matches from config..."
# Use awk to filter out the matching Host/HostName block
# Logic:
# - If a line matches 'Host target' or 'HostName target', start skipping.
# - If skipping and a new 'Host ' line appears, stop skipping.
# - Print lines only when not skipping.
TEMP_CONFIG=$(mktemp)
awk -v target="$TARGET_HOST" '
BEGIN { skip = 0 }
$1 == "Host" && $2 == target { skip = 1; next }
$1 == "HostName" && $2 == target { skip = 1; next }
skip == 1 && $1 == "Host" { skip = 0 }
!skip { print $0 }
' "$CONFIG_FILE" > "$TEMP_CONFIG"
mv "$TEMP_CONFIG" "$CONFIG_FILE"
chmod 600 "$CONFIG_FILE"
echo "- Config file updated (UTF-8 without BOM)."
fi
echo "--------------------------------------------------"
echo "[Finish] Cleanup complete."
echo ""