Use shell and Python to automate FTP login and upload and download

Source: Internet
Author: User
Tags ftp login

1.FTP Automatic Login Download file

-N is not affected by the. netrc file. (FTP defaults to read settings in the. netrc file)! is a sign of an instant file it must appear in pairs to identify the start and end of instant files binary-set file Transfer type binary (binary transfer) prompt-switch interaction hint (default is on) close-end F TP session and return to command line quit-end FTP session and exit FTP (feature pass bye) mget-copy one or more remote files to local get-copy a single remote file to local put-copy a local file to remote mput-copy one or more local files to remote ha Sh-toggles hash-sign (#) printing for each data block transferred (default = OFF) toggles print "#" for every buffer transfer (hash mark Print)
# # # # # # #从ftp服务器上的/home/data to the local/home/databackup#####!/bin/bashftp-n<<!open 192.168.1.171user guest 123456binarycd/home/datalcd/home/databackuppromptmget *closebye!

2.FTP Automatic Login Upload file

# # # #本地的/home/data#####!/bin/bashftp-n<<!open 192.168.1.171user guest on/home/databackup to FTP server 123456binaryhashcd/home/datalcd/home/databackuppromptmput *closebye!

3.ftp automatically log in and download individual files

# # # #ftp服务器上下载/home/data/a.sh to Local/home/databackup#####!/bin/bashftp-n<<!open 192.168.1.171user Guest 123456binarycd/home/datalcd/home/databackuppromptget a.sh a.sh closebye!

4.ftp automatically log in and upload individual files

# # # #把本地/home/databachup/a.sh up ftp/home/databackup #####!/bin/bashftp-n<<!open 192.168.1.171user Guest 123456binarycd/home/datalcd/home/databackuppromptput a.sh a.sh closebye!



5.python Login ftp and download

FTP Login Connection from ftplib import ftp              #加载ftp模块ftp =ftp ()                            #设置变量ftp. Set_debuglevel (2)                #打开调试级别2, Show Details ftp.connect ("IP", "Port")             #连接的ftp  sever and Port ftp.login ("User", "password")         #连接的用户名, Password print ftp.getwelcome ()               #打印出欢迎信息ftp. cmd ("xxx/xxx")                   #进入远程目录bufsize =1024                        #设置的缓冲区大小filename = " Filename.txt "            #需要下载的文件file_handle =open (filename, "WB"). write # Open files locally in write mode Ftp.retrbinaly ("Retr filename.txt", file_handle,bufsize)   #接收服务器上文件并写入本地文件ftp. set_ DebugLevel (0)               #关闭调试模式ftp. Quit ()                           #退出ftp  ftp Related command operations FTP.CWD (pathname)                    #设置FTP当前操作的路径ftp. Dir ()                             #显示目录下所有目录信息ftp. Nlst ()                           #获取目录下的文件ftp. MKD (Pathname)                   #新建远程目录ftp. PWD ()                             #返回当前所在位置ftp. RMD (dirname)                     #删除远程目录ftp. Delete (filename)                 #删除远程文件ftp. Rename (fromname, toname) #将fromname修改名称为toname. Ftp.storbinaly ("Stor filename.txt", file_handel,bufsize)    #上传目标文件ftp. Retrbinary ("retr  Filename.txt ", file_handel,bufsize)    #下载FTP文件


Log in FTP to view files

#!/usr/bin/env python#_*_ Coding:utf8 _*_import ftplib#from ftplib Import FTP ftp = ftplib. FTP ("123.56.1.1") ftp.login ("User", "passwd") data = []FTP.CWD ("Hxy/tmp") Ftp.dir (Data.append) ftp.quit () for line in Data:print (line)

Upload Download file

#coding:  utf-8from ftplib import ftpimport timeimport tarfile#!/usr/bin/python#- *- coding: utf-8 -*-from ftplib import ftpdef ftpconnect (host,  Username, password):     ftp = ftp ()      #ftp. set_ DebugLevel (2)           #打开调试级别2, show details      Ftp.connect (host, 21)            #连接      ftp.login (Username, password)    #登录, if anonymous login is replaced by an empty string     return ftp     def downloadfile (Ftp, remotepath, localpath):     bufsize = 1024                  #设置缓冲块大小     fp = open (localpath, ' WB ')      # Open a file locally in write mode     fTp.retrbinary (' retr  '  + remotepath, fp.write, bufsize)   #接收服务器上文件并写入本地文件      ftp.set_debuglevel (0)           #关闭调试      fp.close ()                       #关闭文件def  uploadfile (ftp, remotepath, localpath):     bufsize = 1024    fp = open (localpath,  ' RB ')     ftp.storbinary (' stor  ' + remotepath , fp, bufsize)   #上传文件     ftp.set_debuglevel (0)     fp.close ()                                       if __name__ ==  "_ _main__ ": &NBSP;&NBSP;&NBSP;&NBsp;ftp = ftpconnect ("******",  "* * * *",  "* * *")     downloadfile (FTP,   "* * *",  "* * *")     uploadfile (ftp,  "* * *",  "* * *")      ftp.quit ()

This article refers to:

Http://www.cnblogs.com/kaituorensheng/p/4480512.html

Http://www.jb51.net/article/34361.htm

http://blog.csdn.net/indexman/article/details/46387561


This article is from the "Forand" blog, make sure to keep this source http://853056088.blog.51cto.com/12966870/1960242

Use shell and Python to automate FTP login and upload and download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.