Skip to content

Instantly share code, notes, and snippets.

@jrbenito
Last active January 26, 2017 18:55
Show Gist options
  • Save jrbenito/259e4030dce480e8a3e7d24e219175aa to your computer and use it in GitHub Desktop.
Save jrbenito/259e4030dce480e8a3e7d24e219175aa to your computer and use it in GitHub Desktop.
Set display variable when reconnect to a tmux terminal on a server with many users (display can change on each ssh section)
function setdisplay ()
{
USERID=$(id -u);
# list all connections by numeric (don´t wait slow DNS)
# and filter by current user´s listening ports on localhost
# that that is in the range of 6000 ~ 6999 (generally X ports)
PORTA=$(netstat -ane|grep "LISTEN "|grep "$USERID"|sort -k8n|sed -n -e "s/.*127\.0\.0\.1:6.\(..\) .*/\1/p"|tail -n 1);
export DISPLAY="localhost:$PORTA.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment