Telnet into host and execute commands - java

I am using the Apache commons-net lib to telnet into a PC.
After I am done connecting to this server, I want to be able to execute commands on it(server).
What is the best way to achieve this in Java?

You're better off using JSch to telnet to your remote machine and execute the commands. Checkout the examples sections - it has everything you need.

Related

Java SSH Client (running commands and catching answers) Remote Device

I want to implement application working on Internet (I have remote Devices connected using Internet), I want to develop Java application to perform changes on remote devices....
I found this link example/code (http://twit88.com/blog/2007/12/22/java-writing-an-automated-telnet-client/) working in Telnet but is insecure I want to use SSH.
JSCH can help me (http://www.jcraft.com/jsch/)? or have you another alternative.
If you have similar code....
Thank you
JSch is be the best solution.
You can use my code snippet as an example:
https://gist.github.com/kobylinsky/1c2b21c73b68a0daa91f

Connect the other user through remote machine on local machine?

I wanted to connect a Remote machine using java and have to run some commands from cmd prompt. To do this, I need a confirmation the tool (AutoIt) which i picked is working fine for me. Is there any possible to connect two users in our laptop and connect the other user by remote machine.. so that i can verify and proceed..
Any possibility to write a program to ssh to remote machine and execute some commands?
You can use JSCH
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

how to access a remote database in java which can be accessed only through ssh

I need to access the remote MYsql database from java which can be accessed only through ssh
SSH tunneling works perfectly fine for my application as most of the linux systems come with built in ssh client.
java.sql.Connection extension for SSH
what if i need to access the database from windows system, by using putty or some other ssh client.
can we do ssh tunneling in windows system as if in ubuntu systems.:)
Yes, you can, see Using PuTTY under Windows to create an SSH tunnel to your NetManager. Basically you need to set up a tunnel to the mysql port and then you can use the JDBC as natural without it being aware of the tunneling happening
There are plenty of SSH clients for java:
Apache MINA SSH
Java Secure Channel (JSCH)
github.com/shikhar/sshj
http://orion-ssh2.sourceforge.net/
Probably you can take a look and see who is going to work for you, but I think jsch could solve your probelm. See examples here

Connect to Unix File System using Java Program

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.

Executing batch file on remote system using Java

How to execute a batch file located on Windows remote system? Batch file should run on remote system.
Abhinav,
For your problem I see RMI is the quickest possible solution
Check out the basics from these links (1,2)
Start the server from where you want to run the batch
In the Remote object on the server side Use Runtime.getRuntime().exec() to run your batch.
From the client machine give call to this remote object and method.
Another approach is to use SSH like sshj. This only requires the remote system to have SSH installed and is more secure than RMI.
You can use Jsch and Expect4j for executing commands on remote machine(window/Linux). Further more, if your system allows, transfer the batch file on remote machine using some FTP utility like Apache Commons Net and then execute commands that executes the script.

Categories