Is it possible for a JAVA application to get the file system directory structure of another host on the same LAN?
For example:
Machines A and B are on the same LAN. A is the localhost and B is the remote host. Is it possible for a JAVA application executing on A to get the directory structure (essentially a 'ls -R') of B?
One way to do this is by issuing the ls -R command over the ssh protocol.
For ssh library recommendations, see SSH library for Java
Yes, although you'd have to use something like SSH to open a remote terminal session to execute the ls command. There's no way to natively do that with just Java without opening a socket and machine B somehow cooperating with the request. (That would be a huge security hole if you could...)
Related
I wanna programmatically (using Java's Runtime.exec) open Putty on my system and connect to a remote linux system. Is there any syntax to pass Putty private key to the method runtime.exec and connect successfully. I am getting "Access Denied" error on the remote system if I just pass the username and pass-phrase and try to connect.
I am assuming by your comments that you are trying to actually run some commands on a linux server, like
find /some-directory/ -type f -ecec grep -Hnw this-word {} ";"
via bash or other shell as opposed to rcp.
If yes then stop using putty, that is an interactive client for you to login to a remote server and interact with the connected shell.
I would suggest that you would be far better off using something like jssh, assuming it is still a current library.
With that you get fine control of the interaction flow with the remote server and iirc it supports private/public keys.
I am trying to make a terminal emulator in Java. The java program will accept the commands from user, and show its output to them. I can emulate simple commands like 'ls', but I don't know how to handle commands like 'cd'. This is because, I am using exec() method for executing terminal commands. So, all the commands are executed at current directory. The commands like 'cd ..' are executed, but then they have no persistent effect, because each command is separately executed by exec().
Any Ideas How I can emulate a whole session??
If you are executing commands with exec(), you are not writing a terminal emulator; you are writing a shell. In that case, you will need to keep track of things the shell keeps track of, like environment variables and working directory.
If you really want to write a terminal emulator, you would be talking to a shell process through a pseudo-terminal. Then your program would just be keeping track of the things a terminal keeps track of, like the line state and what appears on the screen.
Working with a pseudo-terminal from Java will be a little tricky, because most of the documentation assumes you are using a C api. man pty should get you started. Your Java process will have to open the master side of the pseudo-terminal with FileStream objects. I'm not sure there is a way within Java to get a child process to open the slave side of the pseudo-terminal; you might have to invoke a shell command with exec() that starts another shell command with standard input/output/error redirected to the slave side of the pseudo terminal.
JSch is a pure Java implementation of SSH2.
JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs.
http://www.jcraft.com/jsch/
You should really give a try to Ganymed.
Ganymed SSH-2 for Java is a library which implements the SSH-2
protocol in pure Java (tested on J2SE 1.4.2 and 5.0). It allows one to
connect to SSH servers from within Java programs. It supports SSH
sessions (remote command execution and shell access), local and remote
port forwarding, local stream forwarding, X11 forwarding, SCP and
SFTP.
http://www.ganymed.ethz.ch/ssh2/
Ganymed along with apache FTP client you can also download and upload files.
Also there is a inbuilt example code for terminal emulation in Ganymed.
The following is a link to a project which is did using Ganymed along with apache FTP client.
GITHUB
Happy Coding!!
The question us also related to linux but solution is needed for Java. So I have a data directory
/somedir/data
on linux server
servername
I can ssh to the server and do anything I want only from deployment machine (due public/private keys in place). But there's a Java process that should read files from that directory. How can I force it read that files? I was trying to use File("//servername/somedir/data") with no success. Any help would be appreciated.
You must share the file using one of the network file services.
For example:
NFS (check with showmount -e);
Samba (check with smbclient -L);
AFS;
HTTP/FTP (check first if there a HTTP/FTP-server on the host).
You can also access this file using SSH (you say that you have SSH connection to the host, that means that SSH is accessible anyway).
If you want to connect to the SSH server from Java program,
you can use (for example) JSch for that.
Example of JSch usage is here.
I want to establish a connection with my UNIX file system using java program.. So that I can make some File I/O operations and normally I can connect using Putty.
How can I do the same using java program
I have the Host name, username,password and Port number
Help appreciated :)
You need several things:
A server that takes commands (create directory, list directory, write data to a file, read data from a file) over the network. This server should listen to port1 on localhost
You need to configure putty to forward port2 on your local computer to port1 on the server.
A local client which allows you to connect to port2 on your local computer. Putty will tunnel any data send to port2 to port1 on the remote server and vice versa.
Or you get WinSCP which uses the SSH protocol (just like Putty) and maybe already does what you want.
There's a pure Java implementation of SSH/SCP available: http://www.cleondris.ch/opensource/ssh2/
You can use its SCPClient or SFTPv3Client classes to work on the remote file system.
Documentation is available at http://www.cleondris.ch/opensource/ssh2/javadoc.
If you want to do it from Java, you can use Apache Commons VFS. It provides a common approach to dealing with files on all of the supported file systems. SFTP is one of the supported types which is most likely what you would need if you have been connecting with PuTTY.
You need SSH client. There are various pure java SSH clients. Google "java ssh client" and try any one of them. I used Jsch http://www.jcraft.com/jsch/ and it worked fine for me.
What I'm attempting to do is copy a number of files from one host machine to a remote server using java and after the copy is made, I'll execute those files that I transferred. The host machine may have some dependencies like requiring putty or some other program but I'm hoping that there might be a solution that doesn't require anything installed on the remote side. And on top of that, this needs to be OS independent, though different methods can be used for different communications. I'll have access to the IP address and admin control (root username and password).
What I've had so far was that for Windows to Windows, I can mount the remote windows drive and access the files that way. In Windows to Linux, I can use putty or a similar program to ssh into the remote box. I'll also ssh from Linux to Linux and obviously I won't need putty. I can't figure out what to do for a Linux to Windows instance that won't require me setting up some ssh method on the remote end. Any ideas? Any way (or library) to perform both the copy and/or execute methods that won't even be OS specific?
A simple solution is to use what Windows already offers: rdesktop or the more comfortable Terminal Server Client if you are on a Gnome machine.
To get the files to the Windows box, you can set up Samba Client on your Linux machine and mount the Windows file share, copy your files there, connect via rdesktop and then execute them.