-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtunnel
More file actions
executable file
·102 lines (88 loc) · 2.21 KB
/
Copy pathtunnel
File metadata and controls
executable file
·102 lines (88 loc) · 2.21 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
#!/bin/bash
script_folder="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
if [[ ! "$1" == "" ]]
then
conffile="$1"
else
conffile="tunnel"
fi
if [[ ! -f "$script_folder/$conffile.conf" ]]
then
echo "Conf file not exists : $conffile.conf"
exit 0
fi
. $script_folder/$conffile.conf
if [ `whoami` != "$tunnel_script_user" ]
then
echo "- Reconnexion sous $tunnel_script_user..."
sudo -u $tunnel_script_user -- $script_folder/tunnel $conffile
exit
fi
date=`date`
#echo "$date"
for ssh_host in $hostnames
do
echo "Check tunnel $ssh_host :"
conn=$( ssh $tunnel_user@$ssh_host -o ConnectTimeout=2 ls / | grep home )
# Not connected
if [ ! "$conn" ]
then
msg="$ssh_host ssh inaccessible"
echo "- $msg"
echo "$date $msg" >> $logfile
# Test if send mail
if [[ ! -f "$sentlock" ]]
then
echo "$msg" | mail -s "$myhostname : Tunnel ssh $ssh_host inaccessible" -r "$email_from" $email_to
touch "$sentlock"
fi
# Connected
else
echo "- $ssh_host ssh accessible"
# test if sent mail
if [[ -f "$sentlock" ]]
then
rm "$sentlock"
fi
fi
# Search if a tunnel process exists
pid=$( ps aux --user $tunnel_script_user | grep "$tunnel_port:127.0.0.1" | grep "$ssh_host" | grep -v grep | awk '{print $2}' )
remount="0"
tokill="0"
if [[ $pid ]]
then
echo "- Processus Tunnel existant : $pid"
if [ $conn ]
then
# Test connexion retour via tunnel (doule tunnel)
test=$( ssh $tunnel_user@127.0.0.1 -o ConnectTimeout=2 -p $tunnel_port "ssh $tunnel_script_user@127.0.0.1 -o ConnectTimeout=2 -p $tunnel_port ls / | grep home" )
if [ ! "$test" == "home" ]
then
tokill="retour tunnel dans les choux"
remount="1"
else
echo "- Tunnel OK"
fi
fi
else
if [ $conn ]
then
remount="1"
fi
fi
if [[ $tokill != "0" ]]
then
msg="- Tunnel $ssh_host $tokill, killing pid $pid ..."
echo $msg
echo $msg >> $logfile
kill $pid
fi
if [[ $remount == "1" ]]
then
echo "- Remontage tunnel $ssh_host ..."
echo "$date Remontage tunnels $ssh_host" >> $logfile
ssh -f -NC -o ServerAliveInterval=60 $tunnel_user@$ssh_host -R $tunnel_port:127.0.0.1:22 -L $tunnel_port:127.0.0.1:22
echo "- OK"
fi
done
exit 0