[英]Java program to run shell commands from a windows machine
我正在嘗試運行Java程序以在遠程(Linux)機器上安裝命令。 我可以運行putty.exe,然后使用SSH密鑰連接到計算機。 但是無法運行實際的命令,例如“ bash”,“ ps-ef”或“ ls -la”。 當前使用Java runtime.exec,不確定使用java.lang.ProcessBuilder
是否會有所幫助? 我究竟做錯了什么 ? 任何幫助/指導將不勝感激..在此先感謝
package hello;
import java.io.*;
public class RuntimeExample {
public static void main(String args[]) throws IOException {
try{
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(new String[]{"C:\\Users\\yky90455\\Desktop\\putty.exe","abc@login.testserver.helloworld.co.uk","bash", "ps -ef"});
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running the command is:");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
從這里嘗試Jsch以獲得從Java執行到一些遠程Linux機器的shell腳本。 我已經做了這個工作,這確實很有趣。盡管您可能很少會缺少理解該文件的文檔,但是可以輕松克服。
還要考慮ExpectTC,它是TCL Expect的包裝。 自2010年中以來,該項目似乎沒有任何活躍的發展,但是我過去曾將其用於SSH。
http://expectj.sourceforge.net/apidocs/expectj/SshSpawn.html
感謝您的所有答復。 我嘗試了Ganymed SSH-2庫。 它適用於遠程計算機上的基本命令。 如果遇到SSH-2的任何限制,我將不得不探索其他API。
public class triggerPutty {
public static void main(String[] a) {
try {
String command = "putty.exe user@abc.text.com -pw password -m C:\\containing_comman.txt";
Runtime r = Runtime.getRuntime();
Process p = null;
p = r.exec(command);
p.waitFor();
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-m幫助您從該文件運行命令。
您可以在該文件中保留N個命令。.##標題##
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.