The problem is that JavaScript does not have support for TCP/IP sockets, so it is really impossible to do this purely in JavaScript. However, you could accomplish this using AJAX web sockets (like WebShell and AnyTerm do) and have the server handle and authenticate the session locally.
Another option, should you absolutely require everything to be done on the client-side, would be to use a java applet run in the user's browser. Obviously this applet could function as a terminal shell, but per your needs, you could very easily make the applet open, authenticate, run the script then pass back a confirmation the script started OK.