1

I have multiple screen sessions running on one of my server (say session_read, session_prod..)

Currently I specify the screen session after I have sshed into my server.

Is there a way to specify the screen session while I am trying to ssh in?

Something like

ssh user@servername screen_session_name
Anshul Goyal
  • 416
  • 1
  • 8
  • 18

2 Answers2

1

You need to allocate a TTY, or SSH will complain:

$ ssh host screen
Must be connected to a terminal.

Assuming the session is already disconnected:

$ ssh -t user@host screen -r session

otherwise a suitable combination of -d, -r, -D, -R, -x per the man page.

Andrew
  • 8,002
  • 3
  • 36
  • 44
1

I use a custom hardstatus line in ~/.screenrc:

hardstatus alwayslastline "%{wk}%-w%{Gk}[%n %t]%{wk}%+w%=%{Ck}%d %M %Y %c:%s"

combined with a little shell script:

#!/usr/bin/env bash
show_help() {
  printf "Usage: con hostname\n"
  exit 0
}

[[ "$#" -eq 1 ]] || show_help

uppercase="${1^^}"
lowercase="${1,,}"

screen -t "${uppercase}" -T screen-256color ssh "${lowercase}"

exit 0

Put it somewhere in your PATH, and you only need to call it:

$ my_script hostname

and a new screen session will be allocated with the title set to the hostname you are connecting to.

You can provide further customizations via ~/.ssh/config settings.

dawud
  • 15,096
  • 3
  • 42
  • 61