From c91f4d8cc45aa50522b236ec610176925b460024 Mon Sep 17 00:00:00 2001 From: Stefano Eutizi Date: Sat, 30 Nov 2019 19:11:55 +0100 Subject: [PATCH] Import of project --- .classpath | 6 + .gitignore | 1 + .project | 17 ++ README.md | 26 ++ configuration.properties | 71 +++++ configuration.properties_example2 | 34 +++ src/config/carica.java | 18 ++ src/main/put.java | 65 +++++ src/main/testFTP.java | 121 ++++++++ src/main/trasferisci.java | 455 ++++++++++++++++++++++++++++++ 10 files changed, 814 insertions(+) create mode 100755 .classpath create mode 100755 .gitignore create mode 100755 .project create mode 100755 configuration.properties create mode 100755 configuration.properties_example2 create mode 100755 src/config/carica.java create mode 100755 src/main/put.java create mode 100755 src/main/testFTP.java create mode 100755 src/main/trasferisci.java diff --git a/.classpath b/.classpath new file mode 100755 index 0000000..fb50116 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100755 index 0000000..0fafe64 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + SFTP-SSL-Client + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/README.md b/README.md index b5b61bf..6911e15 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ # SFTP-SSL-Client A Java SFTP/FTPS Client for command line file transfer upload/download + +You need it.sauronsoftware.ftp4j to get client working + +#Transfer mode +there is three types of file transfer: +- All files in a directory +- A fixed list of files +- A variable file name with date in it (dynamic) + + +#Proxy +You can set different proxy types: +HTTPTunnelConnector +FTPProxyConnector +SOCKS4Connector +SOCKS5Connector + + +#EXTRA +The script allow you to: +- set file permission (linux chmod) +- execute a custom command, the file is passed as an argument + + +## P.S. +Sorry but the English translation is in progress, parameters and classes are an half in Italian \ No newline at end of file diff --git a/configuration.properties b/configuration.properties new file mode 100755 index 0000000..923e89e --- /dev/null +++ b/configuration.properties @@ -0,0 +1,71 @@ +# tipo di traferimento: download | upload +TIPO_TRAFERIMENTO=upload + +# logleve: INFO | DEBUG +LOGLEVEL=DEBUG + +#SSL activation (SSL|SSLv3|false) +SSL=SSLv3 +# SSL type (IMPLICIT|EXPLICIT) +SSL_TYPE=EXPLICIT + +# passive mode: true o false +PASSIVE=true + +# tipo trasferimento: AUTO, BINARY, TEXTUAL +TRANSFER_TYPE=AUTO + +# Eliminazione dei file/s sorgenti +DELETE_SOURCE_FILES=false + +# se download posso impostare i permessi del file es: 644 +is_set_PERMESSI=false +PERMESSI=644 + + +# dati connessione +IP=rmftp1.postel.com +PORTA=2110 +USER=2012294 +PW=YMXXXXXX + +#proxy true|false +proxy=false +# type: HTTPTunnelConnector | FTPProxyConnector | SOCKS4Connector | SOCKS5Connector +proxy_type=SOCKS5Connector +proxy_ip=192.168.78.69 +proxy_port=1080 +# Proxy is authenticated +proxy_auth=false +proxy_user= +proxy_pass= + + + +# numero di file da trasferire, da inserire un percorso per ogni file +NUMERO_FILE=1 + +# trasferimento file: +# - dinamico (sostituisce %data% dei nomi file con la data odierna nel formato specificato) +# - statico (copia i files specificati come ORIGINE - DESTINAZIONE e NUMERO_FILE) +# - all_files (copia tutti i files nella dalla cartella di input nella cartella di output) +TRASFERIMENTO_FILE=all_files + +# formato della data es: yyyy-MM-dd HH:mm:ss.S +FORMATO=yyyyMMdd + +# comando da eseguire su ogni file, indicare se necessario con %file% il nome del file stesso +is_set_COMANDO=false +COMANDO=dos2unix %file% %file% + +#CARTELLA REMOTA +REMOTE_FOLDER=/lotti/ +#CARTELLA LOCALE +LOCAL_FOLDER=/home/stefano/conservazione/ + +# specifica dei file con percorso completo, origine e destinazione inserire (se dinamico, dove va inserita la data mettere %data% nel punto esatto) +ORIGINE_FILE_1=prova.zip +DESTINAZIONE_FILE_1=prova.zip + + + diff --git a/configuration.properties_example2 b/configuration.properties_example2 new file mode 100755 index 0000000..692f7b2 --- /dev/null +++ b/configuration.properties_example2 @@ -0,0 +1,34 @@ +# tipo di traferimento: download | upload +TIPO_TRAFERIMENTO=download + +# dati connessione +IP=10.207.230.60 +USER=ptc3012 +PW=ptc3012 + +# numero di file da trasferire, da inserire un percorso per ogni file +NUMERO_FILE=4 + +# file dinamico con data +DINAMICO=true +# formato della data es: yyyy-MM-dd HH:mm:ss.S +FORMATO=yyyyMMdd + +#CARTELLA REMOTA +REMOTE_FOLDER=/transfer/bolrep/ +#CARTELLA LOCALE +LOCAL_FOLDER=/web/postalizzazioni/bollettini_telematici/ + +# specifica dei file con percorso completo, origine e destinazione inserire (se dinamico, dove va inserita la data mettere %data% nel punto esatto) +ORIGINE_FILE_1=BollettinoReport55111.%data%.txt +DESTINAZIONE_FILE_1=BollettinoReport55111.%data%.txt + +ORIGINE_FILE_2=BollettinoReport55111.%data%.xml +DESTINAZIONE_FILE_2=BollettinoReport55111.%data%.xml + +ORIGINE_FILE_3=BollettinoReport77555.%data%.txt +DESTINAZIONE_FILE_3=BollettinoReport77555.%data%.txt + +ORIGINE_FILE_4=BollettinoReport77555.%data%.xml +DESTINAZIONE_FILE_4=BollettinoReport77555.%data%.xml + diff --git a/src/config/carica.java b/src/config/carica.java new file mode 100755 index 0000000..4a47960 --- /dev/null +++ b/src/config/carica.java @@ -0,0 +1,18 @@ +package config; + +public class carica { + + /** + * @param args + */ + public static void main(String[] args) { + // TODO Auto-generated method stub + + + + + + + } + +} diff --git a/src/main/put.java b/src/main/put.java new file mode 100755 index 0000000..a504204 --- /dev/null +++ b/src/main/put.java @@ -0,0 +1,65 @@ +package main; + +import it.sauronsoftware.ftp4j.FTPAbortedException; +import it.sauronsoftware.ftp4j.FTPClient; +import it.sauronsoftware.ftp4j.FTPDataTransferException; +import it.sauronsoftware.ftp4j.FTPException; +import it.sauronsoftware.ftp4j.FTPFile; +import it.sauronsoftware.ftp4j.FTPIllegalReplyException; +import it.sauronsoftware.ftp4j.FTPListParseException; + +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.cert.X509Certificate; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +public class put{ +/* + public static void main(String[] args) throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException, FTPListParseException{ + TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() { + public X509Certificate[] getAcceptedIssuers() { + return null; + } + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + } }; + + SSLContext sslContext = null; + try { + sslContext = SSLContext.getInstance("SSL"); + sslContext.init(null, trustManager, new SecureRandom()); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } + SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); + FTPClient client = new FTPClient(); + client.setSSLSocketFactory(sslSocketFactory); + //client.setSecurity(FTPClient.SECURITY_FTPS); // implicito + client.setSecurity(FTPClient.SECURITY_FTPES); // esplicito + + client.connect("rmftp1.postel.com", 2110); + client.login("2012294", "YM372619"); + + FTPFile[] list = client.list(); + + for (FTPFile ftpFile : list) { + + System.out.println(ftpFile.getName()); + } + + client.disconnect(true); + + } +*/ + + +} \ No newline at end of file diff --git a/src/main/testFTP.java b/src/main/testFTP.java new file mode 100755 index 0000000..ee8dae8 --- /dev/null +++ b/src/main/testFTP.java @@ -0,0 +1,121 @@ +package main; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Properties; + +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPClientConfig; + +public class testFTP { + + /** + * @param args + */ + public static void main(String[] args) { + + FTPClient client = null; + + try { + Properties properties = new Properties(); + properties.load(new FileInputStream("configuration.properties")); + + String TIPO_TRAFERIMENTO = properties.getProperty("TIPO_TRAFERIMENTO"); + String IP = properties.getProperty("IP"); + String USER = properties.getProperty("USER"); + String PW = properties.getProperty("PW"); + String NUMERO_FILE = properties.getProperty("NUMERO_FILE"); + String DINAMICO = properties.getProperty("DINAMICO"); + String FORMATO = properties.getProperty("FORMATO"); + String REMOTE_FOLDER = properties.getProperty("REMOTE_FOLDER"); + String LOCAL_FOLDER = properties.getProperty("LOCAL_FOLDER"); + String PERMESSI = properties.getProperty("PERMESSI"); + String COMANDO = properties.getProperty("COMANDO"); + +// CONNESSIONE + client = new FTPClient(); + FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX); + client.configure(conf); + client.connect(IP); + client.login(USER, PW); + +// COSTRUISCO DUE ARRAY CON I FILE SORGENTE E DESTINAZIONE + int numero_files = Integer.parseInt(NUMERO_FILE); + String[] sorgenti = new String[numero_files]; + String[] destinazioni = new String[numero_files]; + + if (DINAMICO.equals("true")) { + Date date = Calendar.getInstance().getTime(); + DateFormat formatter = new SimpleDateFormat(FORMATO); + String today_formattato = formatter.format(date); + + System.out.println("Data costruita: " + today_formattato); + + for (int i = 1; i <= numero_files; i++) { + String sorgente = properties.getProperty("ORIGINE_FILE_" + i); + String destinazione = properties.getProperty("DESTINAZIONE_FILE_" + i); + + sorgenti[i - 1] = sorgente.replaceAll("%data%", today_formattato); + destinazioni[i - 1] = destinazione.replaceAll("%data%", today_formattato); + } + } else { + for (int i = 1; i <= numero_files; i++) { + String sorgente = properties.getProperty("ORIGINE_FILE_" + i); + String destinazione = properties.getProperty("DESTINAZIONE_FILE_" + i); + + sorgenti[i - 1] = sorgente; + destinazioni[i - 1] = destinazione; + } + } + + for (int i = 0; i < sorgenti.length; i++) { + + String s = sorgenti[i]; + String d = destinazioni[i]; + + client.changeWorkingDirectory(REMOTE_FOLDER); + client.setFileType(FTPClient.BINARY_FILE_TYPE); + System.out.println(TIPO_TRAFERIMENTO + " del file remoto: " + REMOTE_FOLDER + s + " locale: " + LOCAL_FOLDER + d); + + if (TIPO_TRAFERIMENTO.equals("download")) { + String f = LOCAL_FOLDER + d; + FileOutputStream fos = new FileOutputStream(f); + client.retrieveFile(s, fos); + Runtime.getRuntime().exec("chmod " + PERMESSI + " " + f); + String comando_dopo_replace = COMANDO.replaceAll("%file%", f); + Runtime.getRuntime().exec(comando_dopo_replace); + } else if (TIPO_TRAFERIMENTO.equals("upload")) { + String lfile = LOCAL_FOLDER + s; + File fi = new File(lfile); + FileInputStream fin = new FileInputStream(fi); + client.storeFile(s, fin); + } + } + } catch (IOException e) { + System.out.println("###########################"); + System.out.println("Properties File Not Found"); + System.out.println("###########################"); + e.printStackTrace(); + } catch (IllegalStateException e) { + System.out.println("###########################"); + System.out.println("FTP Connection Error"); + System.out.println("###########################"); + e.printStackTrace(); + e.printStackTrace(); + } finally { + if (client != null) { + try { + client.disconnect(); + } catch (Exception e) { + System.err.println("ERROR : Error in disconnecting the Remote Machine"); + } + } + } + } +} diff --git a/src/main/trasferisci.java b/src/main/trasferisci.java new file mode 100755 index 0000000..0a1acba --- /dev/null +++ b/src/main/trasferisci.java @@ -0,0 +1,455 @@ +package main; + +import it.sauronsoftware.ftp4j.FTPAbortedException; +import it.sauronsoftware.ftp4j.FTPClient; +import it.sauronsoftware.ftp4j.FTPDataTransferException; +import it.sauronsoftware.ftp4j.FTPException; +import it.sauronsoftware.ftp4j.FTPFile; +import it.sauronsoftware.ftp4j.FTPIllegalReplyException; +import it.sauronsoftware.ftp4j.FTPListParseException; +import it.sauronsoftware.ftp4j.connectors.FTPProxyConnector; +import it.sauronsoftware.ftp4j.connectors.HTTPTunnelConnector; +import it.sauronsoftware.ftp4j.connectors.SOCKS4Connector; +import it.sauronsoftware.ftp4j.connectors.SOCKS5Connector; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.SocketTimeoutException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.cert.X509Certificate; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Properties; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +public class trasferisci { + + /** + * @param args + * @throws FTPListParseException + */ + public static void main(String[] args) throws FTPListParseException { + + FTPClient client=null; + + try { + Properties properties = new Properties(); + String TIPO_TRAFERIMENTO = ""; + String IP = ""; + String PORTA="21"; + String USER = ""; + String PW = ""; + String NUMERO_FILE = ""; + String DINAMICO = ""; + String FORMATO = ""; + String REMOTE_FOLDER = ""; + String LOCAL_FOLDER = ""; + String PERMESSI = ""; + String COMANDO = ""; + String LOGLEVEL = ""; + int NUMERO_RETRY_ERROR = 100; + + String is_set_PERMESSI = ""; + String is_set_COMANDO = ""; + String PASSIVE = ""; + String TRANSFER_TYPE="AUTO"; + String DELETE_SOURCE_FILES="AUTO"; + String SSL="false"; + String SSL_TYPE="IMPLICIT"; // IMPLICIT | EXPLICIT + + String proxy="false"; + String proxy_type=""; + String proxy_ip=""; + String proxy_port=""; + + String proxy_auth=""; + String proxy_user=""; + String proxy_pass=""; + + + try{ + properties.load(new FileInputStream("configuration.properties")); + + TIPO_TRAFERIMENTO = properties.getProperty("TIPO_TRAFERIMENTO"); + IP = properties.getProperty("IP"); + PORTA = properties.getProperty("PORTA"); + USER = properties.getProperty("USER"); + PW = properties.getProperty("PW"); + NUMERO_FILE = properties.getProperty("NUMERO_FILE"); + DINAMICO = properties.getProperty("TRASFERIMENTO_FILE"); + FORMATO = properties.getProperty("FORMATO"); + REMOTE_FOLDER = properties.getProperty("REMOTE_FOLDER"); + LOCAL_FOLDER = properties.getProperty("LOCAL_FOLDER"); + PERMESSI = properties.getProperty("PERMESSI"); + COMANDO = properties.getProperty("COMANDO"); + is_set_PERMESSI = properties.getProperty("is_set_PERMESSI"); + is_set_COMANDO = properties.getProperty("is_set_COMANDO"); + PASSIVE = properties.getProperty("PASSIVE"); + TRANSFER_TYPE=properties.getProperty("TRANSFER_TYPE"); + DELETE_SOURCE_FILES=properties.getProperty("DELETE_SOURCE_FILES"); + LOGLEVEL=properties.getProperty("LOGLEVEL"); + NUMERO_RETRY_ERROR=Integer.parseInt(properties.getProperty("NUMERO_RETRY_ERROR")); + SSL=properties.getProperty("SSL"); + SSL_TYPE=properties.getProperty("SSL_TYPE"); + proxy=properties.getProperty("proxy"); + proxy_type=properties.getProperty("proxy_type"); + proxy_ip=properties.getProperty("proxy_ip"); + proxy_port=properties.getProperty("proxy_port"); + proxy_auth=properties.getProperty("proxy_auth"); + proxy_user=properties.getProperty("proxy_user"); + proxy_pass=properties.getProperty("proxy_pass"); + } + catch (IOException e) { + System.out.println("###########################"); + System.out.println("Properties file load error"); + System.out.println("###########################"); + e.printStackTrace(); + System.exit(1); + } + + boolean debug=false; + if(LOGLEVEL.equals("DEBUG")) + debug=true; + + //CONNESSIONE + client = new FTPClient(); + + // SE CONNESSIONE SSL + if(!SSL.equals("false")){ + System.out.println("Uso SSL "+ SSL_TYPE); + TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() { + public X509Certificate[] getAcceptedIssuers() { + return null; + } + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + } }; + + SSLContext sslContext = null; + try { + sslContext = SSLContext.getInstance(SSL); + sslContext.init(null, trustManager, new SecureRandom()); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + System.exit(1); + } catch (KeyManagementException e) { + e.printStackTrace(); + System.exit(1); + } + SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); + client.setSSLSocketFactory(sslSocketFactory); + + if(SSL_TYPE.equals("IMPLICIT")) + client.setSecurity(FTPClient.SECURITY_FTPS); // implicito + else + client.setSecurity(FTPClient.SECURITY_FTPES); // esplicito + } + + // se proxy + if(proxy.equals("true")){ + System.out.println("Uso il Proxy "+ proxy_ip+" Porta "+proxy_port); + if(proxy_type.equals("HTTPTunnelConnector")){ + if(proxy_auth.equals("true")) + client.setConnector(new HTTPTunnelConnector(proxy_ip, Integer.parseInt(proxy_port),proxy_user,proxy_pass)); + else + client.setConnector(new HTTPTunnelConnector(proxy_ip, Integer.parseInt(proxy_port))); + }else if(proxy_type.equals("FTPProxyConnector")){ + if(proxy_auth.equals("true")) + client.setConnector(new FTPProxyConnector(proxy_ip, Integer.parseInt(proxy_port),proxy_user,proxy_pass)); + else + client.setConnector(new FTPProxyConnector(proxy_ip, Integer.parseInt(proxy_port))); + + }else if(proxy_type.equals("SOCKS4Connector")){ + if(proxy_auth.equals("true")) + client.setConnector(new SOCKS4Connector(proxy_ip, Integer.parseInt(proxy_port),proxy_user)); + else + client.setConnector(new SOCKS4Connector(proxy_ip, Integer.parseInt(proxy_port))); + }else if(proxy_type.equals("SOCKS5Connector")){ + if(proxy_auth.equals("true")) + client.setConnector(new SOCKS5Connector(proxy_ip, Integer.parseInt(proxy_port),proxy_user,proxy_pass)); + else + client.setConnector(new SOCKS5Connector(proxy_ip, Integer.parseInt(proxy_port))); + } + } + + client.connect(IP, Integer.valueOf(PORTA)); + client.login(USER, PW); + + client.setPassive(new Boolean(PASSIVE).booleanValue()); + + if(TRANSFER_TYPE.equals("AUTO")) + client.setType(FTPClient.TYPE_AUTO); + else if(TRANSFER_TYPE.equals("BINARY")) + client.setType(FTPClient.TYPE_BINARY); + else if(TRANSFER_TYPE.equals("TEXTUAL")) + client.setType(FTPClient.TYPE_TEXTUAL); +//############################################################################### + FTPFile[] list = client.list(); + + for (FTPFile ftpFile : list) { + + System.out.println("FILES TO TRANSFER:" + ftpFile.getName()); + } +//############################################################################### + // COSTRUISCO DUE ARRAY CON I FILE SORGENTE E DESTINAZIONE + int numero_files=Integer.parseInt(NUMERO_FILE); + + String[] sorgenti=new String[numero_files]; + String[] destinazioni=new String[numero_files]; + + /*************************************************************************** + * DYNAMIC FILE TRANSFER + ****************************************************************************/ + + if(DINAMICO.equals("dinamico")){ + Date date = Calendar.getInstance().getTime(); + DateFormat formatter = new SimpleDateFormat(FORMATO); + String today_formattato = formatter.format(date); + + System.out.println("Data costruita: "+today_formattato); + + for(int i=1;i<=numero_files;i++){ + String sorgente=properties.getProperty("ORIGINE_FILE_"+i); + String destinazione=properties.getProperty("DESTINAZIONE_FILE_"+i); + + //System.out.println("Sorgente: "+sorgente+" - Destinazione: "+destinazione+" - i: "+i); + sorgenti[i-1]=sorgente.replaceAll("%data%", today_formattato); + destinazioni[i-1]=destinazione.replaceAll("%data%", today_formattato); + } + } + + /*************************************************************************** + * ALL_FILES TYPE TRANSFER + ****************************************************************************/ + + else if(DINAMICO.equals("all_files")){ + + if(TIPO_TRAFERIMENTO.equals("download")){ + String f=LOCAL_FOLDER; + client.changeDirectory(REMOTE_FOLDER); + FTPFile[] ls_file=new FTPFile[1]; + try { + ls_file = client.list(); + } catch (FTPListParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + System.exit(1); + } + + sorgenti=new String[ls_file.length-2]; + destinazioni=new String[ls_file.length-2]; + + int count=0; + + for (int i=0;i