Bonjour,
Voici un script que j'ai fait cette nuit. Il permet de gérer les règles du firewall, la connexion et reconnexion au vpn lorsque celui-ci est down et à l'arrêt du scheduler de download station tant que le VPN n'est pas up.
Je viens juste de le finir et il semble tourner correctement après les premiers tests. N'hésitez pas à me dire si il y'a quelque chose qui cloche.
#!/bin/ash
# script for managing firewall, VPN connection and Download station scheduler #
#
# iptables binary path
iptables="/sbin/iptables"
# DLStation
DLStation="/var/packages/DownloadStation/scripts/start-stop-status"
#VPN interface
interface_vpn="tun0"
# Log file
logfile="/root/vpn.log"
# TCP port authorized
vpn_tcp_port=22,5001,45025
# UDP port authorized
vpn_udp_port=
# OpenVPN configuration
openvpn_confid=o1377495440
openvpn_configname=IPredator
############### START SCRIPT ###################
# Is there a VPN connection ?
if [ -z "$(ifconfig | grep "$interface_vpn")" ]; then
echo $(date) ": VPN not started" >> $logfile
#No VPN, No Download station
$DLStation stop
if [ -n "$(ps | grep -v "grep" | grep "/sbin/scheduler")" ] ; then
echo $(date) ": ERROR, DownloadStation is always on :/" >> $logfile
else
echo $(date) ": DownloadStation is stopped" >> $logfile
fi
# Trying to start VPN connection
echo $(date) ": Trying to start VPN connection..." >> $logfile
echo conf_id=$openvpn_confid > /usr/syno/etc/synovpnclient/vpnc_connecting
echo conf_name=$openvpn_configname >> /usr/syno/etc/synovpnclient/vpnc_connecting
echo proto=openvpn >> /usr/syno/etc/synovpnclient/vpnc_connecting
synovpnc reconnect --protocol=openvpn --name=$openvpn_configname --retry=4 --interval=10 >> $logfile
fi
# Is there a VPN connection and can we ping google ?
if [ -z "$(ping -c 4 www.google.fr | grep "64 bytes")" ] && [ -n "$(ifconfig | grep "$interface_vpn")" ] ; then
echo $(date) ": Ping failed to www.google.fr..." >> $logfile
#VPN is going to be stopped, stop Download station
$DLStation stop
if [ -n "$(ps | grep -v "grep" | grep "/sbin/scheduler")" ]; then
echo $(date) ": ERROR, DownloadStation is always on :/" >> $logfile
else
echo $(date) ": DownloadStation is stopped" >> $logfile
fi
# kill the failed VPN connection
synovpnc kill_client >> $logfile
echo $(date) ": VPN connection closed" >> $logfile
wait 10000
# Trying to start VPN connection
echo $(date) ": Trying to start VPN connection..." >> $logfile
echo conf_id=$openvpn_confid > /usr/syno/etc/synovpnclient/vpnc_connecting
echo conf_name=$openvpn_configname >> /usr/syno/etc/synovpnclient/vpnc_connecting
echo proto=openvpn >> /usr/syno/etc/synovpnclient/vpnc_connecting
synovpnc reconnect --protocol=openvpn --name=$openvpn_configname --retry=4 --interval=10 >> $logfile
else
# Firewall is up ?
if [ -n "$(ifconfig | grep "$interface_vpn")" ] && [ -z "$($iptables -L -v | grep "$interface_vpn")" ]; then
echo $(date) ": Interface " $interface_vpn " found but firewall is not configured..." >> $logfile
#Incoming tcp vpn connections ACCEPT
if [ -n "$vpn_tcp_port" ]; then
$iptables -A INPUT -i $interface_vpn -p tcp -m multiport --dports $vpn_tcp_port -j ACCEPT
fi
#Incoming udp vpn connections ACCEPT
if [ -n "$vpn_udp_port" ]; then
$iptables -A INPUT -i $interface_vpn -p udp -m multiport --dports $vpn_udp_port -j ACCEPT
fi
#if none of the rules were matched DROP #
$iptables -A INPUT -i $interface_vpn -p tcp -j DROP
$iptables -A INPUT -i $interface_vpn -p udp -j DROP
$iptables -A INPUT -i $interface_vpn -p icmp -j DROP
echo $(date) ": Firewall configured successfully" >> $logfile
#VPN Connection and firewall are Ok, starting Download station
$DLStation start
if [ -n "$(ps | grep -v "grep" | grep "/sbin/scheduler")" ] ; then
echo $(date) ": DownloadStation is started" >> $logfile
else
echo $(date) ": ERROR, DownloadStation is stopped" >> $logfile
fi
else
# Update 08/10/2014
if [ -n "$(ifconfig | grep "$interface_vpn")" ] && [ -n "$($iptables -L -v | grep "$interface_vpn")" ]; then
#VPN Connection and firewall are Ok,
if [ -n "$(ps | grep -v "grep" | grep "/sbin/scheduler")" ] ; then
#VPN Connection, firewall and Download station are Ok
echo $(date) ": All network configuration is OK"
else
#VPN Connection and firewall are Ok but not Download station
#try to start it
echo $(date) ": ERROR, DownloadStation is stopped" >> $logfile
$DLStation start
if [ -n "$(ps | grep -v "grep" | grep "/sbin/scheduler")" ] ; then
echo $(date) ": DownloadStation is started" >> $logfile
else
echo $(date) ": ERROR, DownloadStation is stopped" >> $logfile
fi
fi
fi
fi
fi
exit 0;