[Jslib] Antwort: Re: Executing unix command ( such as ls ) from the jslib

Peter Leugner PLeugner at as-computer.de
Sun Apr 5 13:13:44 PDT 2009


You can start a local server that's listening to a socket and redirect
stdout to that socket with the help of netcat

Example:

var listener = {
  onStopListening: function() {},
  onSocketAccepted: function(server, transport) {

    try {
      var stream = transport.openInputStream(0,0,0);
      var instream = Components.classes
["@mozilla.org/scriptableinputstream;1"].createInstance
(Components.interfaces.nsIScriptableInputStream);
      instream.init(stream);
    } catch (e) {
      alert("Error "+e);
    }

    var dataListener = {
        data:"",

      onStartRequest: function(request, context) {
            this.data="";
      },

      onStopRequest: function(request, context, status) {
        instream.close();
      // this.data has the sent data
      },

      onDataAvailable: function(request, context, inputStream, offset,
count) {
        this.data = this.data + instream.read(count);
      }
    };

    var pump = Components.classes
["@mozilla.org/network/input-stream-pump;1"].createInstance
(Components.interfaces.nsIInputStreamPump);
    pump.init(stream, -1, -1, 0, 0, false);
    pump.asyncRead(dataListener,null);
  }
};

function startServer() {

  try{
    var socket = Components.classes
["@mozilla.org/network/server-socket;1"];
    server = socket.createInstance();
    server = server.QueryInterface(Components.interfaces.nsIServerSocket);

    server.init(6669,true,-1);
    server.asyncListen(listener);
  }
   catch (e){
     alert("Error: "+e);
  }

}

function stopServer() {
      if (server) server.close();
}


Start the server and then start the script piping the output like

ls | nc localhost 6669

Good luck,

Peter Leugner
---------------------------------------------------------------------
Leiter IT Anwendungsentwicklung
Tel.: +49 (0) 89 45 06 63 26
Fax: +49 (0) 89 45 06 63 81
e-Mail: pleugner at as-computer.de
Web: www.as-computer.de

AS Computer Consulting & Service GmbH
Sitz: München
Geschäftsführer: Dipl.-Ing. (FH) Andreas Erhart
Register: München HRB 111 507


|------------>
| Von:       |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |pawan_sin99 <PAWANthethunder at gmail.com>                                                                                                           |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| An:        |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |jslib at mozdev.org                                                                                                                                  |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| Datum:     |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |04/05/2009 20:39                                                                                                                                  |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| Betreff:   |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |Re: [Jslib] Executing unix command ( such as ls ) from the jslib                                                                                  |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
|------------>
| Gesendet   |
| von:       |
|------------>
  >--------------------------------------------------------------------------------------------------------------------------------------------------|
  |jslib-bounces at mozdev.org                                                                                                                          |
  >--------------------------------------------------------------------------------------------------------------------------------------------------|






So, how can we get the stdout ..because I have to display results...Is
there
anyway to do it...........




Pete Collins wrote:
>
> nsIProcess only supports executing commands and not returning stdout
> back to the caller ...
>
> --pete
>
> pawan_sin99 wrote:
>> Hi, I am developing an extension which will run unix command of shell
>> such as
>> ls..and fetch the ls result and displays it on addon...I am not getting
>> how
>> to do it with jslib.
>>
>>
>> Thanks in advance,
>> Pawan Kumar
>>
>
> --
> Pete Collins - Founder, Mozdev Group Inc.
> www.mozdevgroup.com
> Mozilla Software Development Solutions
> tel: 1-719-302-5811
> fax: 1-719-302-5813
>
> _______________________________________________
> Jslib mailing list
> Jslib at mozdev.org
> https://www.mozdev.org/mailman/listinfo/jslib
>
>

--
View this message in context:
http://www.nabble.com/Executing-unix-command-%28-such-as-ls-%29-from-the-jslib-tp22891225p22895561.html

Sent from the MozDev - jslib mailing list archive at Nabble.com.

_______________________________________________
Jslib mailing list
Jslib at mozdev.org
https://www.mozdev.org/mailman/listinfo/jslib

!DSPAM:3,49d8fac747199060010485!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.mozdev.org/pipermail/jslib/attachments/20090405/0b742587/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: graycol.gif
Type: image/gif
Size: 105 bytes
Desc: not available
URL: <http://www.mozdev.org/pipermail/jslib/attachments/20090405/0b742587/attachment.gif>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ecblank.gif
Type: image/gif
Size: 45 bytes
Desc: not available
URL: <http://www.mozdev.org/pipermail/jslib/attachments/20090405/0b742587/attachment-0001.gif>


More information about the Jslib mailing list