買了個Linux服務器,Centos系統,裝了個寶塔搭建了10個網站,比如有時候要在某個文件上加點代碼,就要依次去10個文件改動,雖然寶塔是可視化頁面操作,不需要用命令,但是也麻煩,雖然還有git的hook方法,但是操作也麻煩,新建個目錄的話還得操作一次,所以萌生了一個想法,用Python來批量更新服務器上的文件
注意:很多人學Python過程中會遇到各種煩惱問題,沒有人幫答疑容易放棄。為此小編建了個Python全棧免費答疑交流.裙 :七衣衣九起起巴而五(數字的諧音)轉換下可以找到了,不懂的問題有老司機解決里面還有最新Python教程項目可拿,,一起相互監督共同進步!
序言
在網上搜索了一圈,發現Python有個庫叫paramiko
可以專門拿來干這個事,具體資料和安裝就網上去搜索吧,我就直接上代碼了,不到100行,其實還可以精簡吧,后面再說了,先把功能實現了再說,Show Code
代碼
import paramiko import os # 連接信息 host = 'xxx.65.9.191' port = 22 username = 'root' password = 'root' # 忽略的目錄 skipArry = ['kai.xxxx.com','demo.xxxx.com'] fullpathArry = [] currentIndex = '' # 判斷文件是否存在 def judgeFileExist(): global currentIndex; currentIndex = os.getcwd() + '/Index.php' if os.path.isfile(currentIndex) == False: print('Index文件不存在') exit() print('文件檢測成功,准備連接服務器...') def creatConnect(): try: print('開始連接服務器...') s = paramiko.Transport((host, port)) s.connect(username=username, password=password) sftp = paramiko.SFTPClient.from_transport(s) print('連接:' + host + '成功') return sftp,s except Exception as e: print('連接服務器失敗:' + str(e)) # # 獲取目錄保存為數組 def getDirectory(sftp): print('開始獲取目錄...') sftp.chdir('/www/wwwroot') pathlist = sftp.listdir(path='.') for path in pathlist: fullpath = '/www/wwwroot/' + path + '/application/index/controller' if path in skipArry: continue fullpathArry.append(fullpath) print('目錄獲取完畢') # 上傳Index文件 def uploadIndex(sftp): for fullpathitem in fullpathArry: remoteIndex = fullpathitem + '/Index.php' print('開始上傳:' + remoteIndex) try: sftp.put(currentIndex, remoteIndex) try: sftp.file(remoteIndex) sftp.chmod(remoteIndex, int("775", 8)) print('修改' + remoteIndex + '權限為755') print(fullpathitem + '上傳成功') except: print(fullpathitem + '上傳失敗') continue except Exception as e: print('錯誤信息:' + str(e)) continue if __name__ == "__main__": judgeFileExist() sftp,s = creatConnect() getDirectory(sftp) uploadIndex(sftp) s.close()
代碼Show完了,開始詳細解釋一波
這個方法是檢測我當前目錄下有沒有Index.php
這個文件,如果沒有的話就直接退出不進行下一步了,這里有個小坑,就是你Index.php
這個文件名,你寫小寫的index.php
,也能為True
,這里有個要注意的地方,就是要修改currentIndex
的值,必須在前面加上global
,否則還是為空
def judgeFileExist(): global currentIndex; currentIndex = os.getcwd() + '/Index.php' if os.path.isfile(currentIndex) == False: print('Index文件不存在') exit() print('文件檢測成功,准備連接服務器...')
這是連接服務器並創建SFTP,使用了Try
來捕獲異常錯誤
def creatConnect(): try: print('開始連接服務器...') s = paramiko.Transport((host, port)) s.connect(username=username, password=password) sftp = paramiko.SFTPClient.from_transport(s) print('連接:' + host + '成功') return sftp,s except Exception as e: print('連接服務器失敗:' + str(e))
這里就是執行操作命令了,使用sftp對象來操作,sftp.chdir
是用於切換目錄,相當於shell
命令的cd /www/wwwroot
sftp.listdir(path='.')
是返回當前目錄下的文件夾,且是以數組形式返回,然后將其拼接成完整路徑后再保存在本地數組里備用,這里有個if in
是用來跳過一些網站目錄,比如我xxx.demo.com這個目錄不想更新,就在開頭的SkipArry里寫上,用來跳過
def getDirectory(sftp): print('開始獲取目錄...') sftp.chdir('/www/wwwroot') pathlist = sftp.listdir(path='.') for path in pathlist: fullpath = '/www/wwwroot/' + path + '/application/index/controller' if path in skipArry: continue fullpathArry.append(fullpath) print('目錄獲取完畢')
這里就是關鍵的上傳部分了,首先遍歷出我們需要修改的文件夾目錄,后面拼接上需要修改的文件Index.php
形成遠程服務器的文件路徑,然后使用sftp.put
函數來上傳我們的文件,第一個參數是本地文件的路徑,第二個參數是遠程服務器上的路徑,上傳成功后使用sftp.file
來驗證該文件是否存在,其實這里我是做了個備份處理的(有點bug就暫時先注釋掉了),先將原本的Index.php
改名為BackIndex.php
在上傳新的Index.php
,這個判斷函數才有用,不然我這樣寫沒啥用,因為上沒上傳成功肯定都會存在一個Index.php
文件.上傳好了之后使用sftp.chmod
方法來改變該文件的權限為755,這里有個坑,你直接在第二個參數寫755,會發現生成的文件權限為363,經過多次試驗發現,第二個參數要傳入8進制的755,也就是493,生成的權限就是755了,感覺有點坑爹。
def uploadIndex(sftp): for fullpathitem in fullpathArry: remoteIndex = fullpathitem + '/Index.php' print('開始上傳:' + remoteIndex) try: sftp.put(currentIndex, remoteIndex) try: sftp.file(remoteIndex) sftp.chmod(remoteIndex, int("775", 8)) print('修改' + remoteIndex + '權限為755') print(fullpathitem + '上傳成功') except: print(fullpathitem + '上傳失敗') continue except Exception as e: print('錯誤信息:' + str(e)) continue
然后在main里依次執行,就能將服務器上對應的目錄下的文件全部替換成我本地的文件了,代碼不多,但效果好使
注意:很多人學Python過程中會遇到各種煩惱問題,沒有人幫答疑容易放棄。為此小編建了個Python全棧免費答疑交流.裙 :七衣衣九起起巴而五(數字的諧音)轉換下可以找到了,不懂的問題有老司機解決里面還有最新Python教程項目可拿,,一起相互監督共同進步!
本文的文字及圖片來源於網絡加上自己的想法,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯系我們以作處理。