0

My problem is a bit similar to one described here, but not the same. I cannot use sudo on external servers S1 / S2 / S3 (to install packages for example) unless they are binaries.

Each day I log onto the terminal locally, and run:

  1. type/run initialization command C1 locally
  2. connect to server S1 by ssh using alias1 (from .bashrc locally)
  3. connect to server S2 from S1 using alias2 (from .bashrc in S1)
  4. connect to server S3 from S2 using alias3 (...)
  5. type/run initialization command C2 in S3
  6. run "byobu" in S3, and then I do normal work in byobu.

I want to have a script, which does all those steps and leaves the terminal with byobu opened, cause I feel it is stupid, to type all those commands every day, but don't know how to change it :)

my try:

#!/bin/bash
ssh -C2qtnN -D1001 S1 & #C1 which is tunel for the browser
ssh S1
ssh S2 # here it fails, trying to execute "ssh S2" locally
ssh S3 # same as above
C2 # initialization command that has to be run before byobu on S3
byobu #this one wont work either - to open byobu ;S
Yurkee
  • 795
  • 2
  • 9
  • 23
  • Can you please show us what code you have written already and how it fails to do what you want? – Corion Nov 05 '18 at 14:53
  • sure, but it looks pathetic! – Yurkee Nov 05 '18 at 14:58
  • 1
    There are some good opportunities for learning some new skills here, but this question is off-topic for Stack Overflow which is for software development questions. You should consider asking it on [superuser](https://superuser.com) instead (or possibly https://unix.stackexchange.com). You probably want to read up on ssh jump hosts; one article is [here](https://wiki.gentoo.org/wiki/SSH_jump_host) but there are many others. – larsks Nov 05 '18 at 16:02
  • You will need `HERE` docs - try the solution in the answer to this question: https://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-a-shell-script-on-a-remote-machine – itChi Nov 05 '18 at 16:03
  • The biggest thing you want to use is the `-J` option: `ssh -JS1,S2 S3 'C2 && byobu` should take care of all but the first line of your script. – chepner Nov 05 '18 at 16:36

1 Answers1

0

I have found a working solution:

The script ssh_teleport.sh looks like this:

#!/bin/bash
ssh -C2qtnN -D1001 S1 &                                                                                                                                                          
echo "tunnel ready!"                                                                                                                                                                                       
ssh -tt $1 'bash -l -c "C2"'

Where $1 can be any host from {S1, S2, S3} and C2 is a full command(aliases, unfortunately, don't work) to run on ssh given in $1

In order to jump (thanks to @chepner) I had to modify config in .ssh on my local machine, the way it is described here:

Host S1
    HostName IP_of_S1
    User MyUsername1

Host S2
    HostName someHostname1
    User MyUsername2
    ProxyJump S1

Host S3
    HostName someHostname2
    ProxyJump S2
    User MyUsername3

The final touch is to add an alias to .bashrc:

alias teleportS3='./scripts/ssh_teleport.sh S3'

It does its purpose!

Yurkee
  • 795
  • 2
  • 9
  • 23