我已經編寫了一個代碼,在服務器中執行SSH並運行帶有參數的Java文件。無法在遠程服務器的SSH中執行我的命令
我面對的問題是,代碼成功執行SSH,但之後不運行該命令。奇怪的是,如果我從服務器單獨運行命令它的作品。對此有任何建議嗎? 以下是代碼:
public void getSSH(String code, String newCode, JspWriter out){
try{
File f = new File("/usr/site/html/Output.txt");
BufferedWriter output = new BufferedWriter(new FileWriter(f));
String Servers[] = {"[email protected]","[email protected]","[email protected]","[email protected]"};
for(int i =0;i<Servers.length && i<1 ;i++){
Process p = Runtime.getRuntime().exec("/usr/site/swapimages.sh "+Servers[i]+" '/root/testRemote.sh "+ code+" "+ newCode+"'");
out.println("/usr/site/swapimages.sh "+Servers[i]+" '/root/testRemote.sh "+ code+" "+ newCode+"'"+"<br>");
Utils.waitFor(10000);
}
}
}
我也一直建議JSch但還沒有研究它。
它會在ssh會話結束後運行命令嗎? – trutheality
只有一個... exec(),所以我只希望它運行一個命令(... swapimages.sh)。你是否期望它也在同一個單獨的exec()中運行testRemote.sh?也許我錯過了一些東西,但不會需要第二個...... exec()for testRemote.sh? – Straff
是啊,我在運行testRemote.sh在同一個exec命令 但它的工作時,我只是跑在服務器上.. – uppaljaskaran