2

I have a GUI using Flex. I have a condition like i need to execute some command line arguments in the local machine and get the results back or output back to a textbox area. How can i do a button on submit, execute command in the local machine and return the output?

Command to execute example: 

    echo logfile.log | grep username

Code:

button1.onRelease = function () 
{
    // in this computer, it will now run a command, please wait.
}

My reference from the answer: https://gist.github.com/993905

  • button1.onRelease is AS2 why are you tagging it for AS3? – The_asMan May 26 '11 at 19:11
  • 5
    I would like to do the same for AS2, AS3 too. –  May 26 '11 at 19:29
  • 1
    Laura please see my answer. This just isn't possible with AS2 or even AS3 from within the browser. You need native extensions and the flash player specifically avoids this to avoid security issues. You need a modified flash player projector or, in the best case, Adobe AIR 2.0 or greater which would require the use of Actionscript 3. –  May 26 '11 at 19:32

1 Answers1

6

You're not going to be able to do this using actionscript 2 and you're not going to be able to do this using actionscript 3 in the flash web player. There are certain tools you can use to create projectors using actionscript 2 and 3 to add this kind of extended capability but you cannot simply do it from the web, as of course this would be an extreme security risk.

However, you can do this with Adobe AIR 2.0 or greater. You use the NativeProcess class and detect the operating system, launch the terminal or cmd.exe and then you can run commands against it. Below is some code from a project I developed to run commands against the OS using AIR just as you're trying to do:

            private var os:String;
            private var consoleExecutable:File;
            private var consoleNativeProcess:NativeProcess;

            private function usbMounted(e:StorageVolumeChangeEvent):void 
            {
                //status.appendText('Device: ' + e.storageVolume.name + ' mounted to drive: ' + e.storageVolume.drive + '\n');

                os = Capabilities.os.substr(0, 3).toLowerCase();

                switch (os) 
                {
                    case "win":
                        //Windows OS
                        var rootDirs:Array = File.getRootDirectories();

                        var i:int = 0;
                        for (i; i < rootDirs.length; ++i) {
                            consoleExecutable = rootDirs[i] as File;
                            consoleExecutable  = consoleExecutable.resolvePath("Windows");
                            if (consoleExecutable.exists == true) {
                                consoleExecutable = consoleExecutable.resolvePath("System32");
                                consoleExecutable = consoleExecutable.resolvePath("cmd.exe");
                                i = rootDirs.length;                                    
                            }                   
                        }

                    break;

                    case "lin":
                        //Linux OS

                    break;

                    case "mac":
                        //Mac OS

                    break;

                    case "iph":
                        //Iphone OS

                    break;
                }   

                var consoleNativeProcessStartupNfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
                consoleNativeProcessStartupNfo.executable = consoleExecutable;
                var startupArgs:Vector.<String> = new Vector.<String>();

                startupArgs.push('/C fsutil fsinfo volumeinfo ' + e.storageVolume.drive + ':');

                consoleNativeProcessStartupNfo.arguments = startupArgs;         
                consoleNativeProcess = new NativeProcess();
                consoleNativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, consoleOutput);           
                consoleNativeProcess.start(consoleNativeProcessStartupNfo);         
                consoleNativeProcess.closeInput();
            }       

            private function consoleOutput(e:ProgressEvent):void 
            {
                var consoleOuput:String = consoleNativeProcess.standardOutput.readUTFBytes(consoleNativeProcess.standardOutput.bytesAvailable);

                switch (os) 
                {
                    case "win":
                        //Windows OS

                    break;

                    case "lin":
                        //Linux OS

                    break;

                    case "mac":
                        //Mac OS

                    break;

                    case "iph":
                        //Iphone OS

                    break;
                }

                consoleNativeProcess.exit();
            }

So the code should be pretty straight forward. It might not all be there as I've just copied the portion of the code dealing with the native process startup, output and termination. I then deleted parts of the code that were sensitive. Basically you're just getting the OS info, navigating to the path of the executable, check to see if it exists and if so, start it up, run a command against it and wait for output. I put in a switch statement to check the OS when handling the output as well, since it's safe to assume what you do next with the output will also be OS dependent (or at least in my case it was). If you need more help understanding all this I suggest googling for a tutorial on the Adobe AIR Native Process API or just post some more questions here. :)

  • 1
    Also note as one example of how this code isn't quite complete, the code starts off with me receiving a StorageVolumeChangeEvent because in this application I am acting whenever a new USB key is mounted. The part of the code that listens for these events, as well as import statements are missing. –  May 26 '11 at 19:39