Open tmux profile when VPN comes up if not open

16 views
Skip to first unread message

Jeffrey Honig

unread,
Apr 1, 2022, 9:47:03 AM4/1/22
to iterm2-discuss
When my work VPN connection comes up, I would like to start tmux via a profile if it is not already connected.

I have a launch agent script that runs when the VPN status changes and if the VPN is up I would like to ensure a specific tmux connection is up.  However there are instances where the tmux connection may already be up, such as a VPN drop that is short enough not to break the existing connection.

I use tmux to different hosts, so I need a way to figure out if the specific tmux connection is up.  

I'm currently blindly launching the profile with this tmux session via AppleScript, but should convert this to python.

Could someone point me in the right direction?

Thanks.

Jeff

George Nachman

unread,
Apr 3, 2022, 2:09:05 PM4/3/22
to iterm2-...@googlegroups.com
Use the Python API. It has limited support for tmux integration but it just might be enough for what you want.

Here are some helpful links:



--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/iterm2-discuss/e37718f0-ee4e-46b3-add1-12ae0ab95e1fn%40googlegroups.com.

Jeffrey Honig

unread,
Apr 3, 2022, 4:02:58 PM4/3/22
to iterm2-...@googlegroups.com
Using the Python API I found several methods of determining that A tmux session was active, but no easy way to match it back to a profile.

The best I can come up with is using -s SESSIONNAME on my tmux command in the profile definition and scanning profile definitions to find those.  Then I can issue a tmux command to list active sessions at compare name, like this:

    tmux_conns = await iterm2.async_get_tmux_connections(connection)
    logging.debug("%d Tmux connections", len(tmux_conns))

    found_tmux = False
    for tmux_conn in tmux_conns:
        sessions = await tmux_conn.async_send_command("list-sessions -F '#{session_name}'")
        logging.debug("sessions: %s", sessions)
        for session in sessions.split('\n'):
            if session == profile_session_name:
                logging.debug("found session: %s", session)
                found_tmux = True
                break

    if not found_tmux:
        logging.debug("Opening profile: %s", options.profile)
        await app.windows[0].async_create_tab(options.profile)
 


You received this message because you are subscribed to a topic in the Google Groups "iterm2-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/iterm2-discuss/K8_q7_TESRU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to iterm2-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/iterm2-discuss/CAB5Rqo%3Do1tFvU4cOxFWo%3Dm_jX47k8QKa2xN32JN2_NAsRVR0sQ%40mail.gmail.com.

George Nachman

unread,
Apr 3, 2022, 5:28:23 PM4/3/22
to iterm2-...@googlegroups.com
I’m not sure if it’s sufficient to solve your problem but each session that belongs to tmux integration has variables giving its tmux window pane. That’ll tell you whether it is a tmux session, although it’s not enough info to guess which tmux connection it belongs to if there are multiple. The test is:

await session.async_get_variable(“tmuxWindowPane”) != None

What would the ideal API look like to you?

Jeffrey Honig

unread,
Apr 3, 2022, 6:00:36 PM4/3/22
to iterm2-...@googlegroups.com
After getting familiar with the Python API again, I think what I’m looking for is a way to get from a tux connection (as returned by iterm2.async_get_tmux_connections(connection)) back to the name of the profile that launched it.  I poked around looking for something, but couldn’t find it.  Does that exist?

I reconfigured so that each profile that launches tmux uses a unique session name, so the code currently does what I want.  It would just be nicer if I didn’t have to maintain the extra level of unique session names.

George Nachman

unread,
Apr 3, 2022, 7:37:26 PM4/3/22
to iterm2-...@googlegroups.com
TmuxConnection has owning_session that gives you the Session in which `tmux -CC` was run. The tmux profile will be approximately equal to its profile. So:

iterm2.async_get_tmux_connections(connection)[I].owning_session.async_get_profile()

The font size could be different in tmux client windows, and in the nightly build the key bindings might also differ.

If you have it set to use the “tmux” profile then it’s a different story—just get the profile named “tmux”. You can test for this with async_get_preference(iterm2.PreferenceKey.USE_TMUX_PROFILE)

Jeffrey Honig

unread,
Apr 3, 2022, 8:16:48 PM4/3/22
to iterm2-...@googlegroups.com
owning_session is None when I look at it on Build 3.5.0beta5:

sessions: cloud-jump-nym2-13353-p-mac
> /Volumes/Xandr/bin/iterm2_jump(112)work()
-> for session in sessions.split('\n'):
(Pdb) str(tmux_conn.owning_session)
'None'
(Pdb) l
107
108         found_tmux = False
109         for tmux_conn in tmux_conns:
110             sessions = await tmux_conn.async_send_command("list-sessions -F '#{session_name}'")
111             logging.debug("sessions: %s", sessions)
112 B->         for session in sessions.split('\n'):
113                 if session == profile_session_name:
114                     logging.debug("found session: %s", session)
115                     found_tmux = True
116                     break
117
(Pdb)

George Nachman

unread,
Apr 5, 2022, 12:54:47 AM4/5/22
to iterm2-...@googlegroups.com
This happens if the session is buried. That’s a bug. Get version 2.3 of the iterm2 module and it should work now.

Jeffrey Honig

unread,
Apr 5, 2022, 9:53:57 AM4/5/22
to iterm2-...@googlegroups.com
That helped me move forward.  Unfortunately, Automatic Profile Switching has changed the profile.  Is there a way to disable APS from switching away from the profiles I use to launch tmux?

Reply all
Reply to author
Forward
0 new messages