Skip to content

Instantly share code, notes, and snippets.

@tkuennen
Created June 25, 2018 15:26
Show Gist options
  • Select an option

  • Save tkuennen/2547f788cdf8925246fb2bc1fdb17475 to your computer and use it in GitHub Desktop.

Select an option

Save tkuennen/2547f788cdf8925246fb2bc1fdb17475 to your computer and use it in GitHub Desktop.
#!/bin/sh
# _____ ______ _ _ ____ ___ ____
# / _ \ \ / / _ \| \ | | ___ ___ _ __ / ___| ___ ___|_ _| _ \
#| | | \ \ / /| |_) | \| |/ __/ _ \| '_ \ _____| | _ / _ \/ _ \| || |_) |
#| |_| |\ V / | __/| |\ | (_| (_) | | | |_____| |_| | __/ (_) | || __/
# \___/ \_/ |_| |_| \_|\___\___/|_| |_| \____|\___|\___/___|_|
# A simple sctipt to email a summary of OpenVPN connections
script="ovpncon"
version="0.1"
run_dir=$(echo "$PWD")
user=$(whoami)
subject='Summary of OpenVPN Connections'
recipient=
logfile=/var/log/openvpnas.log
connections=/tmp/connection.log
echo 'Summary of OpenVPN Connections' > $connections
echo >> $connections
echo ' Web Interface Connections' >> $connections
echo >> $connections
echo '# of connections, ip, city, org, hostname' >> $connections
echo >> $connections
less $logfile |grep 'connection established' |cut -d ' ' -f 17 \
| cut -d ']' -f 2 |cut -d ':' -f 1 |sort -n |uniq -c |sort -nr \
|grep -Ev 'with' |grep -Ev 'established' >> $connections
less $logfile |grep 'connection established' |cut -d ' ' -f 17 \
| cut -d ']' -f 2 |cut -d ':' -f 1 |sort -n |uniq -c |sort -nr \
|grep -Ev 'with' |grep -Ev 'established' |awk -F' ' '{print $2}' >> /tmp/ips.txt
while read line
do
curl -s "http://ipinfo.io/$line" >> /tmp/ipinfo.xml
done < /tmp/ips.txt
while read line
do
grep city |cut -d ':' -f 2 >> /tmp/city.txt
done < /tmp/ipinfo.xml
while read line
do
grep org |cut -d ':' -f 2 >> /tmp/org.txt
done < /tmp/ipinfo.xml
while read line
do
grep hostname |cut -d ':' -f 2 >> /tmp/hostname.txt
done < /tmp/ipinfo.xml
#while read line
#do
# sed '/^/s/$/ , /' >> $connections
#done < /tmp/city.txt
while read line
do
sed '/^/t/$/ /' >> $connections
done < /tmp/city.txt
echo >> $connections
echo >> $connections
echo ' Failed web login attempts' >> $connections
echo ' # of attempts User' >> $connections
echo >> $connections
less $logfile |grep 'Web login authentication failed' |cut -d ' ' -f 22 \
|cut -d ' ' -f 22 | cut -d '}' -f 1 |cut -d ':' -f 1 |sort -n |uniq -c |sort -nr \
|grep -Ev 'to' >> $connections
echo >> $connections
echo ' Successful VPN connections' >> $connections
echo ' # of connections User' >> $connections
echo >> $connections
less $logfile |grep 'AUTH SUCCESS' | cut -d ' ' -f 17 \
|cut -d ',' -f 1 |uniq -c |sort -nr >> $connections
less $connections |mail -s 'Summary of OpenVPN Connections' $recipient
rm $connections
#rm /tmp/ips.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment