exit out of all SSH connections in one command and close PuTTY
Solution 1
Try using the ssh
connection termination escape sequence.
In the ssh
session, enter ~.
(tilde dot). You won't see the characters when you type them, but the session will terminate immediately.
$ ~.
$ Connection to me.myhost.com closed.
From man 1 ssh
The supported escapes (assuming the default ‘~’) are:
~. Disconnect.
~^Z Background ssh.
~# List forwarded connections.
~& Background ssh at logout when waiting for forwarded
connection / X11 sessions to terminate.
~? Display a list of escape characters.
~B Send a BREAK to the remote system (only useful for SSH protocol
version 2 and if the peer supports it).
~C Open command line. Currently this allows the addition of port
forwardings using the -L, -R and -D options (see above). It also
allows the cancellation of existing remote port-forwardings using
-KR[bind_address:]port. !command allows the user to execute a
local command if the PermitLocalCommand option is enabled in
ssh_config(5). Basic help is available, using the -h option.
~R Request rekeying of the connection (only useful for SSH protocol
version 2 and if the peer supports it).
Solution 2
Just press Ctrl + D to exit and it will log you out. Hold Ctrl and press D repeatedly to log you out of multiple windows, tabs, or levels until the window disappears.
Solution 3
Simply close PuTTY. (Alt+F4 by default IIRC.)
Solution 4
If you don't mind doing a little scripting you can do this.
Script: myssh.sh
#!/bin/bash
ssh $1
if [ $? -eq 5 ]; then
exit 5
fi
Call via the dot command:
$ . myssh [email protected]
If you want to exit one level:
$ exit
If you want to exit all:
$ exit 5
Solution 5
Or you could use exec
to replace your shell process with ssh
when jumping to another host:
SSH to host1 with PuTTY...
banjer@host1:~> #...doin some work...ooh! need to go check something on host8...
banjer@host1:~> exec ssh host8
banjer@host8:~> #...doin some work...OK time for lunch. lets close putty...
banjer@host8:~> exit
Putty closes.
5 levels deep is not pretty, since the traffic will pass through all the other servers. Because of that I don't recommend just killing PuTTY or ssh (~.), since (depending on what you do) this could result in orphaned processes on the servers.
Better to try and be less "lazy". Right-click on puttys title bar makes opening a new session quick. If you have a "default" server and accept 1 jump from that, the "Duplicate Session" feature is very useful. Especially when using pubkey authentication.
Related videos on Youtube
Comments
-
devarni over 1 year
Is there a way to back out of all SSH connections and close PuTTY in "one shot"? I work in Windows 7 and use PuTTY to SSH to various Linux hosts.
An example of the way I find myself working:
SSH to host1 with PuTTY... banjer@host1:~> #...doin some work...ooh! need to go check something on host8... banjer@host1:~> ssh host8 banjer@host8:~> #...doin some work...OK time for lunch. lets close putty... banjer@host8:~> exit banjer@host1:~> exit Putty closes.
Per above, any way to get from host8 to closing PuTTY in one shot? Sometimes I find myself up to 5 or 10 hosts deep. I realize I can click the X to close the PuTTY window, but I like to make sure my SSH connections get closed properly by using the exit command. I also realize I'm asking for tips on how to increase laziness. I'll just write it off as "how can I be more efficient".
-
devarni almost 12 yearsPretty cool! Any way to put
~.
into an alias or function? Just to give it an easy to remember name likeexitall
. Now I'm asking for way too much :). I tried a few things in~/.bashrc
but get-bash: ~.: command not found
. I suppose bash sees it as a string and not an escape sequence coming directly from the keyboard. -
devarni almost 12 yearsI like the
ctrl+d
solution as well, but this answers my question more directly. Thanks all. -
devarni almost 12 years@Gilles gotcha, makes sense.
-
Simon Gates almost 12 yearsSadly, this doesn't work universally. Any session running an editor or other full screen tool is likely to ignore
EOT
. And evenbash
will ignore it in the middle of a command line. Try it out yourself: type a single letter and tryCtrl-D
. -
Simon Gates almost 12 yearsAnd there's no guarantee that PuTTY also does this. The CLI
ssh
client needs this technique because it's CLI. PuTTY is a GUI application and there are far more user-friendly ways to do this stuff on a GUI. -
clerksx over 11 years
function
is not POSIX -- just remove the keyword, and you need to quote$@
properly, or your arguments will be passed after word splitting (bad). -
user3731606 almost 10 yearsOf course, you would have copy it to every computer that you may ever ssh from. Also, wouldn't it be easier to just define a shell function called ssh? That would avoid the preceeding period, etc.
-
Joe almost 9 yearsIt safely exits. It is a shell shortcut that is the same as typing 'exit' and pressing enter.
-
velis over 7 yearsActually mine (windows git bash & ubuntu bash) issue a
logout
. -
Joe over 7 yearsyes, I believe exit also calls logout as ssh connections tend not to be login sessions.
-
G-Man Says 'Reinstate Monica' over 6 yearsI believe that you didn't read the question correctly. OP does
ssh host1
, and then, fromhost1
, doesssh host8
. At that point alogout
would do the same thing as theexit
command they already know — exit out ofhost8
and put them back intohost1
. -
Richard over 6 yearsOk, I see, my bad.
-
mwfearnley about 4 yearsInteresting. I've never heard of these
ssh
features until today. PuTTY doesn't seem to have them though.