{"id":3663,"date":"2017-01-24T23:49:45","date_gmt":"2017-01-24T23:49:45","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3663"},"modified":"2017-10-19T03:24:52","modified_gmt":"2017-10-19T03:24:52","slug":"code-detecting-if-a-vpn-connection-is-active-programatically-jan2017","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/code-detecting-if-a-vpn-connection-is-active-programatically-jan2017\/","title":{"rendered":"[CODE] Detecting if a VPN connection is active programmatically (Jan\/2017)"},"content":{"rendered":"<p>This method of programatic detection works as of January 24, 2017 with the latest versions of Express VPN and PIA (Private Internet Access)<\/p>\n<p><strong>On Mac OSX\/<\/strong><br \/>\nThis works for Mac OSX 10.2.2.<\/p>\n<p>The trick is to request your ip routing table and examine through which network interface your default traffic is going through.<br \/>\n(To do this programmatically you will have to parse the output with your favorite programming language)<\/p>\n<p>This is how it looks for both ExpressVPN and PIA when the VPN is active:<\/p>\n<p>To request your routing table you can do this on the command line:<br \/>\n<code>netstat -nr<\/code><\/p>\n<p>Notice the line starting with &#8220;0\/1&#8221;, it&#8217;s going through that tunnel interface. (In Linux it would show <code>0.0.0.0<\/code> instead of <code>0\/1<\/code>)<\/p>\n<p>VPN ON output in Mac<\/p>\n<pre><code>Internet:\nDestination        Gateway            Flags        Refs      Use   Netif Expire\n0\/1                10.81.10.5         UGSc            5        0   utun1\n...\n<\/code><\/pre>\n<p>VPN ON output in Linux<\/p>\n<pre><code>Kernel IP routing table\nDestination     Gateway         Genmask         Flags   MSS Window  irtt Iface\n0.0.0.0         10.31.10.5      128.0.0.0       UG        0 0          0 tun0\n...\n<\/code><\/pre>\n<p>When you turn VPN off this is how it looks:<\/p>\n<p>VPN OFF in Mac<\/p>\n<pre><code>Destination        Gateway            Flags        Refs      Use   Netif Expire\ndefault            192.168.1.1        UGSc           66        0     en0\n127                127.0.0.1          UCS             2        4     lo0\n<\/code><\/pre>\n<p>VPN OFF in Linux<\/p>\n<pre><code>Kernel IP routing table\nDestination     Gateway         Genmask         Flags   MSS Window  irtt Iface\n0.0.0.0         172.16.245.2    0.0.0.0         UG        0 0          0 eth0\n<\/code><\/pre>\n<p>So a quick way to determine if the VPN is on or off in Mac or Linux, is to filter-out what you care for using <code>grep<\/code>.<br \/>\nIf you have any output it&#8217;s on, if not it&#8217;s off<\/p>\n<p><strong><code>netstat -nr | egrep \"^0\" | grep \"tun\"<\/code><\/strong><\/p>\n<p>(we filter for &#8220;tun&#8221; and not utun1, as in linux vpn network interfaces start with &#8220;tun&#8221;)<\/p>\n<p>Parse the output of that command and you will have your VPN status. No output means VPN is disconnected. Some output means the VPN is connected.<\/p>\n<p><strong>On Windows<\/strong><br \/>\nDo a <code>nestat -nr<\/code> and look for <code>128.0.0.1<\/code>, if you find it, VPN is on.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This method of programatic detection works as of January 24, 2017 with the latest versions of Express VPN and PIA (Private Internet Access) On Mac OSX\/ This works for Mac OSX 10.2.2. The trick is to request your ip routing table and examine through which network interface your default traffic is going through. (To do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[1572,635,1571,1570,1569],"class_list":["post-3663","post","type-post","status-publish","format-standard","hentry","category-code","tag-expressvpn","tag-macosx","tag-pia","tag-private-internet-access","tag-vpn"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-X5","jetpack-related-posts":[{"id":4129,"url":"https:\/\/www.gubatron.com\/blog\/introducing-uninews-a-universal-news-scraper-in-rust\/","url_meta":{"origin":3663,"position":0},"title":"Introducing Uninews: A Universal News Scraper in Rust","author":"gubatron","date":"February 17, 2025","format":false,"excerpt":"The internet is overflowing with news, but extracting clean, readable content from articles can be a tedious task. Whether you're aggregating news for personal consumption, research, or AI training, automating this process is a must. Enter Uninews, a powerful, lightweight, and efficient Rust-based news scraper that simplifies content extraction and\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2025\/02\/gubatron_Logo_for_Uninews_a_universal_news_scraper_command_li_4f376071-18e3-400e-9644-8efc878465e4_3.png?fit=1024%2C1024&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":741,"url":"https:\/\/www.gubatron.com\/blog\/how-to-reset-your-joost-profile-on-mac-osx\/","url_meta":{"origin":3663,"position":1},"title":"How to reset your Joost profile on Mac OSX","author":"gubatron","date":"March 19, 2008","format":false,"excerpt":"I've noticed Joost tends to not refresh the content of their channel Line up unless it resets its User Profile. I'm not sure if this trick works on Windows, but it certainly does work for Mac. (Please leave a comment if it works the same way on windows, I don't\u2026","rel":"","context":"In &quot;Joost&quot;","block_context":{"text":"Joost","link":"https:\/\/www.gubatron.com\/blog\/category\/joost\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1208,"url":"https:\/\/www.gubatron.com\/blog\/mac-osx-terminal-to-slow-to-open\/","url_meta":{"origin":3663,"position":2},"title":"Mac OSX Terminal too slow to open?","author":"gubatron","date":"April 3, 2009","format":false,"excerpt":"Try this: rm ~\/.fonts.cache-1 Open a new Terminal, it should open pretty fast after that.","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3364,"url":"https:\/\/www.gubatron.com\/blog\/new-frostwire-5-7-7-available-for-windows-mac-and-linux-contributors-now-earn-bitcoins-instantaneously\/","url_meta":{"origin":3663,"position":3},"title":"New FrostWire 5.7.7 available for Windows, Mac and Linux. Contributors now earn bitcoins instantaneously.","author":"gubatron","date":"October 2, 2014","format":false,"excerpt":"Download FrostWire 5.7.7 for Windows (Bitcoin enabled .torrent) Download FrostWire 5.7.7 for MacOSX (.torrent) Download FrostWire 5.7.7 for Debian\/Ubuntu (.torrent) This update focuses on fixing multiple user interface issues, mostly related to the media player. Libraries were updated, a nasty freeze when opening FrostWire out of a magnet link has\u2026","rel":"","context":"In &quot;Free Software&quot;","block_context":{"text":"Free Software","link":"https:\/\/www.gubatron.com\/blog\/category\/free-software\/"},"img":{"alt_text":"Screen Shot 2014-10-02 at 9.50.11 AM","src":"https:\/\/frostwire.files.wordpress.com\/2014\/10\/screen-shot-2014-10-02-at-9-50-11-am.png?w=350&h=200&crop=1","width":350,"height":200},"classes":[]},{"id":2186,"url":"https:\/\/www.gubatron.com\/blog\/new-location-of-src-jar-for-jdk-1-6-0_24-on-mac-os-x\/","url_meta":{"origin":3663,"position":4},"title":"New Location of src.jar for JDK 1.6.0_24 on Mac OS X","author":"gubatron","date":"March 5, 2011","format":false,"excerpt":"These Apple devs... why do you keep moving src.jar? Here's the new location: [bash]\/Library\/Java\/JavaVirtualMachines\/1.6.0_24-b07-330.jdk\/Contents\/Home\/src.jar[\/bash] Not there! If you don't see src.jar there, and you're running Mac OSX 10.6, and you did download the Java system update, you will have to go to developer.apple.com and download the \"Java for Mac OS\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":649,"url":"https:\/\/www.gubatron.com\/blog\/tecnotulia-podcast-mac-osx-vs-linux\/","url_meta":{"origin":3663,"position":5},"title":"TecnoTulia Podcast: Mac OSX vs Linux","author":"gubatron","date":"January 30, 2008","format":false,"excerpt":"Hoy tengo el honor de salir en el primer podcast de TecnoTulia, hablando con JM sobre las diferencias, similitudes, ventajas entre Mac OSX y Linux. TecnoTulia es uno de los podcasts que forman parte de una nueva iniciativa llamada OyeEsto Labs.","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3663"}],"version-history":[{"count":10,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3663\/revisions"}],"predecessor-version":[{"id":3677,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3663\/revisions\/3677"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}