How to Copy and Paste into the Digital Ocean VNC Console
I recently signed up for Digital Ocean to test droplets for my development and linux management testing. I was securing the server and setup fail2ban, disabled the root login, and created a user account but forgot to change the user name from root to the user name I selected in my SSH client profile. I ended up locking myself out of the server and had to resort to logging into the admin panel on Digital Ocean’s website and use their browser embedded VNC client to restore my access. I quickly discovered that one of the downsides of using this panel is that you can’t copy and paste commands. Luckily there’s a workaround and you can use the browser console to do sendkeys through Javascript.
Bring up the console in the browser developer tools. Here’s a cheat sheet for keyboard shortcuts:
Browser | Description | Windows | Mac |
---|---|---|---|
Chrome | Open Developer Tools and bring focus to the console | Ctrl + Shift + J | Cmd + Opt + J |
Firefox | Open Console | Ctrl + Shift + K | Cmd + Opt + K |
Internet Explorer | After hitting F12, you have to click the console tab. There’s no direct shortcut to the console tab. | F12 | N/A |
!function(){function t(){window.rfb.sendKey(e.shift().charCodeAt()),e.length>0&&setTimeout(t,10)}var e=prompt("Enter text to be sent to console").split("");t()}();
Update 9-21-16
Ruden and Sebastiaan’s pointed out there was a bug in the code above. This updated snippet has support for characters when using shift characters like !@#$%^&*()_+
!function(){function t(){function n(t,e){s=s.concat(RFB.messages.keyEvent(t,e))}var o=e.shift(),s=[],i=o.charCodeAt(),c=-1!=='!@#$%^&*()_+{}:"<>?~|'.indexOf(o),r=XK_Shift_L;c&&n(r,1),n(i,1),n(i,0),c&&n(r,0),rfb._sock.send(s),e.length>0&&setTimeout(t,10)}var e=prompt("Enter text to be sent to console").split("");t()}();
You’ll receive a dialog prompt to enter the copy to paste in. Please note that you need to click on the VNC console and hit enter to execute the command.
If you need to enter more than one command, just hit the up arrow on your keyboard in the console to get the script again for easy reuse.
Jesse
Great tip! Thank you!
ruden
How do i get this to print out + % and & instead its printing out = 5 and 7?
Greg
Hey Ruden,
The + % and & are working for me with the script.
Try this in the console:
!function(){function t(){console.log(e.shift().charCodeAt()),e.length>0&&setTimeout(t,10)}var e=prompt(“Enter text to be sent to console”).split(“”);t()}();
It should put out the numbers to see what it’s converting the letter to char code. For me, + = 43, % = 37, and & = 38. You can also test the output in the console by executing
console.log(String.fromCharCode(43,37,38)); to see what it converts to.
Let me know if that helps or what your output is and I’ll try to help you further.
Eliot
Thank you, you saved my life. That a pity the web console is so utterly broken you can even use it simply in case of emergency…
Sebastiaan
Indeed a lifesaver, thank you.
I did also notice special characters produced by holding shift key do not come through and instead the regular character is used. tried Greg’s suggestion but to no avail. (I’m on OSX and chrome.) any suggestions?
Greg
Hey Sebastiaan,
Thanks for the comment! I just did some more testing with your information and found out that there was indeed a bug in the code. I think this is what Ruden was alluding too but I didn’t pick up on it. I’ll update the code above shortly but this snippet should do the trick for you:
!function(){function t(){function n(t,e){s=s.concat(RFB.messages.keyEvent(t,e))}var o=e.shift(),s=[],i=o.charCodeAt(),c=-1!==’!@#$%^&*()_+{}:”<>?~|’.indexOf(o),r=XK_Shift_L;c&&n(r,1),n(i,1),n(i,0),c&&n(r,0),rfb._sock.send(s),e.length>0&&setTimeout(t,10)}var e=prompt(“Enter text to be sent to console”).split(“”);t()}();
Sebastiaan
fabulous. Saved my life twice now 😉
ahmed
Thanks for the this amazing code…using chorme/firfox on windows7 caused weird behavior… it prints the lowercase as UPPERCASE and vice versa… any idea why is that happening and how to fix?
No Alternative
I am not sure which keys I’m supposed to use?
Greg
Hi No Alternative! Can you clarify what you mean by that or what you’re trying to do? I’ll be happy to help the best I can.
Scott Rowley
Thank you!
Greg
Glad it helped!
dousser
Thank you great job! you save my life!!!!!!!!!!!!!your Updated code on 9-21-16 worked for me via chrome 64.0.3282.167 for mac.
Greg
Glad it helped dousser!
Colin
Man, you saved my life here. Got a new router from Comcast. Didn’t think much of it, and everything was working fine… except I suddenly couldn’t log into my server. Odd. Tried resetting the root password from DO’s control panel, but couldn’t log in with their console. Eventually I find out that their key input support is terrible and I can’t type numbers. Your code got me in as root, where I was able to update the /etc/hosts.allow file with the new IP address. Thanks again!
Ebad khan
Really very nice
Thanks a lot.
naresh
Worked like charm. Saved me lot of trouble.
Greg
Glad it helped Naresh!