News for hackers. Stuff that matters.
Re:SSH Escape Sequences by __david__ on Tuesday October 24, 2017 @03:07AM (#15539767)
Also remember that if you are sshing to a box (call it A) and then ssh-ing from there to another box (call it B) and you want to close the connection to B then you need to use "~~." SSH on your local computer will see "~~" and will send a single "~" to A. A will see "~." and close the connection to B.

Basically, however deep you are you need to send that many ~s before the period.

Re:SSH Escape Sequences by userbinator on Tuesday October 24, 2017 @06:30AM (#15540581)
Basically, however deep you are you need to send that many ~s before the period.

Isn't it exponential? To escape ~, you need ~~. To escape ~~, you need ~~~~. To escape ~~~~, you need ~~~~~~~~. Etc.

Re:SSH Escape Sequences by rootbear on Tuesday October 24, 2017 @09:35AM (#15541385)
The escape sequence for Telnet, however, was exponential. This lead to the Telnet Song, by Guy Steele, a link to which was once posted on HN.

https://news.ycombinator.com/item?id=2060597

Note that the "up arrow" referred to in the song is not the current up arrow on the cursor keypad. Some early versions of ASCII-ish had an up arrow where the caret is now. And telnet actually mostly used control-], not control-^ anyway. I seem to recall Steele saying that he used "up arrow" because it worked better in the lyrics. I heard Steele sing this once and I think I recorded it.

Re:SSH Escape Sequences by schoen on Tuesday October 24, 2017 @10:14AM (#15541667)
Thanks for that! Do you remember how many repetitions were used in your recording?
Re:SSH Escape Sequences by rootbear on Tuesday October 24, 2017 @10:27AM (#15541773)
I don't, but it's at least three, as I recall. Beyond that and you tend to lose your place and forget how many to sing.
Re:SSH Escape Sequences by kevhito on Tuesday October 24, 2017 @06:39AM (#15540618)
No, not exponential. The escape sequence is: <newline> then '~'. Anything after the '~' is special. In other words, only the first '~' appearing directly after a new line is special.

For example, <newline>~~~~ will send <newline>~~~ to the remote side.

Re:SSH Escape Sequences by barrkel on Tuesday October 24, 2017 @06:36AM (#15540606)
You need to enter ~ after pressing return (or else you have a bad time editing command lines involving home directories), so in practice only the first ~ each SSH instance sees needs escaping.
Re:SSH Escape Sequences by schoen on Tuesday October 24, 2017 @10:43AM (#15541902)
It's convenient there (and probably not accidental!) that Unix usernames are usually lowercase, so if you want to run a command like

~otheruser/bin/cat

it won't conflict with an ssh escape sequence (although you won't see the ~ appear until you've typed ~o).

On the other hand,

~Randomuser/bin/cat

would be impossible to type immediately at a new command prompt over an ssh connection (though of course it's possible to run such a command by typing anything such as "x<Backspace>" first).

Re:SSH Escape Sequences by kylek on Monday October 23, 2017 @11:19PM (#15538936)
I often use ~C (to open the ssh command prompt) and type (e.g.) “-L 80:localhost:8000” to set up forwarding via the same ssh session.
Re:SSH Escape Sequences by mpfz0r on Tuesday October 24, 2017 @03:30PM (#15544499)
I wrote a small utility that allows you to create these forwardings through multiple hosts.

https://github.com/mpfz0r/sshdrill

Re:SSH Escape Sequences by wiredfool on Tuesday October 24, 2017 @04:20AM (#15540033)
This is useful to know, especially if it works with agent forwarding. I default to agent forwarding off, but invariably forget to enable it the few times I need it.
Re:SSH Escape Sequences by exikyut on Monday October 23, 2017 @11:33PM (#15538996)
I really don't like that this requires the first key to be Enter.

In the edge-case scenario of a glitchy link that appears frozen, it's entirely possible that my Enter keypress might actually make it through and then do something I don't want.

I've had mouse and keyboard input get through to seemingly-frozen VNC sessions, so...

Re:SSH Escape Sequences by anyfoo on Monday October 23, 2017 @11:40PM (#15539026)
Yep, as I wrote below another comment, I share the same sentiment. To mitigate the risk somewhat, I tend to press Ctrl-C a few times before hitting enter to initiate the escape sequence (there are some edge cases where you need more than one Ctrl-C to interrupt).

Still not satisfied about it, as there are plenty of situations where that doesn't help. Being in emacs, for example, it could make things worse...

Re:SSH Escape Sequences by jtl999 on Tuesday October 24, 2017 @01:52AM (#15539519)
cough accidently sending half-pasted gibberish to IRC.
Re:SSH Escape Sequences by majewsky on Tuesday October 24, 2017 @08:33AM (#15541070)
My most frequently posted line in IRC is

  :wq
Re:SSH Escape Sequences by falcolas on Tuesday October 24, 2017 @10:06AM (#15541588)
And slack. And hipchat. And google docs. And Sublime Text. And...:wq
Re:SSH Escape Sequences by ben174 on Tuesday October 24, 2017 @03:26PM (#15544472)
Try switching to

    ZZ
Doesn't require a CR and therefore less likely to be submitted accidentally.
Re:SSH Escape Sequences by majewsky on Tuesday October 24, 2017 @06:08PM (#15545646)
I'm aware of ZZ and use it occasionally, but I can't get my muscle memory from "occasionally" to "consistently".
Re:SSH Escape Sequences by mjevans on Tuesday October 24, 2017 @01:59AM (#15539547)
My solution is to use a stateless in-application (where I expect to be) command to switch to a different window (In this case, the server chatter window) where pasting the line is an error since there's no command.

It usually works...

I agree that it would be nice if /any/ terminal escape sequence followed by ~ would initiate the escape.

Re:SSH Escape Sequences by asdfaoeu on Tuesday October 24, 2017 @05:29AM (#15540338)
You don't need to press enter you can just use ^D.
Re:SSH Escape Sequences by peterwwillis on Tuesday October 24, 2017 @12:22AM (#15539201)
Always use Ctrl+C Enter ~. Enter. Just think of it as an ssh Konami code.

If you've sshed into another box first, use ~~. to only reset the embedded session.

Re:SSH Escape Sequences by dijit on Tuesday October 24, 2017 @07:43AM (#15540859)
^E^U<enter> standard practice.

It's TCP so the connection either breaks or arrives in order.

Re:SSH Escape Sequences by derefr on Tuesday October 24, 2017 @12:18AM (#15539182)
How about typing "Ctrl+C Ctrl+C e x i t Enter", and then adding the "~ ." if that doesn't work?
Re:SSH Escape Sequences by phillipwills on Tuesday October 24, 2017 @01:28AM (#15539436)
I've posted random ~. in irc because I did it in the wrong order...
Re:SSH Escape Sequences by JoshTriplett on Monday October 23, 2017 @11:08PM (#15538896)
Note that these only work at the beginning of a "line", so you may have to hit enter before the tilde.
Re:SSH Escape Sequences by anyfoo on Monday October 23, 2017 @11:21PM (#15538940)
Yeah. I can understand where they are coming from, at least with tilde as the escape character it would be highly inconvenient if it happened all the time, but I still a bit unhappy about having to press enter before being able to terminate a stuck session. That's usually not something I want to actually send, should the session not be as dead as I thought it was.
Re:SSH Escape Sequences by emmelaich on Tuesday October 24, 2017 @12:40AM (#15539261)
And if your terminal state is hosed by a crashed cursor-addressing (curses) program, you may have to use ^J instead of Enter.
Re:SSH Escape Sequences by anyfoo on Tuesday October 24, 2017 @12:47AM (#15539288)
Are you sure? I never noticed that. As an experiment, I did this in an ssh session from macOS to OpenBSD:

    stty raw; cat
... and while every key I tried now had its literal effect, including enter producing only a carriage return and Ctrl-C, Ctrl-V etc. producing their control characters (I think, I cannot see them :) ), <enter>~. still terminated the connection. Lucky terminal combination maybe?
Re:SSH Escape Sequences by emmelaich on Tuesday October 24, 2017 @12:58AM (#15539331)
Hmm, I just tried it with stty raw and you're right.

It has happened to me though. Maybe a curses crash can leave it a state other than raw? One of the other modes such as inlcr plus ..? Also I suspect smart terminals and shells prevent a interactive session from getting too crazy.

Re:SSH Escape Sequences by anyfoo on Tuesday October 24, 2017 @01:04AM (#15539349)
Yeah, come to think of it, it wouldn't really surprise me if some weird terminal state swaps ^J and ^M or something.

Edit: Oh, you mentioned that yourself already, inlcr.

Re:SSH Escape Sequences by erikb on Tuesday October 24, 2017 @10:03AM (#15541568)
This is very important to know. It often doesn't work and then there's no feedback why.
Re:SSH Escape Sequences by JdeBP on Tuesday October 24, 2017 @10:18AM (#15541694)
Others have mentioned that this is of course in the manual for OpenSSH. I recommend the Escape sequences section of the HP/UX manual for rlogin as useful reading, not least because it explains things that the OpenSSH manual does not, and actually answers questions brought up in discussion on this very page.

* http://nixdoc.net/man-pages/HP-UX/man1/rlogin.1.html

Re:SSH Escape Sequences by CaliforniaKarl on Monday October 23, 2017 @10:50PM (#15538840)
I learned this unexpectedly: I run ipmitool on a bastion host, and it uses similar sequences, like ~., to terminate a Serial over LAN session. I've had to remember to key ~~. so that the first tilde is able to get through to ipmitool.
Re:SSH Escape Sequences by IgorPartola on Monday October 23, 2017 @11:57PM (#15539094)
Omg thank you. Same exact use case and I can’t believe I didn’t realize this.
Re:SSH Escape Sequences by emmelaich on Tuesday October 24, 2017 @12:47AM (#15539289)
It's not as bad as it use to be. In the old days you might do many telnets to get where you want.

Sing the Telnet song!

http://www.poppyfields.net/filks/00222.html

Re:SSH Escape Sequences by anyfoo on Tuesday October 24, 2017 @01:03AM (#15539345)
Speaking of telnet, that protocol was actually much richer and had many more features than it would seem at a casual look.

I learned that when implementing a tiny part of the telnet protocol in a retro computer emulator's serial port emulation routines, mostly just to enable character mode and proper (non-)echoing. (I also implemented the RS232 control lines, so that telnet'ing into the port actually behaved like a physical terminal would with respect to carrier detect and all that, but I think there wasn't any overlap with the telnet protocol itself. Maybe flow control.)

Re:SSH Escape Sequences by DonHopkins on Tuesday October 24, 2017 @04:05AM (#15539975)
Telnet also uses TCP's out of band data mechanism to send "urgent data" and a DATA MARK to interrupt and synchronize with runaway processes, and send important commands around flow control and before buffered data.

https://en.wikipedia.org/wiki/Out-of-band_data

http://www.serverframework.com/asynchronousevents/2011/10/ou...

Re:SSH Escape Sequences by anyfoo on Tuesday October 24, 2017 @10:49AM (#15541951)
(Oh, Don Hopkins answering. I read you so often on TUHS!)

Didn’t know that, seems useful. Especially because another comment in this thread since mentioned that they often kill their ssh sessions because of runaway processes.

Overall, it seems to me that a lot was lost when giving up telnet.

Re:SSH Escape Sequences by teddyh on Tuesday October 24, 2017 @03:55AM (#15539932)
See also the even older SUPDUP protocol (RFC 734¹), which I understand had even more features.

1. https://tools.ietf.org/html/rfc734

Re:SSH Escape Sequences by DonHopkins on Tuesday October 24, 2017 @04:07AM (#15539983)
I wrote a terminal emulator for the Apple ][ + Videx 80 column card in FORTH and reverse polish notation 6502 code, that supported SUPDUP including the line saving protocol that RMS hacked into ITS and EMACS (%TDSAV %TDRES), so Emacs could stash lines on the screen in a memory buffer (on the Apple's bank switched RAM card) and restore them later to paint the screen quickly when scrolling back and forth. (That was great at 300 baud!)

This is just the part of it that interprets SUPDUP codes -- layered on top of the rest of the terminal emulator that emulated H19 (VT52 + character/line insert/delete):

http://www.donhopkins.com/home/archive/forth/supdup.f

Re:SSH Escape Sequences by DonHopkins on Tuesday October 24, 2017 @02:22PM (#15543899)
Marc Crispin (the author of the SUPDUP RFC #734, who was famous for his catchphrase "MM is not at fault!") also wrote another RFC #748 documenting the Telnet Randomly-Lose Option, which was useful for fuzzball testing.

https://en.wikipedia.org/wiki/Mark_Crispin

https://tools.ietf.org/html/rfc748

   IAC WILL RANDOMLY-LOSE

      The sender  of this command  REQUESTS  permission  to, or confirms
      that it will, randomly lose.

   IAC WON'T RANDOMLY-LOSE

      The sender of this command REFUSES to randomly lose.

   IAC DO RANDOMLY-LOSE

      The sender  of this command  REQUESTS that the receiver, or grants
      the receiver permission to, randomly lose.

   IAC DON'T RANDOMLY-LOSE

      The command sender DEMANDS that the receiver not randomly lose.
Re:SSH Escape Sequences by anilakar on Tuesday October 24, 2017 @03:49AM (#15539911)
On my keyboard layout tilde is a dead key that requires three keypresses, so the usual disconnection command with the appropriate precautions is usually something like this:

Esc Esc Esc Ctrl-U Ctrl-C Enter AltGr-] Space .

OpenSSH lets you map EscapeChar to something more convenient, but it doesn't allow multibyte characters.

Re:SSH Escape Sequences by majewsky on Tuesday October 24, 2017 @08:37AM (#15541092)
> it doesn't allow multibyte characters

Someone should make a patch. I want to use :hankey: as the EscapeChar.

EDIT: Apparently HN swallows emojis. I can see why.

Re:SSH Escape Sequences by anilakar on Tuesday October 24, 2017 @12:04PM (#15542629)
If your keyboard layout had pile of poo where your tilde key currently is, you would not be joking. The problem is that even Latin-based international layouts have quite a few characters that don't fit in the ASCII range.
Re:SSH Escape Sequences by unkown-unknowns on Monday October 23, 2017 @10:35PM (#15538782)
I come across these once a year or so on the net but when I need them I never can remember them because it's not so often that I need them
Re:SSH Escape Sequences by vlunkr on Monday October 23, 2017 @10:42PM (#15538813)
Fortunately you only need to remember ~? since it tells you the rest.
Re:SSH Escape Sequences by Groxx on Monday October 23, 2017 @11:09PM (#15538902)
Unfortunately, ~? is two characters too many for me to remember a year later :|
Re:SSH Escape Sequences by anyfoo on Monday October 23, 2017 @11:18PM (#15538932)
Do you never have to terminate a stuck session? <enter>~. to do so is practically muscle memory. The other sequences I cannot remember either, since their frequency of use is several orders less for me, but ~? is pretty intuitive to find out.
Re:SSH Escape Sequences by laumars on Tuesday October 24, 2017 @03:20AM (#15539810)
Not the OP but I generally don't need to use <enter>~. because I have my terminal multiplexed in tmux so will kill the tmux pane rather than OpenSSH session.

The main reason I prefer to kill via tmux rather than OpenSSH is because sometimes I'll be connected several sessions deep (eg via a Bastian / SSH jumpbox or tmux itself might be running remotely) so the tmux kill is easier than having to remember whether I need to ~. or ~~.

But ultimate there is no right nor wrong way to do things here, it's all just a question of preference and habit.

Re:SSH Escape Sequences by Groxx on Tuesday October 24, 2017 @04:16PM (#15544883)
Typically I just wait for timeout, or open a new session (remote tmux session makes this quite painless). Plenty of other things to do, usually, waiting a couple minutes every week or three doesn't bug me enough to google.
Re:SSH Escape Sequences by majewsky on Tuesday October 24, 2017 @08:40AM (#15541102)
Since I didn't know about <Enter>~. until a few days ago, I usually `killall ssh` or the specific PID.
Re:SSH Escape Sequences by gumby on Tuesday October 24, 2017 @07:50AM (#15540882)
If you need them but don’t remember them just use man ssh in another terminal.
Re:SSH Escape Sequences by empath75 on Tuesday October 24, 2017 @08:48AM (#15541135)
This is one of those things like discovering ^r that’s going to change my life.
Re:SSH Escape Sequences by OJFord on Tuesday October 24, 2017 @04:44AM (#15540136)
This page takes ~5s to load if JS from cdn.ampproject.com is blocked...
Re:SSH Escape Sequences by tokenizerrr on Tuesday October 24, 2017 @09:15AM (#15541260)
So don't block it? You can expect a broken browsing experience if you purposely break your browser.
Re:SSH Escape Sequences by notzorbo3 on Tuesday October 24, 2017 @09:28AM (#15541346)
This page is more broken than usual, so it's still interesting. Regardless of you snide remark.
Re:SSH Escape Sequences by liveoneggs on Tuesday October 24, 2017 @09:34AM (#15541383)
I always wanted a serial transfer available from this escape so I could drop into sftp mode or kermit/xmodem/zmodem files over inside of the same session.

There are actually more things supported by ssh than are exposed here (clone and stuff) but just not implemented in openssh's ssh client.

Re:SSH Escape Sequences by AdmiralAsshat on Tuesday October 24, 2017 @09:41AM (#15541419)
I am sad to say how often I've used ~. to close an SSH session because I entered an `ls -latr` command on a really huge directory. Ctrl+Z, Ctrl+C, Ctrl+\ would not halt the output, and it was faster to just terminate the session and come back in than wait for the output to finish.
Re:SSH Escape Sequences by selectnull on Tuesday October 24, 2017 @02:57AM (#15539739)
I spend quite some time in ssh sessions and have learned that "Enter~." terminates a connection when it gets unresponsive, but I never bothered to understand it. This actually explains it.
Re:SSH Escape Sequences by gear54rus on Tuesday October 24, 2017 @01:12AM (#15539378)
How is this SSH escape sequences when it's actually OpenSSH escape sequences?

They look neat though, better than having to restart the session to forward ports. Too bad it doesn't work in PuTTY.

Re:SSH Escape Sequences by vesinisa on Tuesday October 24, 2017 @10:18AM (#15541698)
I checked, and it is indeed an OpenSSH feature. According to the release notes, it was added in v2.2.0, but you can actually tell just by the perverse choice of ~ as an escape character!

In many European keyboard layouts, including the Finnish layout used by original SSH's author, tilde (~) is a compose key and a chore to type. It is usually expressed by typing AltGr+¨ followed by Spacebar: https://en.wikipedia.org/wiki/Tilde#Keyboards

English is one of the few keyboard layouts where a person can express ~ with one, rather than two, handstrokes. For others, terminating an SSH session with the escape character usually requires blindly typing this four-part sequence: Enter, AltGr+¨, Spacebar, Dot.

I know you can change the escape character with ssh_config(5), but no one who used a non-English layout would have ever chosen ~ as the default to start with. I've always felt like it had to be an OpenSSH-specific feature, because I knew the original author of SSH (before the open-source codebase became OpenSSH) used the same KB layout as myself. ;)

Re:SSH Escape Sequences by phillipwills on Tuesday October 24, 2017 @01:30AM (#15539445)
Pretty sure you can edit a lot of these session settings in putty while it is running. It's in the menu, I think (been a while since I used putty).
Re:SSH Escape Sequences by cat199 on Tuesday October 24, 2017 @09:16AM (#15541266)
because for just about everyone, ssh is openssh for better or worse.

true though.

Re:SSH Escape Sequences by reacweb on Tuesday October 24, 2017 @03:19AM (#15539806)
They work in mobaXterm.
Re:SSH Escape Sequences by namibj on Tuesday October 24, 2017 @06:42AM (#15540625)
Which AFAIK uses a cygwin build of OpenSSH, the only non-cygwin builds are the main GUI, maybe some helper tools, and the X11 Server. The commandline is a cygwin busybox.
Re:SSH Escape Sequences by snvzz on Monday October 23, 2017 @11:05PM (#15538888)
It's in the manpage.
Re:SSH Escape Sequences by majewsky on Tuesday October 24, 2017 @08:43AM (#15541116)
To be fair, who of us reads every manpage for every command that they use? (And I mean reading, not just skimming.)

I mean, it's a good practice to read manpages every now and then, just to learn new stuff (in the same way that there are always new vim commands to learn). But we encounter so many comands with so many options that knowing all of the options is just implausible.

Re:SSH Escape Sequences by snvzz on Tuesday October 24, 2017 @02:30PM (#15543986)
This topic has a whole section in the manpage, though. It's not easy to miss even if just skimming, because of that.
Re:SSH Escape Sequences by jwilk on Tuesday October 24, 2017 @05:31AM (#15540342)
Re:SSH Escape Sequences by hnarn on Tuesday October 24, 2017 @06:51AM (#15540651)
What's wrong with the AMP version?
Re:SSH Escape Sequences by wjoe on Tuesday October 24, 2017 @08:52AM (#15541154)
Aside from the common dislike for AMP centralising everything on Google's servers, I also dislike AMP links for the same reason I dislike mobile links to sites when I'm using a desktop PC.

The AMP versions usually lose formatting and links, and are designed to be read on a small mobile screen. I'd rather have the full version when I have a big screen available.

Re:SSH Escape Sequences by tokenizerrr on Tuesday October 24, 2017 @09:08AM (#15541219)
Did you even click the link? It's not hosted on Google's server and it looks great. Much less clutter than the non-AMP version, and loads faster as well of course.
Re:SSH Escape Sequences by notzorbo3 on Tuesday October 24, 2017 @09:18AM (#15541272)
Normal website works fine without cookies and javascript. AMP version does not. It gives you a blank page. Thus, AMP is bad, since a simple HTML page like this one should never require javascript to just display the contents.
Re:SSH Escape Sequences by Tyr42 on Tuesday October 24, 2017 @09:57AM (#15541514)
In this case I like the yellow block for the quoted content.
Re:SSH Escape Sequences by JustSomeNobody on Tuesday October 24, 2017 @07:30AM (#15540800)
Somewhat controversially, AMP is what’s wrong.
Re:SSH Escape Sequences by corobo on Tuesday October 24, 2017 @07:40AM (#15540844)
Wait I thought we were all annoyed that AMP was being served by Google (at google.com even). At least that was my beef, that and clicking the X took me back to Google instead of to the actual site.

Self hosted AMP is just a lightweight version of the site isn't it?

Re:SSH Escape Sequences by egeozcan on Tuesday October 24, 2017 @07:45AM (#15540864)
The AMP version requires a script from cdn.ampproject.org to display any content. I'm extremely annoyed by this new craziness where a 3rd-party script/xhr is required to display the content/images. This is more than just sloppiness on the development side, this risks user privacy as well.
Re:SSH Escape Sequences by corobo on Tuesday October 24, 2017 @07:54AM (#15540893)
New craziness? People have been using third party hosts for bootstrap, jQuery, anything on cdnjs, fonts.com, etc forever

You yourself use recaptcha on the site in your profile and a gist script from GitHub on your blog

Re:SSH Escape Sequences by egeozcan on Tuesday October 24, 2017 @09:10AM (#15541223)
You are right, but to be fair, I did migrate that website from Github so there are artifacts from the giant pile of mess that was my Jekyll config and I even wrote my own static generator (like the next webdev) to get rid of all the dependencies but this side-project never finished (I was experimenting with formatting/highlighting to inline gists as you can see in the last post but then I got bored).

I can't fight the spam bots myself so I have to hide my email behind that captcha for practical reasons (which is not a part of my site, it is a popup).

Other than that, there are nothing 3rd-party there. And everything loads pretty fast without any alien scripts (or any scripts at all for that matter).

Re:SSH Escape Sequences by majewsky on Tuesday October 24, 2017 @08:32AM (#15541060)
You missed the important part of that statement:

> a 3rd-party script/xhr is required to display the content

Way too many websites just stay blank when JS is disabled, even though HTML is very capable of displaying text.

Re:SSH Escape Sequences by notzorbo3 on Tuesday October 24, 2017 @09:24AM (#15541318)
It's interesting that people go out of their way to complain about their privacy being invaded by cameras, drones, the NSA snooping backbones, google, etc but most of us are fine with out browsers connecting to 61 different domains (CNN.com) to render a 3 paragraph news story.
Re:SSH Escape Sequences by notzorbo3 on Tuesday October 24, 2017 @09:21AM (#15541298)
The craziness is that more and more sites won't even render anymore without third-party javascript. Without bootstrap, jquery, fonts, etc, at least the page will still work, and it will work better because there's no bootstrap, jquery, fonts, etc.

Mind you, these are sites that can be served perfectly with a 1Kb HTML page + a bit of CSS. Not web apps or anything, where I understand you'd want to load a bunch of XHR requests.