-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivebox_ssh.sh
executable file
·167 lines (135 loc) · 4.07 KB
/
livebox_ssh.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/sh
TCP_=6
UDP_=17
BOTH_=$TCP_,$UDP_
ORIGIN_=webui
SOURCE_INTERFACE_=data
SYSBUS=~/sysbus/sysbus.py
function set_port_forwarding_rule {
ARG_INTERNAL_PORT=$1
ARG_EXTERNAL_PORT=$2
ARG_PROTOCOL=$3
ARG_TARGET_IP_ADDR=$4
ORIGIN=$ORIGIN_
SOURCE_INTERFACE=$SOURCE_INTERFACE_
INTERNAL_PORT=$ARG_INTERNAL_PORT
DESTINATION_IP_ADDRESS=$ARG_TARGET_IP_ADDR
PROTOCOL=$ARG_PROTOCOL
# opt values
ID=""
EXTERNAL_PORT=$ARG_EXTERNAL_PORT
SOURCE_PREFIX=""
ENABLE=True
PERSISTANT=""
DESCRIPTION=""
DESTINATION_MAC_ADDRESS=""
LEASE_DURATION=""
UPNPVN1_COMPAT=False
$SYSBUS sysbus.Firewall:setPortForwarding \
id="$ID" \
origin="$ORIGIN" \
sourceInterface="$SOURCE_INTERFACE" \
externalPort="$EXTERNAL_PORT" \
internalPort="$INTERNAL_PORT" \
destinationIPAddress="$DESTINATION_IP_ADDRESS" \
sourcePrefix="$SOURCE_PREFIX" \
protocol="$PROTOCOL" \
enable="$ENABLE" \
persistent="$PERSISTANT" \
description="$DESCRIPTION" \
destinationMACAddress="$DESTINATION_MAC_ADDRESS" \
leaseDuration="$LEASE_DURATION" \
upnpv1Compat="$UPNPVN1_COMPAT"
}
function delete_port_forwarding_rule {
ARG_TARGET_IP_ADDR=$1
ORIGIN=$ORIGIN_
DESTINATION_IP_ADDRESS=$ARG_TARGET_IP_ADDR
$SYSBUS sysbus.Firewall:deletePortForwarding \
origin="$ORIGIN" \
DestinationIPAddress="$DESTINATION_IP_ADDRESS"
}
function config_sysbus {
ARG_PASSWORD=$1
ARG_LIVEBOX_VERSION=lb$2
$SYSBUS -config -password $ARG_PASSWORD -lversion $ARG_LIVEBOX_VERSION
}
function open_ssh_access {
ARG_LOCAL_IP_ADDRESS=$1
SSH_PORT=22
systemctl start sshd &&
set_port_forwarding_rule $SSH_PORT $SSH_PORT $BOTH_ $ARG_LOCAL_IP_ADDRESS 1&>/dev/null
echo "Accès SSH ouvert."
}
function close_ssh_access {
ARG_LOCAL_IP_ADDRESS=$1
systemctl stop sshd &&
delete_port_forwarding_rule $ARG_LOCAL_IP_ADDRESS 1&>/dev/null &&
echo "Accès SSH fermé."
}
function print_ssh_infos {
DISTANT_IP_ADDRESS=$($SYSBUS -info | grep ExternalIPAddress | cut -d ":" -f2 | tr -d " ")
USER=$(whoami)
echo "Addresse IP : $DISTANT_IP_ADDRESS"
echo "Nom d'utilisateur : $USER"
}
function check_config_ok {
if [ ! -f ~/.sysbusrc ]; then
echo "Erreur, la configuration du mot de passe administrateur et de la version de la livebox semble ne pas avoir été faite !"
exit 1
fi
}
function print_usage {
echo "Utilisation :"
echo -e "\t$0 {help|open|close|infos}"
echo -e "\t$0 config <livebox admin password> <livebox version number>"
}
function print_help {
print_usage
echo ""
echo "Ouvre ou ferme l'accès SSH au travers d'une livebox."
echo ""
echo "Commandes :"
echo -e "\thelp :"
echo -e "\t\tAffiche cette aide."
echo -e "\topen :"
echo -e "\t\tOuvre le port SSH sur la livebox et démarre le service sshd."
echo -e "\tclose :"
echo -e "\t\tFerme le port SSH sur la livebox et arrête le service sshd."
echo -e "\tinfos :"
echo -e "\t\tRetourne l'addresse IP publique et le nom d'utilisateur."
echo -e "\tconfig <password> <version> :"
echo -e "\t\tEnregistre le mot de passe administrateur de la livebox ainsi que sa version."
echo -e "\t\tCette action est necessaire pour pouvoir utiliser l'outil."
}
LOCAL_IP_ADDRESS=$(ip route | grep src | head -n 1 | cut -d ' ' -f9)
if [ -z "$1" ]; then
echo "Erreur, commande manquante."
print_usage
exit 1
fi
CMD=$1
if [ "$CMD" = "help" ]; then
print_help
elif [ "$CMD" = "open" ]; then
check_config_ok
open_ssh_access $LOCAL_IP_ADDRESS
elif [ "$CMD" = "close" ]; then
check_config_ok
close_ssh_access $LOCAL_IP_ADDRESS
elif [ "$CMD" = "infos" ]; then
check_config_ok
print_ssh_infos
elif [ "$CMD" = "config" ]; then
if [ "$#" != "3" ]; then
echo -e "Erreur, arguments maquants ou trop nombreux."
print_usage
exit 1
else
config_sysbus $2 $3
fi
else
echo "Erreur, commande invalide : $CMD."
print_usage
exit 1
fi