0

I would like to connect to a server using ssh and after logging in I would like to automatically execute

screen -R

My script looks as follows:

sshpass -p password ssh -t name@server.cheese 'screen -R; bash -l'

It is important that I would like to be able to control from the outside which commands are executed after login. Else I could most likely just add stuff the ".bashrc" or some equal file.

6
  • 1
    your query is not quite clear, what is it you are trying to achieve here ?
    – Sagar
    Jul 28 '20 at 7:24
  • The 'screen -R' command retrieves a running screen session to continue it. When I execute the command as above it isn't able to find this screen session and instead starts a new. Still I can kill this session and afterwards access my existing old one. So I assume the reason is the execution order. I would like to know how I can fix my command so that it finds the running screen session bound to my account?
    – baxbear
    Jul 28 '20 at 7:28
  • 1
    screen -ls && screen -R This should do the trick. But this will only work if you have one detached session. may be you can tweak this by wrapping around shell script.
    – Sagar
    Jul 28 '20 at 7:39
  • I tried it and it prints out the detached session of mine but then it ignores the 'screen -R' completely...
    – baxbear
    Jul 28 '20 at 7:56
  • 1
    yeah so the exit code for screen works differently in linux flavour. With ubuntu flavour this would work. Please do check answer posted by me.
    – Sagar
    Jul 28 '20 at 7:58
1
ssh -tt user@server 'screen -ls 2>/dev/null | grep -i detached && screen -r || echo "No screen detached sessions found"'

This would work provided you have one screen detached.

1
  • It does it perfectly, thank you very much!
    – baxbear
    Jul 28 '20 at 8:05
0

check your ssh config? for example:

grep PermitTTY /etc/ssh/config
grep PermitTTY ~/.ssh/config

If they are not set to YES, maybe that's your problem?

PS: any reason to not use tmux instead of screen? Just curious.

2
  • Yeah, tmux isn't installed on the server I am accessing, the admin prefers screen.
    – baxbear
    Jul 28 '20 at 7:47
  • '/etc/ssh/config' as well as '~/.ssh/config' do not exist or I am not able to see them but still get the message that they do not exist
    – baxbear
    Jul 28 '20 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.