Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: prompt and ssh

On May 22,  3:08am, Vincent Lefevre wrote:
} > if grep -q $SSH_CONNECTION[(w)1]\:$SSH_CONNECTION[(w)2] =(netstat -na)
} > then
} > else
} >     print Invalid SSH_CONNECTION
} > fi
} > 
} > This "fails" only if you disconnect from screen but leave ssh connected.
} This won't work in my case, because I sometimes do the following:
} 1. On my machine at home, start ssh connections to my machine at work.
} 4. Resume the screen session at work (but the ssh connections started
}    in (1) are still there).

Yes, this is exactly what I meant.  However, this ...
} I also do the following:
} 1. Start screen remotely (via ssh).
} 2. Detach the screen session.
} 3. Resume the screen session from a different ssh connection (e.g.
}    because the first one has died -- BTW, this is even one of the
}    reasons of using screen).

... is one of the situations the netstat test is meant to discover.

However, if you can load the correct settings from an external file
at will, you probably don't need to bother checking for whether the
existing settings are still valid.  Just always clobber them.

} >   screen() {
} > 	typeset -pm SSH_\* > ~/.screen_SSH
} > 	screen "$@"
} >   }

Oops, that second line should say: command screen "$@"

} >   screen_ssh_precmd() {
} > 	source ~/.screen_SSH
} >   }
} >   precmd_functions+=(screen_ssh_precmd)
} One should also find a way to make it work with several screen
} sessions (in screen, that can be identified by $STY, so that the
} filename could be based on it).

That's a bit problematic, because the file has to be written by the
shell "outside" the screen session, but $STY is only available to
the shell "inside" screen.  That is, $STY doesn't exist yet at the
point when you need to write the file.  This is almost analogous to
the X-session geometry-specific-zsh-startup discussion that went on
several months ago.

There is a workaround:

    screen -S sessionname

In at least some versions of screen, you can also do

    screen -R sessionname

and screen will look for a session on PID.sessionname and if it does
not find it, it'll behave as if you passed the -S option.  I don't know
if that's really intended to work, as it's not documented, so use at
your own risk.

  screen() {
    local -a sty
    # Note: We claim the argument to -R is mandatory because we want
    # zparseopts to fail here if no sessionname argument is provided.
    zparseopts R:=sty S:=sty && [[ $sty[2] != R ]] &&
    { print "unset -m 'SSH_*'"
      typeset -pm SSH_\*
    } > ~/.screen_SSH_$sty[2]
    command screen "$@"
    if [[ -n $sty ]]
      grep -q "[0-9]\.$sty[2]" <(command screen -ls $sty[2]) ||
      rm ~/.screen_SSH_$sty[2]
  screen_ssh_precmd() {
    [[ -z $STY ]] && return
    local sty=~/.screen_SSH_$STY:e
    if [[ -f $sty ]]
      source $sty
      { print "unset -m 'SSH_*'"
        typeset -pm SSH_\*
      } > $sty

That requires that (in the calling login shell) you always provide the
sessionname argument, because if you use just -R or -RR the caller doesn't
know what file to write.  Also it's possible to create more than one
session with the same name, which makes it a bit preferable to use -R
rather than -S.

} When resuming a screen session, one should detect
} which one, and use a filename corresponding to this session.

I don't know any good way to do that from "outside" of screen.  Maybe
something involving using the -X option could be hacked up.

} When screen terminates, one should check if the session still exists
} (attached or detached), and if it isn't, remove the file.

It's unfortunate that screen -ls exits with nonzero status even if it
finds a matching session.

Messages sorted by: Reverse Date, Date, Thread, Author