我試圖暫時禁止ip地址,如果有人試圖不通過代理服務器加入到我的服務器。Java Runtime.exec(「sleep 180; ...」)
String st = e.getRealAddress() + "";
if(!st.startsWith("/x.x.x.x")) {
Runtime runtime = Runtime.getRuntime();
runtime.exec("iptables -A INPUT -s " + st.replace("/","") + " -j DROP");
runtime.exec("sleep 180 ; iptables -D INPUT -s " + st.replace("/","") + " -j DROP");
}
但由於某種原因只執行-A命令,並且在我等了三分鐘後,我無法連接。我做錯了什麼?
Runtime.exec沒有啓動shell,你不能給它shell語法命令,如'sleep 180;等等'。 – pvg
但是它爲什麼輸出第一個命令? –
,因爲第一個命令就是這樣一個命令。在第二個例子中,你給了它兩個命令並期待類似shell的行爲。 Runtime exec不是一個shell,它一次只需要一個命令。只需使用Thread.sleep進行睡眠。 – pvg