#!/usr/bin/python
# -*- coding: UTF-8 -*-
import paramiko
from urllib import quote
#Environment_XXX為上傳FTP的服務(wù)器路徑
Environment_root = '/home/appbao/app/shoushua_biz/iOS/'
Environment_text = '/home/appbao/app/shoushua_biz/iOS/test'
Environment_RC = '/home/appbao/app/shoushua_biz/iOS/rc'
Environment_alpha = '/home/appbao/app/shoushua_biz/iOS/alpha'
Environment_release = '/home/appbao/app/shoushua_biz/iOS/release'
#remote_XXX為服務(wù)器ipa下載路徑
remote_head_text = 'http://172.16.132.241/app/shoushua_biz/iOS/test'
remote_head_RC = 'http://172.16.132.241/app/shoushua_biz/iOS/rc'
remote_head_alpha = 'http://172.16.132.241/app/shoushua_biz/iOS/alpha'
remote_head_release = 'http://172.16.132.241/app/shoushua_biz/iOS/release'
Environment = [Environment_text,Environment_RC,Environment_alpha,Environment_release]
remote_heads = [remote_head_text,remote_head_RC,remote_head_alpha,remote_head_release]
class SFTPClient:
#init
def __init__(self,host = None,userName = None,pwd = None):
print '初始化完成'
self.host = host
self.userName = userName
self.pwd = pwd
print self.host + self.userName + self.pwd
pass
# creat SFTP
def connectSFTP(self):
# 創(chuàng)建SSH對(duì)象
self.sftp = paramiko.SSHClient()
self.sftp.set_missing_host_key_policy (paramiko.AutoAddPolicy ())
self.sftp.connect(self.host,22, self.userName, self.pwd ,timeout=20)
print 'SFTP連接完成'
def execute(self, *cmd):
self.files = []
stdin, stdout, stderr = self.sftp.exec_command (*cmd)
for x in stdout.readlines():
print x
self.files.append(x)
#upload
def uploadData(self,localpath = None,remotepath = None):
try:
trans = paramiko.Transport ((self.host, 22))
# 建立連接
trans.connect (username=self.userName, password=self.pwd)
# 實(shí)例化一個(gè) sftp對(duì)象,指定連接的通道
sftp = paramiko.SFTPClient.from_transport (trans)
# 發(fā)送文件
sftp.put (localpath, remotepath)
# 下載文件
trans.close ()
except Exception,e:
print str(e) + 'SFTP上傳失敗'
finally:
print 'SFTP上傳結(jié)束'
#close
def close(self):
self.sftp.close()
if __name__ == '__main__':
ftp = None
try:
ftp = SFTPClient (host='FTP地址', userName='用戶名', pwd='密碼')
ftp.connectSFTP ()
except Exception,e:
print '連接失敗'+str(e)
exit(1)
#獲取遠(yuǎn)端服務(wù)器下對(duì)應(yīng)的文件佳
ftp.execute('cd /home/appbao/app/shoushua_biz/iOS/ && ls')
remote = raw_input('選擇一個(gè)文件夾或者重新創(chuàng)建一個(gè)文件夾\r\n')
print '您選擇了%s文件'%remote
hasFiles = False
for sub in ftp.files:
if(sub.encode ('gbk') == remote):
hasFiles = True
if hasFiles == False:
ftp.execute ('mkdir /home/appbao/app/shoushua_biz/iOS/%s' % remote)
#獲取文件名稱
filePath = raw_input('請(qǐng)輸入本地文件\r\n')
filePath = filePath.strip()
filePath = quote(filePath)
fileName = filePath.split('/')[-1]
#上傳
ftp.uploadData(localpath=filePath,
remotepath='%s%s/%s'%(Environment_root,remote,fileName))
ftp.close()
finalPath = 'http://172.16.132.241/app/shoushua_biz/iOS/%s/%s'%(remote,fileName)
print '上傳的遠(yuǎn)端地址\r\n%s'%finalPath
將此python執(zhí)行加入到自定義命令
自定義終端執(zhí)行>http://www.reibang.com/p/5fcd173f6103
自定義命令代碼舉例
postFTP(){
python /XXX/XXX/XXX.py #此處文件路徑為你的python代碼路徑
}