Simple way to keep files updated from remote server using java? - java

I was hoping to use Java to access a remote server, and access the files and keep my files updated, or update the files on the server.
Is there a simple way to access a remote server using the username#host and password, which would allow me to upload and download files?
Thanks

You can use JSch to access files via ssh remotely.

Use the proper tool for the job: rsync

If you like to connect to a machine while opening a ssh connection ,
to run OS command you can use trilead.
This is an example to a method that will open a connection.
public static Connection newConnectionNoPassword(String host, String username, File privateKey) {
Connection newConn = new Connection(host);
try {
newConn.connect(); // Ignoring ConnectionInfo returned value.
//If the authentication was successful the authenticated connection will be returend
if ( newConn.authenticateWithPublicKey(username, privateKey, null)){
return newConn;
}else{
newConn.close();
return null;
}
} catch (IOException ioe) {
newConn.close();
ioe.printStackTrace();
return null;
}
}
If you use maven you can get it by adding the following dependency to your pom.xml:
<dependency>
<groupId>com.trilead</groupId>
<artifactId>trilead-ssh2</artifactId>
<version>build213-svnkit-1.3-patch</version>
</dependency>
In order to upload\download files from\to a server you can use trilead SCPClient.
Here is an example of downloading files from a remote server to a local folder:
public void downloadFiles(String[] remoteFiles, String localDir) throws IllegalArgumentException, IOException {
checkNotEmpty(localDir);
checkNotEmpty(remoteFiles);
File dir = new File(localDir);
if (!dir.exists() || !dir.mkdirs()) {
throw new IOException("Failed to create local directory : " + localDir);
}
SCPClient scp = new SCPClient(this.conn);
try {
scp.get(remoteFiles, localDir);
} catch (IOException e) {
throw new IOException("Failed to copy remote files to local folder", e);
}
}
Hope it helps..

Related

Send files to server via FTPS protocol

I'm creating an app which generates a CSV file and some PDFs. I want my app to send those files to a server via FTPS protocol.
I'm using Apache Commons Net FTP library and it was perfectly working when I had "Require TLS session resumption on data connection when using PORT P" unchecked, but since I enabled it I can't send my files.
An error appeared :
450 TLS session of data connection has not resumed or the session does not match the control connection.
After some researches on this site I have overriden _prepareDataSocket_ in order to overcome this problem but now it just creates empty files on the server.
There is my overriden function :
#Override
protected void _prepareDataSocket_(final Socket socket) throws IOException {
if (socket instanceof SSLSocket) {
// Control socket is SSL
final SSLSession session = ((SSLSocket) _socket_).getSession();
if (session.isValid()) {
final SSLSessionContext context = session.getSessionContext();
try {
final Field sessionHostPortCache = context.getClass().getDeclaredField("sessionHostPortCache");
sessionHostPortCache.setAccessible(true);
final Object cache = sessionHostPortCache.get(context);
final Method method = cache.getClass().getDeclaredMethod("put", Object.class, Object.class);
method.setAccessible(true);
method.invoke(cache, String
.format("%s:%s", socket.getInetAddress().getHostName(), String.valueOf(socket.getPort()))
.toLowerCase(Locale.ROOT), session);
method.invoke(cache, String
.format("%s:%s", socket.getInetAddress().getHostAddress(), String.valueOf(socket.getPort()))
.toLowerCase(Locale.ROOT), session);
} catch (NoSuchFieldException e) {
throw new IOException(e);
} catch (Exception e) {
throw new IOException(e);
}
} else {
throw new IOException("Invalid SSL Session");
}
}
}
and this is what FileZilla Server displays:
FileZilla Response
will this answer on another forum help?
http://forum.rebex.net/5673/450-error-connecting-to-ftp-requiring-explicit-ftp-over-tls

Collecting command responses from mocked SSH server (Apache MINA)

So what I'm trying to do, is create unit test that checks if invoked command (on shell via ssh connection) has a proper response. The problem is that I can't read those responses. There are not many tutorials regarding Apache MINA, so I thought maybe some of you could help me out. Here's a code
#Before
public void setUpSSHd() {
sshd=SshServer.setUpDefaultServer();
sshd.setPort(22999);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String username, String password, ServerSession session) {
// TODO Auto-generated method stub
return true;
}
});
List<NamedFactory<KeyExchange>> keyExchangeFactories;
keyExchangeFactories = sshd.getKeyExchangeFactories();
sshd.setKeyExchangeFactories(keyExchangeFactories);
try {
sshd.start();
} catch (Exception e) {
e.printStackTrace();
}
}
#After
public void teardown() throws Exception { sshd.stop(); }
#Test
public void testCommands() throws Exception {
SshClient client = SshClient.setUpDefaultClient();
client.start();
ClientSession session = null;
try {
session = client.connect("localhost", 22999).await().getSession();
session.authPassword("none", "none").await().isSuccess();
System.out.println("Connection established");
final ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
ByteArrayOutputStream sent = new ByteArrayOutputStream();
PipedOutputStream pipedIn = new TeePipedOutputStream(sent);
channel.setIn(new PipedInputStream(pipedIn));
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
channel.setOut(out);
channel.setErr(err);
channel.open();
pipedIn.write("dir\r\n".getBytes());
pipedIn.flush();
channel.waitFor(ClientChannel.CLOSED, 0);
channel.close(false);
client.stop();
System.out.println(out.toString());
} catch (Exception e) {
e.printStackTrace();
fail("Cannot establish a connection");
} finally {
if (session != null)
session.close(true);
}
}
For now I simply try to print out collected response. However I get empty string everytime I try to do that. I assume there might be a problem with ssh server configuration (what shell is it supposed to use?). The best scenario would be if I could define my own commands and responses on server side and then, only check it on client side
EDIT: I've tried to manually connect to this mocked ssh server but I've got
Unable to negotiate with ::1 port 22999: no matching key exchange method found. Their offer: diffie-hellman-group1-sha1
error message.
I would suggest you to update Apache SSH. Based the source repository the version 0.5.0 is 7 years old.
using your posted code with the default JCE provider and Apache SSH
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.5.0</version>
<dependency>
the connect with a ssh client fails with
Their offer: diffie-hellman-group1-sha1
using a more recent Apache SSH release
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>1.4.0</version>
<dependency>
the connect is successful

how can i handle multiple server MongoDB

Hi all i am new to spring maven project, and i am using MongoDB. I want to use two tomcats/ MongoDB both of theri IP address are different. when first DB is down i need to connect with second one how it is possible
I am using following code
public boolean mongoRunningAt(String uri) {
try {
Mongo mongo = new Mongo(new MongoURI(uri));
try {
Socket socket = mongo.getMongoOptions().socketFactory.createSocket();
socket.connect(mongo.getAddress().getSocketAddress());
socket.close();
} catch (IOException ex) {
mongo = new Mongo(new MongoURI(uri_second));
Socket socket = mongo.getMongoOptions().socketFactory.createSocket();
socket.connect(mongo.getAddress().getSocketAddress());
socket.close();
//return false;
}
mongo.close();
return true;
} catch (UnknownHostException e) {
return false;
}
}
Using this code i tried with first one successfully connected, now stoped first DB now restarted server it is connected with second db.
But if i didn't restart server it is always pointing to First only... how should i work on this
Thanks in advance
You deployed 2 servers, are they in a replica set. If not you can follow the link.
When they are already in a replica set you can use a connectionstring containing the 2 servers.
Like this:
mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test

FTPSClient.listFiles() not working for NonStop/Tandem System

I am writing a small FTPS client that will download Enscribe files from NonStop/Tandem and will be processes in Windows. I am using the Apache Commons Net API to achieve this.
I am able to download and upload files from and to NonStop/Tandem. But I am not able to list the files and the directories using the listFiles() and/or mlistDir() methods present under org.apache.commons.net.ftp.FTPClient class.
Below is my code to list the files present in the current working directory.
FTPSClient client = new FTPSClient(false);
try {
client.connect(serverAddress, serverPort);
if (FTPReply.isPositiveCompletion(client.getReplyCode())) {
if (client.login(userName, passwd)) {
System.out.println(client.getReplyString());
// Set protection buffer size
client.execPBSZ(0);
// Set data channel protection to private
client.execPROT("P");
// Enter local passive mode
client.enterLocalPassiveMode();
// Get Current Working Directory
client.printWorkingDirectory();
System.out.println(client.getReplyString());
FTPFile[] files = client.listFiles();
// Logout
client.logout();
System.out.println(client.getReplyString());
} else {
System.out.println("Login failed...");
}
// Disconnect from Server
client.disconnect();
System.out.println("Disconnected from Host...");
} else {
System.out.println("Connection to Host failed...");
System.out.println("Error Code - " + reply);
}
} catch (Exception e) {
e.printStackTrace();
}
I get the following error while executing the code:
org.apache.commons.net.ftp.parser.ParserInitializationException: Unknown parser type: Nonstop J-series Server : J06.19.
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:169)
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:94)
at org.apache.commons.net.ftp.FTPClient.__createParser(FTPClient.java:3377)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3334)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3012)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3065)
at com.connect.ssl.FTPSTest.main(FTPSTest.java:57)
I even tried to set the FTPClient configuration as UNIX as below, but it didn't helped.
client.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
Can anyone help me with this.

How to create a "FTPS" Mock Server to unit test File Transfer in Java

I have a CreateFTPConnection class which create a FTPS connection. Using this connection, files are transferred. Here is the code of TransferFile class
public class TransferFile
{
private CreateFTPConnection ftpConnection;
private FTPSClient client;
public TransferFile(CreateFTPConnection ftpConnection) {
this.ftpConnection = ftpConnection;
this.client = ftpConnection.getClient();
}
public void transfer(Message<?> msg)
{
InputStream inputStream = null;
try
{
if(!client.isConnected()){
ftpConnection.init();
client = ftpConnection.getClient();
}
File file = (File) msg.getPayload();
inputStream = new FileInputStream(file);
client.storeFile(file.getName(), inputStream);
client.sendNoOp();
} catch (Exception e) {
try
{
client.disconnect();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
finally
{
try {
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
I have to write jUnit Testcase for this class. For this, I have to create a FTPS Mock Server connection and have to use that connection to test the File Transfer. So can anyone plz give me any idea of how to make FTPS Mock Server and do the test case. I googled on this, but what I get is on FTP or SFTP, not FTPS. Please help me.
You might find this useful MockFTPServer
The issue is that these mock servers don't implement the TLS portion from what I can see. You may need to do a little work to allow connections via TLS.
You should be able to search around and find some articles here on SO about dealing with certificates, (or in some cases, bypassing them) for the sake of your testing.
Here's another Article that goes through the steps of creating a basic FTP server Test.
Short of a full blown FTP server (Apache http w/ mod_ftp add on), there doesn't seem to be anything useful to do this.

Categories