exit out of all SSH connections in one command and close PuTTY

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...
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2a0a3aca8a7b082aaadb1b6f3">[email protected]</a>:~> #...doin some work...ooh! need to go check something on host8...
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b0b3bcb8b7a092babda1a6e3">[email protected]</a>:~> ssh host8
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d0d3dcd8d7c0f2daddc1c68a">[email protected]</a>:~> #...doin some work...OK time for lunch. lets close putty...
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0b2b1bebab5a290b8bfa3a4e8">[email protected]</a>:~> exit
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abc9cac5c1ced9ebc3c4d8df9a">[email protected]</a>:~> 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”.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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).

Method 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.

Method 3

Simply close PuTTY. (Alt+F4 by default IIRC.)

Method 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 <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f580869087b5869087839087db969a98">[email protected]</a>

If you want to exit one level:

$ exit

If you want to exit all:

$ exit 5

Method 5

Another way is to make function for ssh:

   function ssh(){ /usr/bin/ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90b4d0">[email protected]</a> ; exit ; }

Unfortunately it also will always close the connection and logout from console after you’ll finish work on the remote system.

Note, that you need make such function at all your servers, otherwise this hack won’t work. Btw you can always put function into ~/.bashrc or ~/.whatever_shell_you_use_rc .

It looks a little bit dirty hack comparing to uther’s way.

Method 6

Or you could use exec to replace your shell process with ssh when jumping to another host:

SSH to host1 with PuTTY...
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87e5e6e9ede2f5c7efe8f4f3b6">[email protected]</a>:~> #...doin some work...ooh! need to go check something on host8...
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2341424d494651634b4c505712">[email protected]</a>:~> exec ssh host8
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0361626d696671436b6c70773b">[email protected]</a>:~> #...doin some work...OK time for lunch. lets close putty...
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2c0c3ccc8c7d0e2cacdd1d69a">[email protected]</a>:~> 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.

Method 7

Typing logout in the putty terminal window does it for me:

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56243f353e37243216303924332033243b392433">[email protected]</a>:~ > logout

Closes the ssh connection and quits the Putty Application.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x