36

Picture a scenario where I'm logged into a server (which we'll call "Wallace") from my local machine, and from there I ssh into another server (which we'll call "Gromit"):

laptop ---ssh---> Wallace ---ssh---> Gromit

Then the ssh session from Wallace to Gromit hangs, and I want to kill it. If I enter ~. to kill ssh, it kills the ssh session from my laptop to Wallace, because the ~ is intercepted by that ssh session, and the . is taken as a command to kill the session. How do I send a command to the ssh session between Wallace and Gromit? How do I kill my "inner" ssh?

iconoclast
  • 1,800
  • 2
  • 18
  • 30
  • FYI: Some keyboard layouts may require twice hitting the '~' key to print one '~' character. I often use screen so when a ssh connection is lost I can simply start another shell killing this session. – math Dec 12 '12 at 08:55

2 Answers2

50

Add another tilde (ie, type ~~.). Each successive tilde is eaten by the outermost ssh session which hasn't yet eaten one, but if the next character is another tilde, it's passed along to the next session in.

If, from gromit1, you ssh'ed to a third host (let's call it wensleydale), then ~~~. would drop the session to wensleydale and return you to a prompt on gromit.

1 And what a great server that is; how often have I heard a developer remark "cracking host, gromit"?

MadHatter
  • 79,770
  • 20
  • 184
  • 232
  • 2
    Related: [The Telnet Song](http://www.poppyfields.net/filks/00222.html) – voretaq7 Oct 05 '12 at 15:32
  • 1
    Oh, that's marvellous; I can't believe I hadn't come across it before. – MadHatter Oct 05 '12 at 15:53
  • It would be great if there were some way to get visual feedback of how far down the stack you are, in case you've got `laptop --> Wallace --> Gromit --> Shaun --> Timmy` or something crazy like that... – iconoclast Dec 12 '12 at 15:17
-2

You can issue arbitrary commands, and catch the output from the innermost shell into a variable

export output=`echo some_cmd | ssh user@host |  echo some_cmd2 | ssh user2@host2`
iconoclast
  • 1,800
  • 2
  • 18
  • 30