python paramiko利用sftp上傳目錄到遠(yuǎn)程的實例
網(wǎng)上大部分都是上傳文件,于是個人參照網(wǎng)上一些博客的內(nèi)容,寫了一個把windows上目錄上傳到遠(yuǎn)程linux的一個小程序。
下面是代碼:
class ExportPrepare(object):
def __init__(self):
pass
def sftp_con(self):
t = paramiko.Transport((self.ip, self.port))
t.connect(username=self.username, password=self.password)
return t
# 找到所有你要上傳的目錄已經(jīng)文件。
def __get_all_files_in_local_dir(self, local_dir):
all_files = list()
if os.path.exists(local_dir):
files = os.listdir(local_dir)
for x in files:
filename = os.path.join(local_dir, x)
print "filename:" + filename
# isdir
if os.path.isdir(filename):
all_files.extend(self.__get_all_files_in_local_dir(filename))
else:
all_files.append(filename)
else:
print '{}does not exist'.format(local_dir)
return all_files
# Copy a local file (localpath) to the SFTP server as remotepath
def sftp_put_dir(self):
try:
#本地test目錄上傳到遠(yuǎn)程root/usr/下面
local_dir = "c:/test"
remote_dir = "/root/usr/test"
t = self.sftp_con()
sftp = paramiko.SFTPClient.from_transport(t)
# sshclient
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.ip, port=self.port, username=self.username, password=self.password, compress=True)
ssh.exec_command('rm -rf ' + remote_dir)
if remote_dir[-1] == '/':
remote_dir = remote_dir[0:-1]
all_files = self.__get_all_files_in_local_dir(local_dir)
for x in all_files:
filename = os.path.split(x)[-1]
remote_file = os.path.split(x)[0].replace(local_dir, remote_dir)
path = remote_file.replace('\\', '/')
# 創(chuàng)建目錄 sftp的mkdir也可以,但是不能創(chuàng)建多級目錄所以改用ssh創(chuàng)建。
tdin, stdout, stderr = ssh.exec_command('mkdir -p ' + path)
print stderr.read()
remote_filename = path + '/' + filename
print u'Put files...' + filename
sftp.put(x, remote_filename)
ssh.close()
except Exception, e:
print e
if __name__=='__main__':
export_prepare = ExportPrepare()
export_prepare.sftp_put_dir()
比較匆忙,不足之處可以指出,共同進(jìn)步。
以上這篇python paramiko利用sftp上傳目錄到遠(yuǎn)程的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python實現(xiàn)Word轉(zhuǎn)HTML
將Word轉(zhuǎn)換為HTML能將文檔內(nèi)容發(fā)布在網(wǎng)頁上,這樣,用戶就可以通過瀏覽器直接查看或閱讀文檔而無需安裝特定的軟件,下面我們就來學(xué)習(xí)一下Python是如何實現(xiàn)Word轉(zhuǎn)HTML的吧2023-12-12
Python3將數(shù)據(jù)保存為txt文件的方法
這篇文章主要介紹了Python3將數(shù)據(jù)保存為txt文件的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
解決Tensorflow安裝成功,但在導(dǎo)入時報錯的問題
今天小編就為大家分享一篇解決Tensorflow安裝成功,但在導(dǎo)入時報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
python?Ajenti控制面板輕松地管理所有服務(wù)器網(wǎng)站
Ajenti是一個值得擁有的管理面板,免費開源的管理面板工具,可以幫助你集中管理多個服務(wù)器和網(wǎng)站,Ajenti?支持?Linux、BSD、Mac?OS?X和Windows?等多個操作系統(tǒng),并且可以通過一個直觀的?Web?界面來完成各種系統(tǒng)管理任務(wù)2024-01-01
Python實現(xiàn)批量將word轉(zhuǎn)換成pdf
這篇文章主要為大家詳細(xì)介紹了如何利用Python實現(xiàn)批量將word文檔轉(zhuǎn)換成pdf文件,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08
python通用數(shù)據(jù)庫操作工具 pydbclib的使用簡介
這篇文章主要介紹了python通用數(shù)據(jù)庫操作工具 pydbclib的使用簡介,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
pandas pd.cut()與pd.qcut()的具體實現(xiàn)
本文主要介紹了pandas pd.cut()與pd.qcut()的具體實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

