OpenVPN notification on connect and disconnect



  • Hello,

    Since i am working with domoticz home automation and i figured out the telegram notifications (which works really great) i would like to get a notification in telegram on connect and disconnect of my openvpn. I found already some descriptions of notifications by email:
    https://forum.pfsense.org/index.php?topic=97267.msg541709#msg541709
    https://www.reddit.com/r/PFSENSE/comments/6yuisa/is_it_possible_to_setup_email_notifications_when/

    As i can send a notification to telegram via http-call i put the command in the /usr/local/sbin/openvpn.attributes.sh in the connect- and disconnect-section. But it doesn't work. The connection is established normally, but no notification.

    This is the file:

    echo $script_type > /tmp/script
    if [ "$script_type" = "client-connect" ]; then
    	if [ -f /tmp/$common_name ]; then
    		/bin/cat /tmp/$common_name > $1
    		/bin/rm /tmp/$common_name
    		/usr/local/bin/curl https://api.telegram.org/bot-my-personal-api-key/sendMessage?chat_id=xxx&text=VPNCONNECT
    ${trusted_ip} vpn_client_ip: ${ifconfig_pool_remote_ip} datetime: $(date +'%Y/%m/%d %H:%M:%S') ${common_name} has connected to OpenVPN
    		fi
    elif [ "$script_type" = "client-disconnect" ]; then
    	command="/sbin/pfctl -a 'openvpn/$common_name' -F rules"
    	eval $command
    	/sbin/pfctl -k $ifconfig_pool_remote_ip
    	/sbin/pfctl -K $ifconfig_pool_remote_ip
    	/usr/local/bin/curl https://api.telegram.org/bot-my-personal-api-key/sendMessage?chat_id=xxx&text=VPNDISCONNECT
    ${trusted_ip} vpn_client_ip: ${ifconfig_pool_remote_ip} datetime: $(date +'%Y/%m/%d %H:%M:%S') ${common_name} has disconnected from OpenVPN
    fi
    
    exit 0
    
    

    The only change in the original script are the lines with the curl-command.
    If i enter the url in the webbrowser or also in the shell of pfsense the message is sent. Sot the url is working. Just in the sript it seems not to work.

    Once it is working i will complete the message like this:
    /usr/local/bin/curl "https://api.telegram.org/bot-my-personal-api-key/sendMessage?chat_id=xxx&text=CONNECT user_name: ${common_name} remote_pub_address: ${trusted_ip} vpn_client_ip: ${ifconfig_pool_remote_ip} datetime: $(date +'%Y/%m/%d %H:%M:%S') ${common_name} has connected to OpenVPN"

    Does anyon have an idea what can be the problem?

    Thanks very much,
    Willy



  • @willi25 Finally, did you get it fixed? I'm also interested in sending notifications via telegram


Log in to reply