Call Unix Shell commands from Servlet/JSP - java

I need to call some Unix commands from my Servlet.
I have some Perl script, but I want to "translate" them into Java.
Here is something that I want to do on Java, but that I've made in Perl:
system("myfolder/myscript.sh > /myfolder/logs/myscript.log");
Is it possible to do this on a Servlet?

Yes, but note that redirect is part of the shell you will want:
ProcessBuilder pb =
new ProcessBuilder("/bin/sh", "-c", "myfolder/myscript.sh > /myfolder/logs/myscript.log");
pb.start();

Short answer:
it's possible but it's bad design, and can pose a security risk.
better to flag somehow that the script needs to run and check the flag via script
Long answer (following the commments):
Servlets are usually used to provide a user interface (or api) to something, for example accessing data or in your case triggering an action. As such, they imply the possibility of access from a remote resource such as a remote computer. In some (actually most) cases, that remote computer may even be out of the network, for example somebody's home.
Every server which is exposed to the outside world has the potential of being hacked or attacked in some way, with the risk being directly related to the level of interest this resource poses.
For example, if you work for a big company (which is then noticeable by hackers), and this servlet is used to trigger a build in your local repository, and you decide that developers will be able to work from home and need to login in order to trigger a build or check their build status, it means that anyone with the right credentials can potentially access the servlet, from anywhere in the world. Now lets assume that your perl script needs to access your CI server for some data, and your source repository for another data (maybe it even copies the sources instead of letting the CI server do it). In this case, you just created a direct link between someone sitting somewhere in the world, to the company's source code. It also means that even if it's too hard to penetrate your incredibly secure service because you spent a vast amount of time closing all potential gaps, they may still be able to trigger many unnecessary builds, and if you work in Continuous Deployment even make those builds go to production (maybe causing a DOS attack or service disruption). If at some point someone decides that the script also needs to get a parameter from the servlet, you've even made the hacker's life easier and could eventually give him access to your system.
All I described in the previous paragraph may be completely irrelevant to your case, you might be developing a service which will run on your home computer and won't interest anyone but yourself, but this does not change the fact that this is bad design (which might be ok for home use by the way).
What I said in the short answer is that it's better to have servlets flag the system that an action is needed, for example set a flag in DB or even in a file, in this case a hacker's life would be much more difficult, as there's no direct link. This also makes the servlet respond immediately, possibly automatically updating on status, instead of waiting for the perl script to finish running.

Did u try Jsch.It can do ssh and execute shell commands.

Related

Java Application (With GUI) as a service

We have a a Java app that takes a few registers off a database and sends them back and forth to a web service, nothing too complicated. This java app has a GUI that informs the user about what's going on with the operations it performs, as well as providing means for configuring certain aspects of its execution and giving the user the opportunity to deal with errors that may happen.
The thing is, this application needs to run all the time, even if the user isn't logged on. I tried setting a windows task to make it run when the computer starts, but if that happens the program runs on the background and the interface never comes up.
We could break the interface away from the main project and make them run separately, so the service runs on the background quietly and the user is free to open and close the interface to their heart's content, but unfortunately we suffer from coupling problems in our project which makes that road a little more arduous than it should be.
So the question is: Is there a way to set a service up so that it runs even when the user isn't logged in, but once he does, the interface also comes up?
Our only target platform is, for now, Windows.
to your question : "Is there a way to set a service up so that it runs even when the user isn't logged in, but once he does, the interface also comes up?" the response is Yes and called JavaExe.
look its examples in JavaExe.zip, in particular example8 or 23
Only separation of UI is an answer here. Downgrading to XP doesn't make sense. You can also simply build that as a webapp and try to leverage esbeetle.com for interfaceing and doing all the logic.

Deleting a file that resides on an internal Linux server via my application

I've recently been working on an enhancement for an application at work that will allow users to delete presentations stored not on their local machine but on a Linux server present on the internal network. My problem is that I am not sure how to go about performing this delete. The location of the files are as follows:
http://ipaddress/dataconf/productusers/**ACCOUNT**/presentations/
I have access to the ACCOUNT name which is a parameter that will need to be passed in to navigate to the right directory. I will also have access to the presentation name which will be needed to specify the correct presentation to delete.
What I am having trouble with is where to begin.
I am using the Spring framework so my code is a mixture of Java, JSP, and JavaScript.
Essentially I have a .jsp page where I layout the presentations that are associated with each account. I.E when you click on an account it makes a call to a database and lists the presentations that are associated with that account. You can then select individual accounts and delete, or press one delete all button and delete them all.
I currently have it working so that when you delete a presentation in my application, it deletes the appropriate record from the database, but I also need to delete the physical presentation which is the basis for this question. Just as an FYI, these requests (get presentations from database, remove presentations from database) are all being handled through AJAX and JSON.
I am hoping to learn how to create a connection to the correct server, navigate to the proper directory as specified above, and issue the Linux command "sudo rm file-name" all in the same delete process that I described in the prior paragraph.
If you could point me in the right direction, any help would be much appreciated. Also, if you need any further clarification please feel free to let me know.
Thanks again,
Dave
This will not be easy. Or maybe it will. Please understand first that simply knowing where some files are published on an HTTP server is basically useless in terms of manipulating those files.
So I understand the following: You have your own web application on server A, a database somewhere, and some files located on another web server B. Internally on server B, the files will be in some weird directory e.g. /var/www/docs/whoknowswhat/somefolder/dataconf/productusers.
What you need to do is to somehow expose this folder from within server B over the network to your server A. Talk to your admin people. Maybe NFS is an option, or maybe Samba, or SSHFS. Make sure you have write permissions, and also make sure that noone else does.
Once you have mounted the location from B in your server A and it is available to you as some directory /mnt/serverB/productusers, then all you have to do is something like this, i.e. File f = ...; f.delete();
I did a little research and stumbled upon a neat solution to accomplish what I am trying to do. If you take a look at the following link:
http://www.journaldev.com/246/java-program-to-run-shell-commands-on-ssh-enabled-system
The above site describes a method in which you can open an ssh connection in Java and execute commands as if you were running them from the terminal. It has come in handy for my problem and I hope that if anyone else is experiencing the same problem that this will help them as well. Feel free to let me know what you think.

Serving Java Applets from a Queue?

I'm looking for a elegant way to create a queue for serving up batch compiled applets.
I have hacked together a SQL and PHP script to handle this but it chokes under mild loads.
is there an existing system that can handle taking a list in SQL and serve the applets in descending order each time it is requested. I'm also trying to handle this all server side as well.
The trick would be getting file001, then file002 ++ ect. to get served each time a web page is loaded. I'm batch creating applets that has a slightly modified background and I'm trying to serve a never been used applet waiting in the queue to load each time the a page is requested.
Is there a applet server I can tweak or does look like something that needs to be built?
No, I have never heard of a "batch compile applet server".
Could you maybe explain in more detail why you feel this is necessary?
Why don't you just use the same class and pass parameters to it?
That said, you can do compilation on demand quite well with e.g. ant and / or CruiseControl. You could put the pre-compiled applets into a directory. Then your PHP frontend just needs to keep track of what applet it delivered last, and fetch the next one the next time.
Still, this sounds rather complicated to me; so maybe you could explain your motivation.
In particular, why do you want a new applet on every reload? Would that not be rather confusing? Why not offer a link for each variant, so the user can choose?

Installed Programs/Computer Info for Web Application

I'm currently developing a support system for a university. The system is written in PHP and I would like to be able to get a current list of software and basic computer information on a computer. Basically when one of the faculty or staff creates a ticket from our web interface, I would like to have a Java Applet or similar that could be run and would return the information to the help desk PHP script. Does something like this exist?
There are lots of programs that do this sort of thing. Googling for "CMDB" should give you a reasonable start -- a couple of them are open source, though others aren't even close to free (e.g., BMC Atrium).
To keep things closer to topical (i.e., programming related), one of the main frameworks for this sort of situation is called Web-Based Enterprise Management (WBEM). On Windows this is implemented as WMI. On Linux there are a couple of implementations including OpenWBEM and HP WBEM.
In Java? You'd probably have a hard time even finding, let alone making, an applet that can get that info without already having some software installed on the user's end. The biggest features of java are (1) that it runs in a virtual machine (read: getting to the underlying OS/hardware is not something it likes to do), and (2) that in a browser, applets generally run in a "sandbox" that keeps the applet from doing anything remotely dangerous. Basically the most it can do is tie up resources.
Number 2 can be worked around by signing the applet, but that'll require you either buy a code signing certificate or install a self-signed certificate on any computer that'll run your app.
Number 1 might be worked around with some help from Runtime.exec and ...\wmic.exe, but that assumes the WMI stuff is installed -- which is rarely the case unless someone does a full install.

Is there something called Self running applications?

I need to create an application "dll, script or exe" which when the user upload on a folder on his server using his ftp, it will automatically run on the current folder and do some image manipulations in the folder images,
My Question is how to make something like this, which the user will not need to configure anything on his server, all what he want is upload in the right folder, and it will run automatically and keep running
Is it possible? How do it? Which Language to use?
UPDATE: I am targeting shared hosting server, which the user have no way to configure his server OS, about the OS, lets start saying its just windows.
I know that "Not possible" may be the right answer, and also "its a virus" may be another answer, i just want to know if its possible or should i think in another new way.
Everything I have read in this question screams "security vulnerability exploit". Since it's one of the main things hosting companies are making sure doesn't happen, I would say your chances are very slim to have that work.
However, if it's a web server, with something like CGI or PHP enabled, you could leverage that by uploading a CGI or PHP script in a place it can be run, and then calling it through a browser, thus doing whatever file manipulations you need... Things like safe mode, reserved or virtual directories could get in the way, but I think there is a better chance of that working.
Hmmm... is this a Window's or Linux machine? If this were on Windows, I would say create a C# service that uses FileSystemWatcher to listen for changes to the FTP folder and do your processing. As a service, it has no user interface and can run automatically on bootup.
Yes, there is!
http://en.wikipedia.org/wiki/Self_(programming_language)
One way I can think of is to use C# directory monitor. So whenever it found out a file is created in the folder it opens it up, checks the file type and executes it if it is an executable. However, you do have to watch out for incomplete file uploads though.
which the user will not need to configure anything on his server
What's the rationale of this requirement?
What's wrong with doing an initial configuration?
What's the target server? Is it linux, window, both?
Please be more specific.

Categories