import com.jcraft.jsch.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class MyShell { final static String ENDTAG = "!@#@!$@!23432432fdsafds#@$@#"; public static void main(String[] arg) { String host = "96.126.126.117"; String username = "zgq"; String password = "xxxx"; String[] commandArr = { "ls", "date", "pwd" }; MyShell myShell = new MyShell(); myShell.runCommands(host, username, password, commandArr); } public void runCommands(String host, String username, String password, final String[] commandArr) { try { JSch jsch = new JSch(); Session session = jsch.getSession(username, host, 4321); session.setPassword(password); UserInfo ui = new MyUserInfo() { public void showMessage(String message) { } public boolean promptYesNo(String message) { return true; } }; session.setUserInfo(ui); session.connect(30000); // making a connection with timeout. Channel channel = session.openChannel("shell"); final PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(); pos.connect(pis); channel.setInputStream(pis); PipedInputStream pis2 = new PipedInputStream(); PipedOutputStream pos2 = new PipedOutputStream(pis2); channel.setOutputStream(pos2); final BufferedReader outBuffReader = new BufferedReader( new InputStreamReader(pis2)); channel.connect(3 * 1000); Thread inputThread = new Thread(new Runnable() { public void run() { try { for (int i = 0; i < commandArr.length; i++) { Thread.sleep(5000); System.out.println("###begin run:" + commandArr[i]); pos.write((commandArr[i] + "\n").getBytes()); } pos.write((ENDTAG + "\n").getBytes()); pos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { pos.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("inputThread end"); } }); inputThread.start(); // channel.setOutputStream(System.out); Thread outputThread = new Thread(new Runnable() { public void run() { String oneLine = null; try { while ((oneLine = outBuffReader.readLine()) != null) { System.out.println("output:" + oneLine); if (oneLine.contains(ENDTAG)) { break; } } } catch (IOException e) { e.printStackTrace(); } finally { try { outBuffReader.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("outputThread end"); } }); outputThread.start(); inputThread.join(); outputThread.join(); channel.disconnect(); session.disconnect(); System.out.println("###end process commands###"); } catch (Exception e) { e.printStackTrace(); } } public static abstract class MyUserInfo implements UserInfo, UIKeyboardInteractive { public String getPassword() { return null; } public boolean promptYesNo(String str) { return false; } public String getPassphrase() { return null; } public boolean promptPassphrase(String message) { return false; } public boolean promptPassword(String message) { return false; } public void showMessage(String message) { } public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo) { return null; } } }
MyShell
猜你喜欢
转载自zgq456.iteye.com/blog/2164665
今日推荐
周排行