flashactionscript-3actionscriptactionscript-2

How can i on button press execute a command in the command prompt and get back the output in ActionScript?


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


Solution

  • 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. :)