shell 和python 实现ftp文件上传或者下载

一、shell脚本

#####从ftp服务器上的/home/data 到 本地的/home/databackup####
#!/bin/bash
ftp -n<<!
open 172.168.1.171
user guest 123456
binary
cd /home/data
lcd /home/databackup
prompt
mget *
close
bye
!
2. ftp自动登录上传文件。

####本地的/home/databackup to ftp服务器上的/home/data####
#!/bin/bash
ftp -n<<!
open 192.168.1.171
user guest 123456
binary
hash
cd /home/data
lcd /home/databackup
prompt
mput *
close
bye
!

3. ftp自动登录下载单个文件。
####ftp服务器上下载/home/data/a.sh to local /home/databackup####
#!/bin/bash
ftp -n<<!
open 172.168.1.171
user guest 123456
binary
cd /home/data
lcd /home/databackup
prompt
get a.sh a.sh 
close
bye
!

4. ftp自动登录上传单个文件。
####把本地/home/databachup/a.sh up ftp /home/databackup 下####
#!/bin/bash
ftp -n<<!
open 192.168.1.171
user guest 123456
binary
cd /home/data
lcd /home/databackup
prompt
put a.sh a.sh 
close
bye
!

 

二、python脚本批量下载

#code:utf-8
from ftplib import FTP
import  os

ftp_list={
   'hostname':'171.20.3.333',
   'username':'test',
   'password':'123456',
   'port':'21'
          }
def  ftp_login(**ftp_list):
   '''
   :param ftp_list:
   :return: 登录ftp 返回一个ftp实例
   '''
   try:
      ftp=FTP()
     ftp.encoding = "utf-8"  ###################################留意ftp路径如果有中文,必须设置这个。

ftp.connect(ftp_list['hostname'],ftp_list['port'])
      ftp.login(ftp_list['username'],ftp_list['port'])
      print("登录成功")
   except:
      print("登录ftp失败")
   else:
      return ftp

def ftp_download(self,remote_path,local_path):
   '''
   下载远程机器上的tar.gz压缩包
   :param self:
   :param remote_path:
   :param local_path:
   :return:
   '''
   ftp=ftp_login(**ftp_list)
   try:
      res=ftp.nlst(remote_path)
      for  file  in  res :
         file_name=file.split('/')[-1]
         bufsize = 1024
         local_file = open(local_path+'/'+file_name,"wb")  #二进制方式打开一个文件
         ftp.retrbinary('RETR %s'%(file_name),local_file.write,bufsize)
         ftp.set_debuglevel(0)
         local_file.close()    #关闭本地文件
   except:
      print("连接异常")


def ftp_upload(self,remote_path,local_path):
   '''
   批量上传本地文件
   :param self:
   :param remote_path:
   :param local_path:
   :return:
   '''
   ftp = ftp_login(**ftp_list)
   try:
      ftp.mkd(remote_path)
      res=os.walk(local_path,topdown=True)
      for main_dir_list,insub_dir_list,file_list  in   res:
         for filename in  file_list:
            local_file = os.path.join(main_dir_list,filename)
            remote_file=remote_path+filename
            bufsize = 1024
            fp = open(local_file, 'rb')
            ftp.storbinary('STOR ' + remote_file, ftp, bufsize)
            ftp.close()
   except:
      print(0)

 

三实际应用场景:发版本自动从ftp服务器拉取代码

#code:utf-8
from ftplib import FTP
import  os
import sys
full_vserion='4.0.0.149'    #版本号 #sys.argv[1]  #
root_path='*************'  #具体路径
full_vserion_sub_path=full_vserion.split('.')[0]+'.'+full_vserion.split('.')[1]+'.'+full_vserion.split('.')[2]
sub_file=full_vserion.split('.')[-1]


ftp_list={
   'hostname':'172.30.3.222',
   'username':'test',
   'password':'123456',
   'port':21
          }

def  ftp_login(**ftp_list):
   '''
   :param ftp_list:
   :return: 登录ftp 返回一个ftp实例
   '''
   try:
      ftp = FTP()
      ftp.connect(ftp_list['hostname'], ftp_list['port'])
      ftp.login(ftp_list['username'], ftp_list['password'])
   except:
      print("登录ftp失败")
   else:
    return ftp

def ftp_download(remote_path,local_path):
   '''
   下载远程机器上的tar.gz压缩包
   :param self:
   :param remote_path:
   :param local_path:
   :return:
   '''
   ftp=ftp_login(**ftp_list)
   ftp.encoding = "utf-8"  ##############请留意
   file_list = ftp.nlst(remote_path)
   try:
      for file_name in file_list:
         if not file_name == "yylending-cms-%s.tar.gz" % (full_vserion):
            print("版本号有误,请核实")
            exit(0)
         bufsize = 1024
         local_file = open(local_path + '/' + file_name, "wb")  # 二进制方式打开一个文件
         print(remote_path + '/' + file_name)

         ftp.retrbinary('RETR %s' % (remote_path + '/' + file_name), local_file.write, bufsize)
         ftp.set_debuglevel(0)
         local_file.close()  # 关闭本地文件
   except:
      print("连接异常")



print(root_path)
print(full_vserion_sub_path)
print(full_vserion)
remote_path=root_path+'/'+full_vserion_sub_path+'/'+full_vserion
ftp_download(remote_path,'C:')

 

posted @ 2019-07-23 17:14  马里亚纳仰望星空  Views(554)  Comments(0Edit  收藏  举报