gpt4 book ai didi

java - 如何通过Java运行或访问linux系统目录

转载 作者:太空宇宙 更新时间:2023-11-04 05:42:47 25 4
gpt4 key购买 nike

我尝试在CentOS 7中使用java构建Snort GUI,但出现了linux不允许java程序访问系统目录的问题。有没有办法让java访问系统库?

一些commandSender.class:

import java.io.IOException;

public class commandSender {
public static String snortLocal="/usr/sbin/";
public static String rulesLocal="/etc/snort/rules/local.rules";
public static String logLocal="/var/log/snort/";
public static String ethDevice="enp0s25";
public static String configLocal="/etc/snort/snort.conf";

public static void makeComm(int option) throws IOException, InterruptedException{
if(option==1){
String local=getSnortLocal();
String comm=checkSnort();
Controller.exec(sendComm(local, comm));
}
else if(option==2){
String local=getSnortLocal();
String comm=startSnort();
Controller.exec(sendComm(local, comm));
}
else if(option==3){
SettingGUI.setting();
String local=getSnortLocal();
String comm=startSnortNIDS();
Controller.exec(sendComm(local, comm));
}
else if(option==4){
//kill

}
else if(option==5){
String local=getSnortLocal();
String comm=showVersion();
Controller.exec(sendComm(local, comm));
}

}

public static String getLogLocal() {
return logLocal;
}

public static void setLogLocal(String logLocal) {
commandSender.logLocal = logLocal;
}

public static String[] sendComm(String local, String comm){
String[] commmand={local, comm};
return commmand;
}

public static String checkSnort(){
configLocal=getConfigLocal();
String checkSnort="snort -T -c "+rulesLocal;
return checkSnort;
}

public static String startSnort(){
configLocal=getConfigLocal();
ethDevice=getEthDevice();
String startRules="snort -dv -i "+ethDevice + " -l "+logLocal;
return startRules;
}

public static String startSnortNIDS(){
configLocal=getConfigLocal();
ethDevice=getEthDevice();
String startRules="snort -dv -i "+ethDevice+" -c "+configLocal+" -A fast -l "+logLocal;
return startRules;
}

Controller .类:

import java.util.*;
import java.io.*;

public class Controller {

public static Process exec(String[] command) throws IOException, InterruptedException{
Process p = Runtime.getRuntime().exec(command);
InputStream is = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
p.waitFor();
is.close();
reader.close();
p.destroy();
return null;
}

错误消息:

java.io.IOException: Cannot run program "/usr/sbin/": error=13, Permission denied
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:485)
at Controller.exec(Controller.java:7)
at commandSender.makeComm(commandSender.java:21)
at GUI$3.mouseClicked(GUI.java:172)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6536)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.io.IOException: error=13, Permission denied
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 37 more

我已经将java、javac、javaws和jar设置为777

最佳答案

您正在将数组 {"/usr/sbin/", "snort -T -c/etc/snort/rules/local.rules"} 传递给 Runtime.getRuntime().exec (对于 option==1 的情况,其他情况类似)。

这是错误的,原因有两个:该数组的第一个值需要指定要执行的命令,而不仅仅是其目录,即它应该是/usr/sbin/snort。此外,每个参数都需要是其自己的数组元素。

因此,数组总体应如下所示:{"/usr/sbin/snort", "-T", "-c", "/etc/snort/rules/local.rules"}。我建议使用 ArrayList 来构建命令行,方法是附加所需的所有值,然后使用 list.toArray(new String[0]); 从中创建一个数组。

关于java - 如何通过Java运行或访问linux系统目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43308918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com