{"id":21,"date":"2020-07-03T23:04:39","date_gmt":"2020-07-03T23:04:39","guid":{"rendered":"https:\/\/blog.sbaranidharan.online\/?p=21"},"modified":"2020-07-09T16:02:04","modified_gmt":"2020-07-09T16:02:04","slug":"applescript-to-automatically-connect-to-cisco-anyconnect-vpn","status":"publish","type":"post","link":"https:\/\/blog.sbaranidharan.online\/index.php\/2020\/07\/03\/applescript-to-automatically-connect-to-cisco-anyconnect-vpn\/","title":{"rendered":"AppleScript to automatically connect to Cisco Anyconnect VPN!"},"content":{"rendered":"\n<div class=\"post-views post-21 entry-meta\">\r\n\t\t\t\t<span class=\"post-views-icon dashicons dashicons-chart-bar\"><\/span>\r\n\t\t\t\t<span class=\"post-views-label\">Post Views: <\/span>\r\n\t\t\t\t<span class=\"post-views-count\">1,045<\/span>\r\n\t\t\t<\/div>\n\n\n\n<p>We can connect to Cisco AnyConnect VPN without providing the password and Accept each time.<\/p>\n\n\n\n<p>In my case, I&#8217;ve used Exchange. Microsoft Outlook (Exchange) uses the same password as I used for VPN. In Mac OS, we can retrieve this Exchange password from the keychain access app through \/usr\/bin\/security.<\/p>\n\n\n\n<p class=\"has-text-align-left\">But to access the keychain through security first we need to allow the app to access it. To do it, when we first run this app it will asks for the password to access the Exchange&#8217;s keychain. Once we entered the password we need to click &#8220;Always allow&#8221; instead of Allow (by clicking Always allow it will add entry in Access Control for security). Otherwise it will ask for the password again. We can verify if only these two applications are allowed to access the keychain by navigating to keychain access app and select Exchange -&gt; GetInfo -&gt; Access Control -&gt; where you should see only two entries (Outlook and Security) as follows<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"672\" src=\"https:\/\/blog.sbaranidharan.online\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-07-06-at-3.48.02-PM-1024x672.png\" alt=\"\" class=\"wp-image-26\" srcset=\"https:\/\/blog.sbaranidharan.online\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-07-06-at-3.48.02-PM-1024x672.png 1024w, https:\/\/blog.sbaranidharan.online\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-07-06-at-3.48.02-PM-300x197.png 300w, https:\/\/blog.sbaranidharan.online\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-07-06-at-3.48.02-PM-768x504.png 768w, https:\/\/blog.sbaranidharan.online\/wp-content\/uploads\/2020\/07\/Screen-Shot-2020-07-06-at-3.48.02-PM.png 1066w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;apl&quot;,&quot;mime&quot;:&quot;text\/apl&quot;,&quot;theme&quot;:&quot;default&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;APL&quot;,&quot;modeName&quot;:&quot;apl&quot;}\">on run {input, parameters}\n\tset vpn to do shell script &quot;\/opt\/cisco\/anyconnect\/bin\/vpn status | grep -q 'Connected' &amp;&amp; echo 'connected' || echo 'disconnected'&quot;\n\tif (vpn is &quot;connected&quot;) then\n\t\tdo shell script &quot;\/opt\/cisco\/anyconnect\/bin\/vpn disconnect&quot;\n\t\tsay &quot;VPN is disconnected!&quot;\n\t\tdisplay dialog &quot;VPN is disconnected!&quot; with icon caution buttons {&quot;OK&quot;} default button &quot;OK&quot;\n\telse if (vpn is &quot;disconnected&quot;) then\n\t\tset username to do shell script &quot;whoami&quot;\n\t\tset exchangepasskey to do shell script &quot;\/usr\/bin\/security find-generic-password -wl Exchange&quot;\n\t\tset vpnnames to {&quot;alias-1&quot;, &quot;alias-2&quot;, &quot;alias-4&quot;, &quot;alias-4&quot;}\n\t\tset vpnalias to {choose from list vpnnames}\n\t\tset output to do shell script &quot;printf '&quot; &amp; username &amp; &quot;\\\\n&quot; &amp; exchangepasskey &amp; &quot;\\\\ny' | \/opt\/cisco\/anyconnect\/bin\/vpn -s connect &quot; &amp; vpnalias\n\t\tif (output contains &quot;state: Connected&quot;) then\n\t\t\tsay &quot;VPN is connected successfully!&quot;\n\t\t\tdisplay dialog &quot;VPN is connected successfully!&quot; with icon 1 buttons {&quot;OK&quot;} default button &quot;OK&quot;\n\t\telse\n\t\t\tdo shell script &quot;ps -ef | grep 'cisco' | awk '{print $2};' | xargs kill -9 &gt; \/dev\/null 2&gt;&amp;1&quot;\n\t\t\tsay &quot;Error connecting VPN, try again! I have killed the running instances&quot;\n\t\t\tdisplay dialog &quot;Error connecting VPN, try again!&quot; with icon stop buttons {&quot;OK&quot;} default button &quot;OK&quot;\n\t\tend if\n\tend if\n\treturn input\nend run<\/pre><\/div>\n\n\n\n<p>We also killed the running instance of VPN, in case of any error happened so that when we run the app next time, it will not throw any error.<\/p>\n\n\n\n<p>If you need it as an executable app, just contact me.<\/p>\n\n\n\n<p>Just extract the zip and copy the VPN.app to Applications folder. Now you got the app installed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can connect to Cisco AnyConnect VPN without providing the password and Accept each time. In my case, I&#8217;ve used Exchange. Microsoft Outlook (Exchange) uses the same password as I used for VPN. In Mac OS, we can retrieve this Exchange password from the keychain access app through \/usr\/bin\/security. But to access the keychain through [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[6],"tags":[2,3,4,5],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/posts\/21"}],"collection":[{"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":12,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":49,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/posts\/21\/revisions\/49"}],"wp:attachment":[{"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sbaranidharan.online\/index.php\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}