Call ls via ssh. - Java Native OS

Java examples for Native OS:Shell Command

Description

Call ls via ssh.

Demo Code

/*/*from ww w . ja v a  2  s.co  m*/
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import java.io.*;
import java.lang.reflect.Array;
import java.net.*;
import java.util.*;
import javax.imageio.*;

public class Main{
    /**
     * Call ls via ssh.
     * Returns a string with information about the file, or null if not found.
     **/
    public static String findIOSFile(String filename) {
        Process p = null;
        String ret = null;

        try {
            String[] cmdLS = { "ssh", "-p", "2222", "root@localhost", "ls",
                    "-l", "--time-style=full-iso", filename };
            p = Runtime.getRuntime().exec(cmdLS);
            ret = MobileUtil.monitorProcessOutput(p, filename);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ret;
    }
    /**
     * Given a process and a string, monitors the output of the
     * process for the string.  Returns the line in which
     * the string is found, or null if not found.
     * It will continue until the process exits.
     **/
    public static String monitorProcessOutput(Process p, String theString) {
        boolean keepReadingLines = true;
        boolean endOfLine = false;
        boolean foundIt = false;
        boolean appRunning = true;
        String ret = null;
        int readInt = -1;
        InputStream is = null;
        BufferedReader br = null;
        String line = "";

        try {
            //System.out.println("monitorProcessOutput: theString=" + theString);

            is = p.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));

            while (keepReadingLines) {
                //System.out.println("monitorProcessOutput: reading lines");

                endOfLine = false;
                line = "";

                while (!endOfLine) {
                    //System.out.println("monitorProcessOutput: reading a line");

                    if (br.ready() && (is.available() > 0)) {
                        readInt = is.read();

                        // We get 13 & 10 as end of line.
                        if (readInt == 13 || readInt == 10) {
                            endOfLine = true;
                        } else {
                            line += (char) readInt;
                        }
                    } else {
                        Thread.sleep(100);
                        if (!processRunning(p) && (!br.ready())
                                && (is.available() <= 0)) {
                            // There's nothing else to do.  Bail.
                            endOfLine = true;
                            keepReadingLines = false;
                        } else {
                            //System.out.println("monitorProcessOutput: not bailing yet");
                        }
                    }
                }
                //System.out.println("monitorProcessOutput: line=" + line);

                // At this point, we might have a line.
                //System.out.println("\t\t" + line);
                if (line.indexOf(theString) > -1) {
                    //System.out.println("monitorProcessOutput: found our line");
                    ret = line;
                    foundIt = true;
                    keepReadingLines = false;
                }
            }
            //System.out.println("monitorProcessOutput: done reading lines");
        } catch (Exception e) {
            e.printStackTrace();
        }

        //System.out.println("monitorProcessOutput: returning " + ret);
        return ret;
    }
    /**
     * Returns whether a process is running.
     * exitValue() throws an exception if the process
     * is still running.
     **/
    public static boolean processRunning(Process p) {
        try {
            int exitVal = p.exitValue();
            return false;
        } catch (IllegalThreadStateException e) {
            return true;
        }
    }
}

Related Tutorials