Do you use let's encrypt?





6485 votes ~ 21 comments

 

XML Logo

Posted by lindenle on Mon 9 Jul 2007 at 18:59
Tags: none.
So you just lost your connection to a remote host in the middle of
editing a file with vi. In a past life you would have sworn a blue
streak and perhaps pounded the keyboard, but you are a power user now so
you just reconnect and re-attach your screen session. In fact you are so
happy that you decide to open xeyes just for laughs...but alas the
$DISPLAY environment variable in your screen session no longer matches
the new $DISPLAY variable set by ssh. You could of course set it by
hand to the correct value or even to the display of the actual client
you are connecting from. However thanks to the bash preexec utility
covered here and download-able here we can automate this process.
The first step is to have bash write out a file that can be read by the
preexec command when we log in over ssh. To do this I added the following
lines to my .bashrc file:


# check to see if DISPLAY is set
# if so write it to a file
if test -n "$DISPLAY"; then
echo "$DISPLAY" > .display.screen
fi


So now the information we need is stored in a dot file which we can tell our preexec command to read. In my last entry we had the preexec command set our screen title to the command that was running:


function precmd () {
set_screen_title "{@$(hostname -s) $(basename `pwd -P`)}: $1"
}

function preexec () {
set_screen_title "{@$(hostname -s) $(basename `pwd -P`)}: $1"
}



In this case we will modify the command to also set the DISPLAY
environment variable after setting the window title.


function precmd () {
set_screen_title "{@$(hostname -s) $(basename `pwd -P`)}: $1"
export DISPLAY="$(cat ~/.display.screen)"
}

function preexec () {
set_screen_title "{@$(hostname -s) $(basename `pwd -P`)}: $1"
export DISPLAY="$(cat ~/.display.screen)"
}



So now let's see if it worked. First I log in check the DISPLAY
variable and start a new screen session.


{lindenle@localhost lindenle} [19:50:41]
=>> echo $DISPLAY
localhost:42.0
{lindenle@localhost lindenle} [19:50:48]
=>> screen
------screen starts--------
{lindenle@localhost lindenle} [19:51:00]
=>> echo $DISPLAY
localhost:42.0


We see that of course the screen session has the correct DISPLAY right
now. Now I will log in from another window, check the DISPLAY and
detach/re-attach this session to test the method.


{lindenle@localhost lindenle} [19:52:25]
=>> echo $DISPLAY
localhost:50.0
{lindenle@localhost lindenle}
=>> screen -dr
------screen attaches--------
{lindenle@localhost lindenle}
=>> echo $DISPLAY
localhost:50.0


Looks like it works. In the future I will never have to set the
DISPLAY variable by hand in my screen windows. The only drawback is
that these changes will only take effect on a new screen
session. Which means you will have to exit all your sessions and start
new ones.

 

Posted by lindenle on Sat 23 Jun 2007 at 14:24
Tags: none.
Screen is my new favorite utility for Linux administration, but I found that I wanted it to behave a bit differently. Specifically I wanted the window list to show the directory and command being run in each pseudo terminal.

1. First get the bash prexec utility.

2. Then I added the following to my .bashrc


if  [ -f ${HOME}/bin/preexec.bash ]  &&  [ "$TERM" == "screen" ] ; then
    . ${HOME}/bin/preexec.bash
    function set_screen_title () {
        local title="$1"
        echo -ne '\ek'${1}'\e\\'
    }
    function precmd () {
        set_screen_title "$1"
    }
    
    function preexec () {
        set_screen_title "$1"
    }

    preexec_install
fi



Now when I run screen it lists the full command of what I am running
as the screen name. Sweet!