PuTTY inserts random characters during a session

27

8

I recently started renting space on a remote server so that I could work on a project. I found that a relatively painless way to access it on a windows machine is through PuTTY. However, there is one thing that has always irked me when using it: for seemingly no reason random characters are sometimes inserted at the cursor.

Most of the time it is just a single tilde, but rarely it spits out what looks like some escape sequence ([[^8 or the like). It will only occur when I am focused on the window, whether I am typing or 20 feet away from the keyboard. If left for long enough, it will spit tildes at random intervals (average is about 1 minute).

Finally, this behavior seems to be inconsistant when running programs such as nano or the mysql interface: in nano, instead of inserting tildes, it will set marks (ctrl-^); in mysql, lines will become un-editable.

My question is this: Has anyone else experienced this sort of behavior in PuTTY? And if so, what can be done to prevent/correct this behavior?

Zachary Polikarpus

Posted 2014-01-29T05:51:49.820

Reputation: 273

Does your computer also do this if you leave open a random program on your host for a few hours, like notepad? – cutrightjm – 2014-01-29T06:08:45.847

@ekaj No, only in PuTTY. And aparantly PuTTY based programs like MobaXterm (I downloaded the portable version to test) EDIT: only during an SSH session in Moba – Zachary Polikarpus – 2014-01-29T06:39:30.480

Hmm. Are you sure it's not pretty close to periodic, like always 55-65 seconds while putty is entirely idle? If it is, it might be a "keep-alive" activity, either from the server end or maybe putty. Have a look at things found under a search "alive" in putty help. Maybe these will be helpful: http://superuser.com/questions/94436/how-to-configure-putty-so-that-home-end-pgup-pgdn-work-properly-in-bash http://unix.stackexchange.com/questions/6105/what-causes-a-ssh-interruption/6107#6107

– mgkrebbs – 2014-01-29T08:54:32.577

@mgkrebbs Sometimes it seems periodic, but it occasionally won't occur for about 5 to 6 minutes, then go right back to seeming periodic. Regarding the keep-alive idea, if that is the case, It seems to be isolated to the client side because if I start an ssh session from a linux machine in native terminal, all is well. I did see that first link when I was asking the question, and tried changing the terminal type to "linux", but that didn't seem to make any noticable difference. – Zachary Polikarpus – 2014-01-29T13:10:08.393

I've been experiencing this issue as well. It shows up when I'm using Putty, Kitty and even MobaXterm. – Attilah – 2014-04-27T07:14:04.523

What do you have for echo $TERM? Also in this thread the solution was to set PuTTY window size to 95 columns.

– harrymc – 2014-04-27T10:21:49.913

What OS are you connecting to...any other info about the remote machine you can provide? – Jared – 2014-04-29T15:50:43.047

@Attilah, any chance you can get the same odd characters when using special buttons on your mouse? Like the scroll wheel? Or: does it still happen if you unplug the mouse? – Arjan – 2014-05-03T12:11:55.760

Related: Why Putty inserting ~ into my Fedora bash shell, which nicely explains why one might only see the ~ (and hear a beeb) when actually something like \e[3~ is sent.

– Arjan – 2014-05-03T12:13:44.570

Answers

31

I had the same problem with PuTTY and found that is was being caused by an application I have called "caffeine" which effectively presses the F13 key every minute to stop the screen saver from being activated (group policies prevent me from changing screen saver timeout on my machine). Disabling the application solved me problem.

Bruce

Posted 2014-01-29T05:51:49.820

Reputation: 326

3Doh, I should've suspected that. Killing Caffeine did the trick. PS: See my solution below to have Caffeine and Putty working together – shreyansp – 2015-10-16T10:24:14.663

I had a signage app that did the same. Some [28~ appeared on the Terminal. – Curious Mind – 2019-01-18T10:28:31.797

2I did have caffeine installed at the time! It all makes sense now... Thanks Bruce! – Zachary Polikarpus – 2014-06-07T02:12:27.650

12

If you prefer to have Putty and Caffeine running then follow these steps:

  1. Stop Caffeine
  2. Check the Caffeine readme.txt for the default key simulated - mine was F15
  3. Login to your remote terminal via Putty
  4. Start Caffeine (Tip: start Caffeine with short interval e.g 3 seconds).
  5. Bring the Putty session in focus.
  6. On your bash command prompt press Ctrl+V (This is required to get the special code for the key. e.g try Ctrl+V followed by F12)
  7. Wait for Caffeine to simulate the key
  8. The key code will be displayed on the command line (e.g F15 is ^[[28~, where ^[ is escape). Stop Caffeine
  9. Now you can bind this key to blank so that when Caffeine simulates it, it won't type ~
    • edit ~/.inputrc (create a new file if it doesn't exist)
    • add the line "\e[28~":""
  10. Start Caffeine
  11. Restart your terminal session
    • There should be no more random ~ when using the Putty terminal with that linux server

shreyansp

Posted 2014-01-29T05:51:49.820

Reputation: 221

+1. I've expanded on this answer below (with explanation on why this wasn't the optimal answer for me) :) – Petru Zaharia – 2017-04-26T19:55:03.927

8

I found shreyansp's solution to be the most (but not quite) satisfactory one :)
Here's my attempt on improving that (of course YMMV). Hope this may be of use to those looking for a solution :)

My solution behaves as follows:

  • caffeine sends to Windows an appropriate Virtual-Key Code which:
    • prevents Windows from going to sleep or idle
    • does not otherwise generate any side-effects on Windows neither alone nor in combination (no Ctrl, Shift, Alt, Alt-Gr, Win, F1-F5, F10, etc.)
    • either is not sent to Putty or is ignored by Putty

TL;DR: My solution is applied between steps 2 and 3 (see below) with the -key:0E caffeine param:
Exit Caffeine & re-launch it with:
caffeine.exe 5 -key:0E (for easy testing)
caffeine.exe 50 -key:0E (for a mandatory screen saver set at 1 minute)

  • Launch read program on the remote host and watch how no keystrokes are received every 5 or 50 seconds.
  • Exit read with Ctrl+C

Shreyansp proposed a solution where a fix would be applied between steps 5 and 10 (see below).
The side-effect of that (on my config) was that, with each keystroke that putty forwarded from caffeine to the remote host:

  • it triggered a 'Reset scrollbar on keypress' (setting on Putty/Window page)
    which I would normally want, but only when I (the human) is pressing the key but not regularly by caffeine :)
  • readline/bash translated version of it (from '"\e[28~"' to '""' (blank key ?) caused the remote session interaction to hang for several seconds

In order to easily test the above, exit Caffeine & re-launch it with 5 seconds interval and Virtual-Key Code 07:
caffeine.exe 5 -key:07

  • Launch read program on the remote host and watch how keystrokes are received every 5 or 50 seconds.
  • Exit read with Ctrl+C

The keystroke 'pipeline', as I understand it:

  1. Caffeine sends a Virtual-Key Code to Windows
  2. Windows sends that Virtual-Key Code to Putty
  3. Putty does some 'translations'/'mappings' based on some session settings in:
    • Terminal/*
    • Window/*
  4. Putty sends the 'translated'/'mapped' key code to the remote host
  5. On the remote host, the 'terminal' program (e.g: $TERM=xterm, vt100, vt102, vt220, etc.) translates from the 'line protocol' into key codes.
  6. the readline library does some translations/mappings based on ~/.inputrc
  7. readline sends the Key Code to bash
  8. bash does some translations/mappings based on ~/.bashrc (based on the builtin bind command)
  9. bash or readline (not sure which one) sends the translated Key Code to nano (my text editor)
  10. This pipeline can get even longer by adding the screen program (which includes a $TERM=screen for step 5. and loops back again at step 6. to 10.)

Note: Once it gets to step 4., it becomes very difficult to precisely control the different layers of 'translations'/'mappings'. I would recommend avoiding that if you can.


Background:
I used caffeine.exe -key:07 for years before having to deal with a pfsense 2.3.3-RELEASE-p1 (based on FreeBSD 10.3-RELEASE).
Then, caffeine.exe -key:07 was received on the other side as ^[[28~ ... which seems to be mapped to Ctrl+^ (Set Mark) in Nano.
This was quite annoying (imagine someone pressing and keeping Shift Key while you move the text caret around in Notepad).

Previously, I did numerous customizations in Putty Settings, ~/.tcshrc, ~/.inputrc, ~/.bashrc, ~/.nanorc, ~/.screenrc to get what I consider basic functionality (Backspace, Delete, Home, End, PgUp, PgDown, Ctrl+Left, Ctrl+Right, Numpad 0-9, Numpad ./*-+) working consistently between bash/nano/screen.

Once I discovered this caffeine.exe -key:07 'bug', I didn't want to retrace that all over again :)


Tested on:
Windows 8.1 64-bit Enterprise (6.3.9600) / Putty 0.66 / pfsense 2.3.3-RELEASE-p1 (based on FreeBSD 10.3-RELEASE) / bash 4.4.12-release / nano 2.7.3 / screen 4.04.00


References:

Petru Zaharia

Posted 2014-01-29T05:51:49.820

Reputation: 182

1

This really does look like an escape sequence, or control character, which might explain why you don't see it in other programs like notepad.

I'm not quite sure which keys have to be pressed to get this output, but I'll play around a bit and try to find out. Can you name any other sequence shown? The behaviour of mysql and nano might just be another interpretation of the escape sequences that seem to get entered.

Finally, the question is what causes these key inputs. Do you, by any chance, use a laptop which e.g. automatically controls the backlight using some proprietary software? I've seen some weird preinstalled software actually emulating the keypresses instead of using the API. You might want to use something like inputlog on yourself.

Patrick R.

Posted 2014-01-29T05:51:49.820

Reputation: 1 221

I have since changed operating systems, first changing my dual Win-8.1 & Ubuntu to Ubuntu only, then to Mint, and finally to Arch. I have not experienced any similar problems, which leads me to believe that the problem was isolated to the manner in which Windows interacted with PuTTY. As for the cause of the key inputs, I would not be surprised if what you suggested was the case, however I did not use or notice any sort of automatic hardware control. If I still had those logs, I would post them. EDIT: If anyone else is experiencing this I encourage you to post this information. – Zachary Polikarpus – 2014-05-01T16:24:06.763

See also Gilles' answer in Why Putty inserting ~ into my Fedora bash shell, which nicely explains why one might only see the ~ (and hear a beeb) when actually something like \e[3~ is sent.

– Arjan – 2014-05-03T12:18:31.697

(As an aside: you might want to link to that post, as at first I figured seeing a single ~ would not indicate an escape sequence.) – Arjan – 2014-05-03T12:24:06.110

1

I use the -key:07 option on the commandline to launch caffiene.. Seems to stop the Putty problem.

Just put this in a bat file

caffeine.exe -key:07

WorsleyBaz

Posted 2014-01-29T05:51:49.820

Reputation: 11

I use 'caffiene.exe -key:07' as well at it served me well for years. However, since connecting to my pfSense (FreeBSD-based), this is received on the other side as ^[[28~ ... which seems to be mapped to Ctrl+^ (Set Mark) in Nano. This is quite annoying (imagine someone pressing and keeping Shift Key while you move the text caret around in Notepad). :) I'll have either find another key on caffeine side or do a custom mapping on the FreeBSD side. (see defs @ https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396)

– Petru Zaharia – 2017-04-26T14:34:24.733

Switched to caffeine.exe -key:0E (see my answer below). Seems to work well with Windows (8.1 64-bit) and Putty happily ignores it :) – Petru Zaharia – 2017-04-26T19:13:11.607

0

The better solution is to configure the settings on your putty session so that Caffine does not interfere with it.

In Putty under Terminal Keyboard choose the SCO option. In Putty under Terminal Bell change action to None (to turn off the chime).

Caffine will still do it's thing and not bother your putty session. (I don't use Fn keys during a putty session so I don't know what happens if you do want to use Fn keys).

TomH

Posted 2014-01-29T05:51:49.820

Reputation: 1

0

2017-09-20 : Solved the char injection by starting caffeine with «caffeine.exe -useshift -noicon» in task scheduler triggered at logon. No icon, no more injected chars in my mintty/bash sessions. That was so much annoying. Thanks everyone for your hints.

Marc Verreault

Posted 2014-01-29T05:51:49.820

Reputation: 1

0

Someone on Cisco forums suggests to change the console speed parameter to solve this problem.

http://www.dslreports.com/forum/r27090677-HELP-Cisco-1800-garbled-output-in-putty

You've definitely tried all the variations of console speed in Putty as mentioned in 1 ?

Note: If the baud rate is set different than the default rate, odd characters are seen on CLI. Check table1 to set baud rates for different Confreg values.

Montag451

Posted 2014-01-29T05:51:49.820

Reputation: 672

1Console speeds are for serial connections. He's referring to a SSH connection.

While SSH does have a "Terminal Speed" setting...I've never actually seen this cause an issue. – Jared – 2014-04-29T15:49:38.473

0

I also had the same problem and found that caffeine.exe was indeed the problem. At first I thought it could not be caused by caffeine.exe because I was using this for 2+ years on my old laptop. I tried changing caffeine.exe -useshift instead, but that sent ^C. Found another program, Mouse Jiggler, that keeps the screen saver away but does not send any Escape sequences in my Putty sessions anymore.

user332804

Posted 2014-01-29T05:51:49.820

Reputation: 1