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:

BrowserDescriptionWindowsMac
ChromeOpen Developer Tools and bring focus to the consoleCtrl + Shift + JCmd + Opt + J
FirefoxOpen ConsoleCtrl + Shift + KCmd + Opt + K
Internet ExplorerAfter hitting F12, you have to click the console tab.
There’s no direct shortcut to the console tab.
F12N/A

Paste this snippet into the textbox in the console window:

Chrome console

 

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 !@#$%^&*()_+

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.

Javascript Prompt

 

 

 

 

 

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.


Comments
Harvey Specter
Posted at 2:52 pm March 19, 2016
Jesse
Reply
Author

Great tip! Thank you!

Harvey Specter
Posted at 8:51 pm May 4, 2016
ruden
Reply
Author

How do i get this to print out + % and & instead its printing out = 5 and 7?

    Harvey Specter
    Posted at 12:21 am May 9, 2016
    Greg
    Reply
    Author

    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.

Harvey Specter
Posted at 8:43 am August 26, 2016
Eliot
Reply
Author

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…

Harvey Specter
Posted at 3:53 pm September 21, 2016
Sebastiaan
Reply
Author

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?

    Harvey Specter
    Posted at 5:08 pm September 21, 2016
    Greg
    Reply
    Author

    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()}();

Harvey Specter
Posted at 5:50 pm November 3, 2016
Sebastiaan
Reply
Author

fabulous. Saved my life twice now 😉

Harvey Specter
Posted at 6:15 am July 30, 2017
ahmed
Reply
Author

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?

Harvey Specter
Posted at 11:35 am September 28, 2017
No Alternative
Reply
Author

I am not sure which keys I’m supposed to use?

    Harvey Specter
    Posted at 2:42 pm September 28, 2017
    Greg
    Reply
    Author

    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.

Harvey Specter
Posted at 3:57 pm October 31, 2017
Scott Rowley
Reply
Author

Thank you!

    Harvey Specter
    Posted at 2:14 pm November 2, 2017
    Greg
    Reply
    Author

    Glad it helped!

Harvey Specter
Posted at 7:14 pm February 20, 2018
dousser
Reply
Author

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.

    Harvey Specter
    Posted at 8:10 pm February 22, 2018
    Greg
    Reply
    Author

    Glad it helped dousser!

Harvey Specter
Posted at 12:33 am May 15, 2018
Colin
Reply
Author

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!

Harvey Specter
Posted at 4:16 pm July 23, 2018
Ebad khan
Reply
Author

Really very nice
Thanks a lot.

Leave a Reply