Thursday, July 17, 2014

Apple Script - To open iTerm (terminal) tabs and automatically execute commands

The below apple script will launch iTerm (terminal app) and open 4 tabs, each tab is a SSH session to an IP address. After opening 4 SSH session, it will automatically execute certain shell commands in each of those tabs. It will also maximize the terminal window.

tell application "Finder"
    set _b to bounds of window of desktop
end tell

set command1 to "cd /root;source /root/openrc;curl -L http://172.168.1.100/initial_login_setup.sh|bash >/dev/null 2>&1;clear;echo server-1;hostname;"
set command2 to "cd /root;source /root/openrc;curl -L http://172.168.1.100/initial_login_setup.sh|bash >/dev/null 2>&1;clear;echo server-2;hostname;"
set command3 to "cd /root;source /root/openrc;curl -L http://172.168.1.100/initial_login_setup.sh|bash >/dev/null 2>&1;clear;echo server-3;hostname;"
set command4 to "cd /root;source /root/openrc;curl -L http://172.168.1.100/initial_login_setup.sh|bash >/dev/null 2>&1;clear;echo server-4;hostname;"

tell application "System Events"
    set _wasRunning to exists (processes where name is "iTerm")
end tell

tell application "iTerm"
    activate
    if not _wasRunning then
        set _session to current session of current terminal
    else
        set _term to (make new terminal)
        tell _term
            launch session "Default"
            tell the last session
                write text "sshpass -p 'password123' ssh root@192.168.1.55;clear;"
                write text command1
            end tell
            
            launch session "Default"
            tell the last session
                write text "sshpass -p 'password123' ssh root@d192.168.1.56;clear"
                write text command2
            end tell
            
            launch session "Default"
            tell the last session
                write text "sshpass -p 'password123' ssh root@192.168.1.57;clear"
                write text command3
            end tell
            
            launch session "Default"
            tell the last session
                write text "sshpass -p 'password123' ssh root@192.168.1.58;clear"
                write text command4
            end tell
            
            set _session to current session
        end tell
    end if
end tell

tell application "iTerm"
    tell front window
        set {_x, _y, _w, _h} to (get bounds)
        set _vz1 to item 3 of _b
        set _vz2 to item 4 of _b
        set bounds to {_x, _y, _vz1, _vz2}
    end tell
end tell

In the above apple script, I run a script on the remote host immediately after login. It is specific to my network/datacenter. You need not have it.

I do things like:

1: cd /root

2: source /root/openrc (for openstack)

3: Download this scritp: 172.168.1.100/initial_login_setup.sh, and execute it remotely

4: echo the server hostname

set command1 to "cd /root;source /root/openrc;curl -L http://172.168.1.100/initial_login_setup.sh|bash >/dev/null 2>&1;clear;echo server-1;hostname;"
set command2 to "cd /root;source /root/openrc;curl -L http://172.168.1.100/initial_login_setup.sh|bash >/dev/null 2>&1;clear;echo server-2;hostname;"
set command3 to "cd /root;source /root/openrc;curl -L http://172.168.1.100/initial_login_setup.sh|bash >/dev/null 2>&1;clear;echo server-3;hostname;"
set command4 to "cd /root;source /root/openrc;curl -L http://172.168.1.100/initial_login_setup.sh|bash >/dev/null 2>&1;clear;echo server-4;hostname;"

Apple Script : Connect to VPN

tell application "System Events"
 tell current location of network preferences
  set VPN to "Walmart GEC VPN"
  set VPNactive to connected of current configuration of service VPN
  if VPNactive then
   disconnect service VPN
  else
   connect service VPN
  end if
 end tell
end tell