I want to connect to a server using ssh and run an executable on the server from my local Java code. I am aware of the thread SSH connection with Java but the trouble is I can not figure out how to use sftpChannel (an object of ChannelSftp) to run a file rather than open a file.
If this can not be done using JSch, how should I do if I want to call a exe file remotely in Java.
Thanks very much!
You need to use ChannelSsh, not ChannelSftp to run a remote command.
http://epaul.github.io/jsch-documentation/javadoc/
Related
Currently I am using weblogic for application server and it is running in one machine. I have to write the pdf(or any file type) to another Unix server. Can anyone please help me find the solution.
One solution is to mount the remote directory to your local server via smb or NFS (keyword sharing). Then you don't have to put any code for the copy job into your application. Instead the operating system will take care for the operation.
A simple and raw solution would be a SSH connection. There exists several Java libraries for SSH such as http://www.jcraft.com/jsch. Open a connection, write your file, close the connection. Of course you need the corresponding SSH keys and privileges to do this on the remote machine.
I'm trying to connect to remote hadoop cluster, which isn't accessible just through HDFS. Right now it is being used in that way: user connects to Jump box through SSH (e.g. ssh user#somejumboxhost.com), then from jump box server we do connect to hadoop also with ssh (e.g. ssh user#realhadoopcluster1.com). What I'm trying to do is to access files from my Scala/Java application using HDFS client. Now I'm feeling like in Matrix - "I must go deeper" and don't know how to reach the server.
May be someone had similar experience? Right now I'm trying to connect to first server with SSH client from my app, but then I don't know how to call the HDFS client.
Any ideas will be appreciated, thanks!
I can think of something like this . There is "ganymed-ssh2" api which helps you to connect to some server using ssh and run unix command from there. Using this you can connect to your jumo box.
And from there you can run command as " ssh user#realhadoopcluster1.com hadoop fs somthing"
As we can run commands with ssh like this.
From your jump box, setup a password less ssh to your hadoopcluster machine. or you can use sshpass with password.
You can visit following link to check how to use this api:
http://souravgulati.webs.com/apps/forums/topics/show/8116298-how-to-execute-unix-command-from-java-
Hadoop is implemented in Java, so you could just run the Hadoop cluster directly from your application. Use Java RMI if it's a remote cluster. This extra pipework you're trying to do makes no sense.
I faced problem to copy(duplicate) a file from one SFTP folder to another one on the same server.
So, question: Is there a some effective method to do it remotely (i.e. without copying a data to client and vice-versa)?
I am using Java with the JSch library.
The SFTP protocol itself does only support data transfer between client and server, not on the server itself.
You can use other parts of the underlying SSH protocol (and JSch's implementation therof) to execute a command on the server, though. In this case, as mentioned by Joop, an Exec channel is the right thing to use. You can use the same Session you have used for your ChannelSftp also for any number of other channels, e.g. for your exec channel. Just make sure to close any channels after use. (Have a look at the Shell, Exec or Subsystem Channel page at the JSch wiki for some more info.
This will of course not work if the server doesn't support command execution, e.g. if it is configured to only support SFTP. Then your only solution is to download and re-upload this file.
In SFTP one possibly can execute ! ... which is execute local command.
! cp a.txt a-backup.txt
As JSch also can give an SSH connection, even the exclamation sign is moot.
For code you might find some starting point in the examples. The Exec sample should do.
How can I run a .jar (Java) file on a server using FTP? I use the ftp command to enter the server, provide the IP address, username, password. Is it possible to run the java file?
You cannot invoke non-ftp commands using ftp. Use ssh to access remote shell and invoke your commands.
FTP is File Transfer Protocol. It's supposed to be used for manipulating files only.
If you need to run a Java file, I suggest you use SSH, RSH, Telnet, or some other method of getting an actual shell.
You could use FTP to transfer your jar, and then use a crontab, or any other sort of scheduled task, to actually run it. It is not possible to execute commands using FTP.
I believe you mean using ssh command to enter the server and if so
I think this might be a starting point :
Can I run .jar file on Tomcat?
if not I don't think its possible.
My desktop is windows. And I would like to connect a Linux server remotely. The middle target is to using script to ssh(using password, not key pairs) remote linux server from windows and run a command. I have putty installed in my machine. The final target here is that I could use java to collect output from this script(remote connect a server via ssh password and run a command) and then parse the output and extract the data I want and put it in excel sheet. The remote server needs login and password.
Why don't you try JSch?
use jsch of jcraft
here is one example
http://www.jcraft.com/jsch/examples/JTAJSch.java
you can find it on SO as well
Jsch or SSHJ or Ganymed SSH-2?
you could run plink (from PuTTY's developers) for running batches and writing the output to files, then process that with Java.
I'm not sure how simple linking Java to PuTTY would be.