引数 host port username password コマンドファイル
コマンドファイル1行を1コマンドとして実行
POM
Javach.ethz.ganymed ganymed-ssh2 262
package jp.tuyoyun.metatrader.sshclient; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.Scanner; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; public class App { private static Scanner scan; public static void main(String[] args) throws Exception { Connection con = new Connection(args[0], Integer.parseInt(args[1])); con.connect(); con.authenticateWithPassword(args[2], args[3]); File file = new File(args[4]); InputStream stream = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF8")); String line = reader.readLine(); while (line != null) { System.out.println("\ncommand----------------------------------------------------\n"); System.out.println(line); System.out.println("-----------------------------------------------------------\n"); boolean confirm = confirm(); if (!confirm) { scan.close(); reader.close(); stream.close(); con.close(); return; } exec(con, line); line = reader.readLine(); } scan.close(); reader.close(); stream.close(); con.close(); } private static boolean confirm() { System.out.println("execute command? y/n"); scan = new Scanner(System.in); String confirm = scan.next(); if ("y".equals(confirm)) { return true; } else if ("n".equals(confirm)) { return false; } else { return confirm(); } } private static void exec(Connection con, String cmd) throws IOException, InterruptedException { Session session = con.openSession(); session.execCommand(cmd); InputStream inputStream = session.getStdout(); readResult(inputStream); inputStream = session.getStderr(); readResult(inputStream); session.close(); } private static void readResult(InputStream inputStream) throws UnsupportedEncodingException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF8")); String line = reader.readLine(); while (line != null) { System.out.println(line); line = reader.readLine(); } reader.close(); inputStream.close(); } }
0 件のコメント:
コメントを投稿