Shell commands
From Trephine
Basic interactive demo showing how shell commands can be run through the privileged trephine applet context.
You must have JavaScript enabled to run this demo.
/* Status and element functions. */ var e = function(tag,index){ return demotab.getElementsByTagName(tag)[index||0]; }; var status = function(msg) { if (!msg) return e('p').style.display = 'none'; e('p').style.display = ''; e('p').innerHTML = msg; }; /* Setup demo */ var setup = function() { e('form').onsubmit = function(event) { try { if (!trephine || !trephine.loaded) throw "Error: trephine not loaded."; if (!trephine.hasPermission()) throw "Error: elevated permissions have not been granted."; var cmds = parse(e('input').value); for (var i=0; i<cmds.length; i++) cmds[i] = cmds[i].replace('\\', '\\\\').replace("\n",'\\n').replace('"', '\\"'); var result = trephine.exec( 'js', 'java.lang.Runtime.getRuntime().exec(["' + cmds.join('", "') + '"])' ); var boxtype = result.success ? 'success' : 'error'; e('div').innerHTML = '<p class="' + boxtype + 'box" style="float:none">' + (result.result || result.error) + '</p>'; } catch(err) { e('div').innerHTML = '<p class="errorbox" style="float:none">' + err + '</p>'; } return false; }; }; /* Load trephine */ status('<span class="spinner"></span> Loading trephine...'); trephine.load({ debug: true, // Enables debug logging to the Java Console engines: ['js'], // Initialize the JavaScript engine as soon as possible onload: function() { // Immediately prompt for permissions status('<span class="spinner"></span> Asking for elevated permissions...'); trephine.askPermission( function(response) { if (!response) { // Short-circuit if permission request was denied return demotab.innerHTML = '<div class="error">Sorry, you must grant trephine privileges to enjoy this demo.</div>'; } setup(); status('Permission request granted.'); setTimeout( function(){ status(); }, 2000 ); } ); } }); /* Command line argument parser */ var parse = function(s) { var re = function(s,o) { return new RegExp(s,o) }; var args = [], ws = re('\\s'), quote = re('[\'"]'), escape = '\\\\'; var machine = { 'search': function(ch) { // Looking for a value if (ch.match(ws)) return; if (ch.match(escape)) return this.state = 'leadescape'; var d = ch.match(quote); this.delimiter = d ? d[0] : ws; this.buffer = [d ? '' : ch]; this.state = 'value'; }, 'leadescape': function(ch) { // Excape character encountered outside a value this.buffer = [ch.match(ws) ? ch : '\\' + ch]; this.delimiter = ws; this.state = 'value'; }, 'value': function(ch) { // Inside a value if (ch.match(escape)) return this.state = 'escape'; if (!ch.match(this.delimiter)) return this.buffer.push(ch); args.push(this.buffer.join('')); this.buffer = []; this.state = 'search'; }, 'escape': function(ch) { // Escape character var m = ch.match(this.delimiter); this.buffer.push( m ? m[0] : '\\' + ch); this.state = 'value'; } }; for (var i=0; i<s.length; i++) machine[machine.state || 'search'](s.substr(i,1)); if (machine.buffer && machine.buffer.length) args.push(machine.buffer.join('')); return args; };
<p class="notice"></p> <form class="shellcommands"> <h4>Command:</h4> <input type="text" /> <h4>Result: </h4> <div></div> </form>
.shellcommands h4 { font-weight: bold; margin-right: 1em; margin-bottom: 0; } .shellcommands input { display: block; margin-bottom: 18px; width: 890px; background: #fffff0; border: 1px solid #999; } .shellcommands div { margin-bottom: 18px; } .demotab .spinner { float: left; height: 20px; width: 25px; background: url("skins/common/images/spinner.gif"); background-repeat: no-repeat; }
Activation
- Activate the demo by clicking the specified link.
- You may see a Java Security dialog, if so, click "Run"
- In the Privilege Confirmation dialog, click "OK" to grant scripts on this page elevated privileges.
Usage
- Type a shell command in the text box
- For example, the commands "
notepad" and "open -e" will launch text editors in Windows and Mac OS X respectively.
- For example, the commands "
- Hit [Enter] to execute
- Note
- The demo merely kicks off the provided command asynchronously and does not handle any standard input/output to the spawned process.