<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gowtham</title>
    <description>The latest articles on DEV Community by Gowtham (@gowthamkosaraju).</description>
    <link>https://dev.to/gowthamkosaraju</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F451259%2F5490e4aa-e4ca-4ecf-90bc-3db7e942d312.jpeg</url>
      <title>DEV Community: Gowtham</title>
      <link>https://dev.to/gowthamkosaraju</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gowthamkosaraju"/>
    <language>en</language>
    <item>
      <title>Automate VPN connection from the Command Line on Mac OS</title>
      <dc:creator>Gowtham</dc:creator>
      <pubDate>Fri, 13 Aug 2021 03:29:55 +0000</pubDate>
      <link>https://dev.to/gowthamkosaraju/automate-vpn-connection-from-the-command-line-on-mac-os-3i8b</link>
      <guid>https://dev.to/gowthamkosaraju/automate-vpn-connection-from-the-command-line-on-mac-os-3i8b</guid>
      <description>&lt;p&gt;&lt;strong&gt;Background&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At Rocketium, developers have to use two different VPN connections based on what they are working on. We can connect directly from the network tab in the status bar. But developers tend to forget to change VPN before SSH into the machine. They are a couple of ways to solve this. In this post, i will show how to automate VPN connection using OSA Script.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite&lt;/strong&gt;&lt;br&gt;
VPN has to be configured in MacOSX network preference and knowledge on osascript. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s Build&lt;/strong&gt;&lt;br&gt;
First, we connect to our VPN and then SSH into the machine. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Automating VPN connection&lt;/strong&gt;&lt;br&gt;
For this, we are using the apple script. In this script, we tell OSX to connect to System Preferences → Network. Now we will iterate through the pre-configured row and select our &lt;strong&gt;VPN&lt;/strong&gt; and then connect our &lt;strong&gt;VPN&lt;/strong&gt;. Here is the script&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;repeat while true
    tell application "System Events"
        tell application process "System Preferences"
            if not (window 1 exists) then
                tell application "System Preferences"
                    activate
                end tell
                repeat while not (menu bar 1 exists)
                end repeat
                repeat while not (menu "System Preferences" of menu bar 1 exists)
                end repeat
                repeat while not (menu item "Hide System Preferences" of menu "System Preferences" of menu bar 1 exists)
                end repeat
                delay 3
                click menu item "Hide System Preferences" of menu "System Preferences" of menu bar 1
            end if

            click menu item "Network" of menu "View" of menu bar 1

            tell window 1
                repeat while not (rows of table 1 of scroll area 1 exists)
                end repeat
                repeat with current_row in (rows of table 1 of scroll area 1)
                    if value of static text 1 of current_row contains "VPN Name" then
                        select current_row
                        exit repeat
                    end if
                end repeat

                repeat with current_button in (buttons in group 1)
                    if name of current_button is equal to "Connect" then
                        click current_button
                        exit repeat
                    end if
                end repeat
            end tell
        end tell
    end tell
    delay 60
end repeat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;strong&gt;VPN Name&lt;/strong&gt; with your name.&lt;/p&gt;

&lt;p&gt;Now we can put the above script and ssh address into a single utility file to make this process seamless.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ConnectAndSSH.sh&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;osascript -e 'tell app "Terminal" to do script "sleep 25; ssh -i sshPemFile username@ip"';
osascript /Users/gowthamkosaraju/Desktop/VPNAutomationScript.scpt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above script, we said the terminal app to open a new window to sleep for 25 seconds and then SSH into the machine. Meanwhile, our VPNAutomation.scpt file will execute and VPN will be connected.&lt;br&gt;
Since most of the developers work terminal app. It wouldn’t be amazing if we can do all these through a single command? &lt;/p&gt;

&lt;p&gt;Let us use our favorite alias commands to do it. Go to the terminal app and type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vim ~/.zshrc 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alias connectVPN=”/Users/gowthamkosaraju/Desktop/ConnectAndSSH.sh”
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reload your terminal app to make these changes effect. Now if you type connectVPN. You will connect to VPN and ssh into your machine with a single command.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
