how execute linux command using java on windows? - java

I need execute bash commands on a separate Linux machine using java on Windows.
I need run executable file from specified directory, like
cd /home/bin
How can I do this?

You will need to use some kind of SSH library for Java that you can then use to create an SSH connection to the target machine and run the commands inside of the SSH session.
Here is another SO question that covers this. And here is another.

After long hours, I finally found useful information. please follow link "How do I run SSH commands on remote system using Java?"
Also use jar from "http://www.ganymed.ethz.ch/ssh2/" the link. It is the jar required for any person to execute ssh commands from java code in windows environment. There are other ways too. And most jars I didn't find them useful, especially when you want to automate user authentication to access linux environment.
Note, passing directly the password (ssh password) is not appropriate way of doing things.And many experts suggested in few other threads.But this simple solution works for someone who is not concerned about security.

Related

Best way to copy a file from Windows machine to Linux machine using java

My requirement is to copy a file that is on windows machine to Linux machine using the IP, User name and password of the windows machine using java code. I have the java code running on linux machine. I want to know the best way to achieve this. What is the procedure to achieve this?
You can use JCIFS to access a Windows share from Java on Linux or Windows.
Your code is running on the Windows machine, right? The most secure way would be to use SSH. http://www.jcraft.com/jsch/ looks like a pretty good implementation. It's used by ant, maven. etc, to do the same thing.
On the other hand, if you're not creating a new build system, could I suggest that perhaps your build system (ant/maven/gradle) should be copying your files around for you?

Java application to execute shell files from different servers

I would like to know if the following is possible:
I have to create a Java application that runs .sh files from different servers, I have my class to execute shells, with Runtime and Process, it runs .sh files from my computer, the thing now is that I would like to know if instead of my location be
process = runtime.exec("/home/user/Documents/example.sh");
could be:
process = runtime.exec("180.150.2.***/server/user/Documents/serverExample.sh");
and the thing is, that to get the .sh files from server, I have to login, this application could be a desktop application or a web application, but has to be in Java, so, how could I do this?
I appreciate your help.
Chema.
Basically, I don't think you can do that, the way you are trying. The Runtime.exec(...) will delegate to the OS to perform the actual execution.
There are any number of ways to achieve what you want, either purely in Java or via additional utilities based on the OS.
You could SSH or telnet to the remote machine and execute the commands via those interfaces.
You could write a client server app, where the server would allow you to send commands to it to be executed on your behalf (but you must understand that this is a massive security risk).
Check out Jsch or Ganymed SSH. I have used the latter to perform ssh/scp tasks programmatically.

Java accessing WMI as a particular user

I am trying to write a Java application that will query the WMI on windows hosts within domains to obtain their mac address and dns name. I can run the script via exec or using a few of the jars I have found on the internet but none that I have seen allow me to impersonate an AD account when I run the query.
I would like to be able to specify the account that the query runs as(an admin of that domain), the program may run on a machine that is not in the domain or there might be two domains that I will query.
I've looked at JACOB and com4J but can't find an example of it doing impersonation.
Also I'm not an advanced programmer by any means so apologies if I've missed something glaringly obvious
Thanks in advance.
If you use the ConnectServer method of SWbemLocator you can specify the username and password to connect to the other machine as. However, you cannot use this method to connect to the machine your running the code on.
If your just calling a script, consider using Python. There is a nice example of how to do this using python in the wmi Cookbook
You can wrap the exec command line with a call to runas, which will run the command as the specified user.
runas manpage

Java Remote File Options

I've recently started an internship and I'm having some issues with Java in the enterprise environment. I've been assigned the task of porting the functionality of an old windows shell script to java so it may be ran on one of our JVMs.
The shell script runs on one of the Windows servers and grabs the previous days log files from a couple of linux boxes then FTPs them to another Windows machine.
I'm not quite sure how to achieve this functionality using java. I only can SSH into the linux boxes. I have got the connection working for the Windows file share, just not sure of the best way to connect to the linux boxes.
I've considered using one of the SSH libraries, but would prefer to avoid using a third party library.
Any insight would be great. Thanks!
Java development is all about the appropriate use of third-party libraries. You can try and avoid it, but you won't get anywhere.
I suggest using JSch for your SSH/SCP/SFTP needs.
You're looking for SFTP. Look at How to retrieve a file from a server via SFTP?.
Try using SCP (an FTP-style way to transfer files, but it uses SSH) to transfer the files from Linux.

jar file to use rsync for uploading and downloading files on linux server

I am writing an application for taking backup of user data. In my application i have to perform incremental backup operation. for performing incremental backup i am trying to use rsync algorithm.
i am writing application for windows version. I am having linux hosting server for storing files. after some search i got two lib "jarsync-0.1" and "sisyphe-0.92" but "jarsync-0.1" is
a beta release jar and not giving appropriate result. and "sisyphe-0.92" is configued with linux o.s.
Please could you suggest me any lib file in java that can be used for rsync (for windows version).
There is currently no complete Java port of rsync.
In your case I would look into a native rsync you could call. DeltaCopy has an rsync GUI for Windows which may be useful.. http://www.aboutmyip.com/AboutMyXApp/DeltaCopy.jsp
I guess, this is going to remain unanswered. Since there are no rsync libraries for Java being actively developed.
I just found there is already a discussion on this topic here.
Apparently, most of the guys happen to have implemented rsync via system calls(i.e invoke native calls via your Java app).
The only way I see this application can be coded is via native rsync calls to the OS. Since there are no actively developed java implementations available for rsync. So my suggestion would be to download rsync utility which works in command line and call the utility via process calls in your code.
This is the best possible solution I could think of as of now.

Categories

Resources