What's new

Notification on OpenVPN client connection?

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

BGood

Regular Contributor
Is there any way I could get a push notification to my cell phone or email every time a remote client connects to my OpenVPN server on my router? It looks like they get logged, but I'd like something more immediate.
 
I'm pretty sure I remember something like this being discussed in the past using OpenVPN's client-connect option.
 
Use this to call your "push" script.
Code:
client-connect /jffs/scripts/server.sh
client-disconnect /jffs/scripts/server.sh
 
Last edited:
@octopus So, being a total noob to jffs and scripts, I have to put your code into a script saved to jffs. Then I need to also find and save the "wan-event' dynamic script, and also save and configure "pushover" as well?
 
Use this to call your "push" script.
Code:
client-connect /jffs/scripts/server.sh
client-disconnect /jffs/scripts/server.sh

So I've been dabbling with this very occasionally over the past few months, but I'm still lost. I have managed to set up WinSCP and I can see the /jffs/scripts folder. I signed up for Pushbullet.

I've also looked at the scripts @octopus linked.

So do I create a file in /jffs/scripts called openvpn-event and inside that file I put:
client-connect /jffs/scripts/notifyme.sh
client-disconnect /jffs/scripts/notifyme.sh


Then I make another file called notifyme.sh with this code:
#!/bin/sh
pushbullet_token="" # Your access token here (https://docs.pushbullet.com/)

pushbullet_message () {
text="OpenVPN event"
title="$USER@$HOSTNAME"
curl -s -u $pushbullet_token: -X POST https://api.pushbullet.com/v2/pushes --header 'Content-Type: application/json' --data-binary '{"type": "note", "title": "'"$title"'", "body": "'"$text"'"}' >/dev/null 2>&1
}

pushbullet_message


I changed the notifyme.sh to make it executable, but from Putty I went to /jffs/scripts and entered:
notifyme.sh

and I got back
-sh: notifyme.sh: not found

So I'm stuck.
 
Try with full path: /jffs/scripts/notifyme.sh

This should be in vpnserver custom config
Code:
client-connect /jffs/scripts/notifyme.sh
client-disconnect /jffs/scripts/notifyme.sh
 
@octopus THANK YOU! I don't know how long it would have taken me to think of putting your code into the vpnsever custom config! And also using the full path. It works! :) MUCH APPRECIATED!
 
So I found something interesting. In tweaking my Pushbullet message, I made an error such that the script didn't work. My VPN client was then not able to connect.

Does this mean that if anything goes wrong, my VPN clients will fail to connect? For example, if I hit my 100 SMS messages in a month (free version of Pushbullet)? Is there a way to tell the VPN to continue on errors?
 
So I found something interesting. In tweaking my Pushbullet message, I made an error such that the script didn't work. My VPN client was then not able to connect.
My noob guess is client-connect /jffs/scripts/notifyme.sh has an exit code of 1 from the pushbullet command which borks the VPN

If it were me I would try creating a separate caller script /jffs/scripts/vpnconnect.sh and within it call your /jffs/scripts/notifyme.sh & in the background
 
@Maverickcdn Interesting thought. I tried it out, but couldn't get OpenVPN to start. Maybe it's trying to load vpnconnect.sh, but once those are put inside a script, "client-connect" isn't an acceptable Linux command is it? Could it need

openvpn client-connect /jffs/scripts/notifyme.sh


But I would think a path would be needed?

I think you're right about the exit code needing to be 0.
 
@Maverickcdn OK, I think I misunderstood what you were suggesting. I went back and did in my server configuration:
client-connect /jffs/scripts/vpnconnect.sh
client-disconnect /jffs/scripts/vpnconnect.sh


Then inside vpnconnect.sh:
#!/bin/sh
/jffs/scripts/notifyme.sh &


Then inside notifyme.sh:
#!/bin/sh
pushbullet_token="{token redacted}" # Your access token here (https://docs.pushbullet.com/)

pushbullet_message () {
text="$script_type $username $time"
title="AsusRouter"
curl -s -u $pushbullet_token: -X POST https://api.pushbullet.com/v2/pushes --header 'Content-Type: application/json' --data-binary '{"type": "note", "title": "'"$title"'", "body": "'"$text"'"}' >/dev/null 2>&1
}

pushbullet_message


It seems that even if my pushbullet syntax gets borked, it does *not* stop the client from connecting. So, I think I'm good now. THANK YOU!!
 
You might not even need a separate caller script like I mentioned earlier, might just need to put your pushbullet command in the background inside /jffs/scripts/notifyme.sh by adding & to the end or having an explicit exit 0 after the pushbullet command.

Code:
#!/bin/sh
pushbullet_message() {
...
}
pushbullet_message &

or

#!/bin/sh
pushbullet_message(){
...
}
pushbullet_message
exit 0
 
Thanks, for the additional post, #13. But I now think it's working fine as I explained in #12. Maybe it's overly complex as I did it, but I did test and the client connects even if the pushbullet script has an error. So I think I'll leave well enough alone since I'm such a noob I can only make it worse at this point.
 

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top