Jump to content

Keyword workflow to open several SSH sessions panes in iTerm


Recommended Posts

Hi,

I'm trying to do something that maybe a bit excessive, but I'll try my luck here.

In my day to day work I need to open several SSH connections to 5-7 different servers, and navigate in each server to the same place usually.

This task takes a lot of time, I need to go one by one and open each server.

I wonder if there is an option to do it all at once using Alfred workflow.

I want to write one keyword and to have this open:

 

8n6Jy0Y.png

 

Note that all panes already broadcast input to each other, so I will only need to type the password once.

 

Is there any way to do that?

 

 

Thanks on advance,

Avishay

Link to comment
On 9/13/2020 at 9:17 AM, Avishay Guttman said:

iTerm script or shell script?

 

I presume you will need an iTerm script to open the splits (unless you're using tmux).

 

On 9/13/2020 at 9:17 AM, Avishay Guttman said:

Do you have any getting started etc? (I know this is not related directly to Alfred, but if we are already talking about this and you may have done or saw something similar)

 

Me, no. I don't do any iTerm scripting.

Link to comment
  • 1 year later...

set hostnames to {"pi@host1.local", "pi@host2.local", "pi@host3.local", "pi@host4.local", "pi@host5.local", "-p 12345 gregorio@host12"}

if application "iTerm" is running then
        tell application "iTerm"
                create window with default profile
                tell current tab of current window
                        select
                        tell current session

                                -- make the window fullscreen
                                tell application "System Events" to key code 36 using command down
                                split horizontally with default profile

                                set num_hosts to count of hostnames
                                repeat with n from 1 to num_hosts
                                        if n - 1 is (round (num_hosts / 2)) then
                                                -- move to lower split
                                                tell application "System Events" to keystroke "]" using command down
                                        else if n > 1 then
                                                -- split vertically
                                                tell application "System Events" to keystroke "d" using command down
                                        end if
                                        delay 1
                                        write text "ssh " & (item n of hostnames)
                                end repeat
                        end tell
                end tell
        end tell
else
        activate application "iTerm"

end if

Link to comment

If you use SSH all the time, tmux will change your life.

 

I keep between 4 and 10 SSH sessions going all the time.  But my computer isn't always connected to them.  I run tmux on an internal server at work.  I SSH or MOSH to the internal server.  Then run "tmux attach" to access my tmux session.  Then I can see all of my SSH sessions, exactly like I left them.  Tmux supports splits as well, so you can have your stacked layout if that's what you really like.  I find it easier to navigate from session to session with tmux commands, but you could do either, or a combination.

 

The key point here is you don't need to keep opening many sessions.  Tmux will keep them open for you.  ...and it will keep you exactly where you were when you last saw them.  Even if you were running an interactive program like top or vim, or a compile or a shell script.  Tmux keeps every session alive.  When you get back to work and attach, you see the current state.  This is also great for working remotely or changing work sites.  All of your stuff is always in the same place, all ready to go.

 

Brian.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...