-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathossh.sh
executable file
·66 lines (54 loc) · 1.51 KB
/
ossh.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
BEFORE_ESTABLISHED_SCRIPT="/home/$USER/.ssh/scripts/before_established.sh"
AFTER_ESTABLISHED_SCRIPT="/home/$USER/.ssh/scripts/after_established.sh"
# First argument should be the Hostname
if [[ -z "$1" ]]; then
echo "Usage: $0 hostname [ssh_args...]"
exit 1
fi
# Function to check if hostname exists in ~/.ssh/config
hostname_exists() {
local host=$1
awk -v host="$host" '
$1 == "Host" && $2 == host { print "exists"; exit }
' ~/.ssh/config
}
# Function fetch ~/.ssh/config data by hostname (e.g., User, IdentityFile)
get_ssh_option() {
local host=$1
local option=$2
awk -v host="$host" -v option="$option" '
$1 == "Host" { in_host_block = ($2 == host); next }
in_host_block && $1 == option { print $2 }
' ~/.ssh/config
}
is_ainb() {
a=$1
b=($2)
for b_item in "${b[@]}"; do
if [[ "$a" == "$b_item" ]]; then
echo 1
return
fi
done
echo 0
}
is_hostname() {
hostnames=$1
echo $(is_ainb "$Hostname" "$hostnames")
return
}
# Make functions accessable to ossh scripts
export -f get_ssh_option
export -f is_ainb
export -f is_hostname
Hostname=$1
shift
# Check if Hostname exists in ~/.ssh/config
if [[ -z "$(hostname_exists "$Hostname")" ]]; then
echo "Error: Hostname '$Hostname' not found in ~/.ssh/config"
exit 1
fi
$BEFORE_ESTABLISHED_SCRIPT "$Hostname"
# Establish SSH connection with LocalCommand option that will be called afer ssh connection established
ssh $Hostname -o PermitLocalCommand=yes -o LocalCommand="$AFTER_ESTABLISHED_SCRIPT $Hostname" "$@"