SFTP & FTP Upload
2023-09-14 08:58:00 时间
1. Install FTP service on Linux(Red Hat) as root user
[root]# yum install ftp
2. Configure FTP as root user
local_enable=YES
# Uncomment this to enable any form of FTP write command.
write_enable=YES
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpds)
local_umask=022
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
userlist_enable=YES
userlist_deny=NO
#Passive mode not allowed
pasv_enable=NO
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#Specify listen port
listen_port=21
#Allow active mode
port_enable=YES
b) Make sure ftp user exists in the file /etc/vsftpd/user_list as root user
3. Restart FTP service as root user
[root]# service vsftpd restart
SFTP:
Generally SFTP service is installed/configured on Linux OS
Please refer to below info:
如果ftp服务器没有开通20,但是FTP服务器开通了高位随机端口,则必须使用被动模块,同时也可以使用主动模式。
如果客服端的防火墙关闭了端口的主动接收功能,则无法使用主动模式,但可以使用被动模块,这也是被动模块存在的原因。
一般公司内部为了服务器安全点,使用主动模式,但对客户端有些要求,可以接受端口的请求数据。
PORT(主动)方式的连接过程是:
客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。
当需要传送数据时,客户端在命令链路上用PORT命令告诉服务器:“我打开了****端口,你过来连接我”。
于是服务器从20端口向客户端的****端口发送连接请求,建立一条数据链路来传送数据。
PASV(被动)方式的连接过程是:
客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。
当需要传送数据时,服务器在命令链路上用PASV命令告诉客户端:“我打开了****端口,你过来连接我”。
于是客户端向服务器的****端口发送连接请求,建立一条数据链路来传送数据。
从上面可以看出,两种方式的命令链路连接方法是一样的,而数据链路的建立方法就完全不同。
【SFTP Upload】
shell使用lftp连接ftp和sftp,并可以指定私钥 lftp连接ftp在脚本中可以 lftp -c open username:password@host:port; ls /Friso/20180822/click/mobile/SUCCESS | wc -l lftp usename:password@host:port -e ls /Fr...
sftp,Secure File Transfer Protocol,默认端口22。 可以为传输文件提供一种安全的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。 3.客户端软件 FileZilla。一款GUI软件,可方便的代替linux下的 sz、rz命令实现文件的上传下
[root]# yum install ftp
2. Configure FTP as root user
a) Be clear with below properties, and configure them in the file /etc/vsftpd/vsftpd.conf as root user
# Uncomment this to allow local users to log in.local_enable=YES
# Uncomment this to enable any form of FTP write command.
write_enable=YES
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpds)
local_umask=022
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=YES
userlist_enable=YES
userlist_deny=NO
#Passive mode not allowed
pasv_enable=NO
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#Specify listen port
listen_port=21
#Allow active mode
port_enable=YES
b) Make sure ftp user exists in the file /etc/vsftpd/user_list as root user
3. Restart FTP service as root user
[root]# service vsftpd restart
SFTP:
Generally SFTP service is installed/configured on Linux OS
Please refer to below info:
如果ftp服务器没有开通20,但是FTP服务器开通了高位随机端口,则必须使用被动模块,同时也可以使用主动模式。
如果客服端的防火墙关闭了端口的主动接收功能,则无法使用主动模式,但可以使用被动模块,这也是被动模块存在的原因。
一般公司内部为了服务器安全点,使用主动模式,但对客户端有些要求,可以接受端口的请求数据。
PORT(主动)方式的连接过程是:
客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。
当需要传送数据时,客户端在命令链路上用PORT命令告诉服务器:“我打开了****端口,你过来连接我”。
于是服务器从20端口向客户端的****端口发送连接请求,建立一条数据链路来传送数据。
PASV(被动)方式的连接过程是:
客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。
当需要传送数据时,服务器在命令链路上用PASV命令告诉客户端:“我打开了****端口,你过来连接我”。
于是客户端向服务器的****端口发送连接请求,建立一条数据链路来传送数据。
从上面可以看出,两种方式的命令链路连接方法是一样的,而数据链路的建立方法就完全不同。
【FTP Upload】
package shuai.study.ftp; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTPClient; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; * @Description: FTP Upload * @author Zhou Shengshuai * @date 2014年8月5日 下午2:37:57 public class FtpUpload { private static final Logger logger = LogManager.getLogger(FtpUpload.class); static { System.setProperty("FTP_LOG", "/home/tmp/log"); DOMConfigurator.configure("/home/tmp/configuration/log4j.xml"); private static String hostname = "192.168.0.1"; private static int ftpPort = 21; private static String ftpUsername = "ftpuser"; private static String ftpPassword = "ftpPassword"; private static String remoteDirectoryString = "/upload"; private static File localDirectory = new File("/home/tmp/local"); public static Set File upload(Collection File localFileCollection) { Set File localFileSet = new HashSet File FTPClient ftpClient = connect(hostname, ftpPort, ftpUsername, ftpPassword, remoteDirectoryString); if (ftpClient != null ftpClient.isConnected()) { Iterator File localFileIterator = localFileCollection.iterator(); while (localFileIterator.hasNext()) { File localFile = localFileIterator.next(); if (upload(ftpClient, localFile)) { // Add localFile into localFileSet after upload successfully, and will delete these local files localFileSet.add(localFile); disconnect(ftpClient); return localFileSet; private static boolean upload(FTPClient ftpClient, File localFile) { boolean storeFileFlag = false; if (localFile != null localFile.isFile()) { InputStream fileInputStream = null; try { fileInputStream = new FileInputStream(localFile); storeFileFlag = ftpClient.storeFile(localFile.getName(), fileInputStream); if (storeFileFlag) { logger.info("Upload local file " + localFile + " to remote directory " + ftpUsername + "@" + hostname + ":" + remoteDirectoryString + " via FTP"); } else { logger.error("Fail to upload local file " + localFile + " to remote directory " + ftpUsername + "@" + hostname + ":" + remoteDirectoryString + " via FTP"); } catch (IOException ioe) { logger.error("FTP Upload Exception", ioe); } finally { IOUtils.closeQuietly(fileInputStream); return storeFileFlag; private static FTPClient connect(String hostname, int ftpPort, String ftpUsername, String ftpPassword, String remoteDirectoryString) { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(hostname, ftpPort); logger.info("FTP Connected " + hostname + ":" + ftpPort); ftpClient.login(ftpUsername, ftpPassword); logger.info("FTP Logined as " + ftpUsername); // ftpClient.enterLocalPassiveMode(); // if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { // logger.error("FTP Response Unsuccessfully"); // disconnect(ftpClient); // } if (ftpClient.makeDirectory(remoteDirectoryString)) { logger.info("FTP Directory Made " + remoteDirectoryString); ftpClient.changeWorkingDirectory(remoteDirectoryString); logger.info("FTP Working Directory Changed as " + remoteDirectoryString); // ftpClient.setBufferSize(1024); // ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.setFileTransferMode(FTPClient.BINARY_FILE_TYPE); } catch (IOException ioe) { logger.error("FTP Connection Exception", ioe); return ftpClient; private static void disconnect(FTPClient ftpClient) { if (ftpClient != null) { try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException ioe) { logger.error("FTP Disconnection Exception", ioe); logger.info("FTP Disconnected"); private static void delete(Set File exportCucFileSet) { Iterator File exportCucFileIterator = exportCucFileSet.iterator(); while (exportCucFileIterator.hasNext()) { File exportCucFile = exportCucFileIterator.next(); if (FileUtils.deleteQuietly(exportCucFile)) { logger.info("Delete local file " + exportCucFile + " after FTP upload"); } else { logger.error("Fail to delete local file " + exportCucFile + " after FTP upload"); public static void main(String[] args) { Collection File localFileCollection = FileUtils.listFiles(localDirectory, new String[] { "gz", "GZ", "xml", "XML", "zip", "ZIP" }, true); Set File localFileSet = FtpUpload.upload(localFileCollection); delete(localFileSet);
【SFTP Upload】
package shuai.study.sftp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Collection; import java.util.Iterator; import java.util.Properties; import org.apache.commons.io.FileUtils; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; * @Description: SFTP Upload * @author Zhou Shengshuai * @date 2014年8月1日 下午2:37:57 public class SftpUpload { private static final Logger logger = LogManager.getLogger(SftpUpload.class); static { System.setProperty("FTP_LOG", "/home/tmp/log"); DOMConfigurator.configure("/home/tmp/configuration/log4j.xml"); private static String hostname = "192.168.0.1"; private static int sftpPort = 22; private static String sftpUsername = "ftpuser"; private static String sftpPassword = "ftpPassword"; private static String remoteDirectoryString = "/upload"; private static String localDirectoryString = "/home/tmp/local"; * @Description: Upload public static void upload() { File localDirectory = new File(localDirectoryString); if (localDirectory.exists()) { ChannelSftp sftp = connect(hostname, sftpPort, sftpUsername, sftpPassword); Collection File localFileCollection = FileUtils.listFiles(localDirectory, new String[] { "gz", "GZ", "xml", "XML", "zip", "ZIP" }, true); Iterator File localFileIterator = localFileCollection.iterator(); while (localFileIterator.hasNext()) { File localFile = localFileIterator.next(); logger.info("Local File: " + localFile); if (sftp != null sftp.isConnected()) { upload(remoteDirectoryString, localFile, sftp); disconnect(sftp); } else { logger.warn("The Local Directory " + localDirectoryString + " doesnt exist"); * @Description: SFTP Upload * @param remoteDirectoryString * Remote CM Directory * @param localFile * Local CM File * @param sftp * ChannelSftp public static void upload(String remoteDirectoryString, File localFile, ChannelSftp sftp) { logger.info("Remote Directory: " + remoteDirectoryString); try { sftp.cd(remoteDirectoryString); sftp.put(new FileInputStream(localFile), localFile.getName()); FileUtils.deleteQuietly(localFile); logger.info("Upload Local File " + localFile + " via SFTP"); } catch (FileNotFoundException fnfe) { logger.error("File Not Found Exception", fnfe); } catch (SftpException se) { logger.error("SFTP Upload Exception", se); * @Description: SFTP Connection * @param hostname * SFTP HOST * @param sftpPort * SFTP PORT * @param sftpUsername * SFTP USERNAME * @param sftpPassword * SFTP PASSWORD * @return ChannelSftp public static ChannelSftp connect(String hostname, int sftpPort, String sftpUsername, String sftpPassword) { JSch jsch = new JSch(); Properties sshConfig = new Properties(); Channel channel = null; try { Session session = jsch.getSession(sftpUsername, hostname, sftpPort); logger.info("Session Created"); session.setPassword(sftpPassword); sshConfig.put("StrictHostKeyChecking", "no"); session.setConfig(sshConfig); // session.setTimeout(60000); // session.setServerAliveInterval(90000); session.connect(); logger.info("Session Connected"); channel = session.openChannel("sftp"); logger.info("Channel Opened"); channel.connect(); logger.info("Channel Connected"); } catch (JSchException je) { logger.error("SFTP Exception", je); return (ChannelSftp) channel; * @Description: Disconnect SFTP * @param sftp * ChannelSftp public static void disconnect(ChannelSftp sftp) { if (sftp != null) { try { sftp.getSession().disconnect(); } catch (JSchException je) { logger.error("SFTP Disconnect Exception", je); sftp.disconnect(); * @Description: Main Thread * @param args public static void main(String[] args) { SftpUpload.upload();
【Shell FTP Upload】
#!/bin/bash HOST="192.168.0.1" USER="ftpuser" PASS="ftpPassword" LOCAL_DIR="/home/tmp/local" REMOTE_DIR="/home/ ftp-user /remote" ftp -v -n EOF open ${HOST} user ${USER} ${PASS} prompt off passive off binary lcd ${LOCAL_DIR} cd ${REMOTE_DIR} mput * close
shell使用lftp连接ftp和sftp,并可以指定私钥 lftp连接ftp在脚本中可以 lftp -c open username:password@host:port; ls /Friso/20180822/click/mobile/SUCCESS | wc -l lftp usename:password@host:port -e ls /Fr...
sftp,Secure File Transfer Protocol,默认端口22。 可以为传输文件提供一种安全的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。 3.客户端软件 FileZilla。一款GUI软件,可方便的代替linux下的 sz、rz命令实现文件的上传下
相关文章
- WMO&ECMWF机器学习次季节预报比赛结束,全部代码已开源!
- 【Django | 开发】面试招聘信息网站(处理产品细节和权限&美化页面样式)
- 参会指南丨第二届SmartNIC & DPU技术创新峰会
- 史上最全的Java&Android面试题搜集整理
- DPU :AMD & VMware,风乍起,吹皱一池春水
- 每日tips:Dart & Flutter Easy Wins 36-42
- 使用()命令来启用FTP服务_windows播放ftp
- 零零信安-D&D数据泄露报警日报【第18期】
- 二叉排序树的平均查找长度(成功&&不成功)
- ECCV2022 &CVPR2022论文速递2022.7.21!& demo
- Simulation of SAW & BAW Resonators for RF Filters
- react源码解析15.scheduler&Lane
- hctf2016 简单部分WEB && misc writeup
- PicoCTF-web类做题笔记-IK&N Hong_zhong
- 微信公众平台开发(四)——微信网页授权:获取用户openid&用户基本信息
- linux登录FTP:简单易行的连接过程(linux登录ftp)
- Linux访问FTP服务器的技巧(Linux如何ftp访问)
- 安装Linux下FTP工具,打造文件分享殿堂(linux安装ftp工具)
- Linux下修改FTP服务器权限的方法(linux修改ftp权限)
- Linux下快速安装FTP服务(linux怎么安装ftp)
- Linux实现FTP自动上传功能(linux自动上传ftp)
- Linux下FTP服务的搭建及使用(linux上的ftp)
- Linux中建立FTP服务器的指南(linux创建ftp服务器)
- 服务Linux搭建FTP服务器的简单指南(linux是否ftp)
- 如何增加Oracle数据库的AMP值(amp值oracle)
- 聊天室php&mysql(六)