I am trying to write java code to execute remote commands on an SSH, similar to the one in this library: Chilkat Android SSH Execute Remote Commands.
However, I do not know where to begin. I do not need to implement the entire library, I only need to be able to send messages from my computer to a device through a SSH using Java code. Specifically I want to implement the Connect, AuthenticatePw and quickCommand methods in the library.
My question is what do I need to learn in order to be able to write a java file that does this? I would appreciate any links to relevant tutorials.
Related
I'm developing a java application from a ubuntu machine and I want to reach a remote file that resides on a ubuntu machine, I want a java code that makes me able to rename or delete this file. I have no idea about how to achieve this task.
Anyone can help me and thanks.
To go about this, first of you have to break the problem into two parts.
First part is connecting to the remote computer. So you will have to look for a better way to connect to a remote computer with java. For this there are several ways to go about it.
You can google it to get a head start. And then come back here when you get stuck
Also you can check out the responses in this link. They will help you out
how to connect remote windows machine by java?
The second phase is doing IO (input output) operations after a successful connection.
Here is a link for sample code with JsCh
http://www.jcraft.com/jsch/examples/Shell.java.html
I have a use case where in I have to connect to a CLI and execute commands in that CLI using java. Usually without using java, I do it by opening a linux terminal and connecting to other CLI and execute commands there. I have to implement the same using Java. I am able to run the commands on the linux terminal using Runtime.getRuntime().exec(). But, I need some help in executing the commands after connecting to a particular CLI from linux terminal using Java
I think you are asking how to make use of pseudo-terminals. I was going to warn you this was likely more complicated than you were bargaining for, but a short search showed the pty4j library which may be of help.
When you run an external program using Runtime.exec(), it returns a Process object that you can use to interact with the running process. That object has a getOutputStream() method that you can use to send commands to the process, and, getInputStream() and getErrorStream() methods that you can use to read messages produced by the process.
Well what I am trying to do, in the long run, is to change some LAN properties when the an ethernet cable is connected to a computer.
I want to run my Java program each time a LAN network is detected. I found a couple questions as to how to do this in C++, but nothing related to Java, specifically. Would this just involve the way I distribute my final application? As in, I could use Jar2Exe Wizard to package my Java program as a Windows service and then just figure out how to run that at startup. But is there any way to do this within the Java program itself?
Don't know what your specific need is but you could try this. This shows how to run windows commands from inside java so you wont have to create an external batch file.
How to Execute Windows Commands Using Java - Change Network Settings
Also check out this answer which talks about retrieving network name in java. then you can combine both!
How to get the wifi network interface name in java
I'm trying to find a way to duplicate a series of files/folders from one section on a server, to a new directory on the same server. I looked into doing this with FTP, but it seems to be strictly for transfer and not so much changing files on the server itself. As such, I've been looking into SSH and wondering if it might be possible that way. Is it possible? If not, is there another way, or perhaps and easier way to do this? Any help would be much appreciated thanks!
I'm not sure if this is the best way, but, if you have SSH access to the server, you can SSH in and use the system's native copy command. I would recommend you use the Jsch library to SSH into the box and then just call the appropriate command (cp or copy).
Jsch supplies no documentation, but they include tons of example code. You can follow the Exec.java file to show you how to execute commands on the remote server. Also, fyi, they use tons of swing code in their examples. You can easily remove all of that if you don't want swing username/password prompts.
Yeah, both FTP and SFTP (which is the file transfer mode of SSH) are mainly for transferring files between client and server. Additionally they also support some management tasks (like creating directories, setting modes, listing files, removing files/directories, even renaming files), but no copying of files without downloading and uploading again.
As Jon7 and Mark proposed, you can (per ssh) invoke the remote server's native copy command (copy or xcopy on Windows, cp on Unix-like systems) to do the job, assuming you have shell access (not only SFTP or some forced command).
If using JSch, an exec channel would be the thing to use here.
I want to connect a remote machine(has a Linux operation system) from my Java Program and I want to run a script on it. I will process the result of that script(it writes something to console) too.
What do you suggest for me? If anything needed I can explain more.
There are SSH libraries in java. you can use them to execute scripts on a remote machine.