简体   繁体   English

在java中调用shell脚本?

[英]Call a shell script in java?

is it possible to call a shellscript or the shellcode in a java class? 是否可以在java类中调用shellcript或shellcode?

My Code (in a static method): 我的代码(以静态方法):

Runtime rtime = Runtime.getRuntime();
Process child = rtime.exec("/bin/bash");
BufferedWriter outCommand = new BufferedWriter(new
OutputStreamWriter(child.getOutputStream()));
outCommand.write("streamer -c /dev/video0 -b32 -o test.jpeg");
outCommand.flush();
outCommand.close();
child.destroy();

But I get this error, if i try this code in my jsp page (tomcat6): 但是我得到这个错误,如果我在我的jsp页面(tomcat6)中尝试这个代码:

java.security.AccessControlException: access denied (java.io.FilePermission /bin/bash execute) java.security.AccessControlException:拒绝访问(java.io.FilePermission / bin / bash execute)

Any solution for this problem? 解决这个问题的任何方法?

EDIT : ls -l /bin/bash shows me follwing line: 编辑 :ls -l / bin / bash向我展示了以下内容:

-rwxr-xr-x 1 root root 875596 2009-09-14 07:09 /bin/bash -rwxr-xr-x 1 root root 875596 2009-09-14 07:09 / bin / bash

Thanks 谢谢

If you want to keep the security manager running, and you may have good reasons for doing so, then you can simply change the policy file that Tomcat is using. 如果您希望保持安全管理器正常运行,并且您可能有充分的理由这样做,那么您只需更改Tomcat正在使用的策略文件即可。 The files may be located in /var/lib/tomcat6/conf/policy.d . 这些文件可能位于/var/lib/tomcat6/conf/policy.d

A nice SO thread discusses this already. 一个不错的SO线程已经讨论过了。 Even if you grant AllPermissions to your application, running with the SecurityManager will allow you to use sandboxes with limited security. 即使您将AllPermissions授予您的应用程序,使用SecurityManager运行也将允许您使用安全性有限的沙箱。 You might want to do that, say, to run JavaScript that could be uploaded at runtime or something similar, or there may be some other code in your application that you don't trust for some reason. 例如,您可能希望这样做以运行可在运行时或类似情况下上载的JavaScript,或者您的应用程序中可能存在您由于某种原因而不信任的其他代码。

Try to disable the security manager. 尝试禁用安全管理器。 Edit /etc/init.d/tomcat6 and change: 编辑/etc/init.d/tomcat6并更改:

TOMCAT_SECURITY=yes

Change that to: 改为:

TOMCAT_SECURITY=no

And then restart Tomcat: 然后重启Tomcat:

/etc/init.d/tomcat6 restart

If it works, it's up to you to see if fully disabling the security manager is acceptable or not (using it is actually unusual IMO). 如果它有效,则由您来决定是否完全禁用安全管理器是否可接受(使用它实际上是不正常的IMO)。

Also if possible, attempt to run it in the single exec call. 如果可能,尝试在单个exec调用中运行它。

Process p = Runtime.getRuntime().exec(args);

where args is a string array of arguments. 其中args是一个字符串数组的参数。

String[] args = new String[] {"/bin/bash", "streamer", "-c", "/dev/video0", "-b32", "-o", "test.jpeg" };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM