Python - Python 通過SFTP 進行文件的批量上傳
1、引用之前的 auto_template,並增加上傳目錄的方法
import paramiko
import time
from dir import dir_utils
def read_unicode(unicode):
return str(unicode, 'utf-8').split('\n')
def command(ssh_config, cmd, result_print=None):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ssh_config.hostname, port=ssh_config.port, username=ssh_config.username,
password=ssh_config.password)
print(ssh_config.hostname + '@' + ssh_config.username, ': ', cmd)
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read()
if result_print:
lines = read_unicode(result)
for line in lines:
print(line)
ssh.close()
def upload_dirs(ssh_config, local, server):
client, transport = sftp(ssh_config)
rel_dirs, rel_filenames = dir_utils.foreach(local)
path_cmd = dir_utils.linux_path_cmd(server)
command(ssh_config, path_cmd)
for path in rel_dirs:
server_dir = server + path.replace(local, '')
path_cmd = dir_utils.linux_path_cmd(server_dir)
command(ssh_config, path_cmd)
for file in rel_filenames:
server_path = server + file.replace(local, '')
print('FILE UPLOAD ↑↑↑ LOCAL:', file, ' -> SERVER: ', server_path)
client.put(file, server_path)
transport.close()
def upload_new(ssh_config, local, server):
client, transport = sftp(ssh_config)
client.put(local, server)
transport.close()
def sftp(ssh_config):
transport = paramiko.Transport(ssh_config.hostname, ssh_config.port)
transport.connect(username=ssh_config.username, password=ssh_config.password)
client = paramiko.SFTPClient.from_transport(transport)
return client, transport
def auto_deploy(deploy_config, ssh_config):
print('Step 1 : stop application')
command(ssh_config, deploy_config.stop_cmd)
print('wait for application stop')
time.sleep(2)
print('Step 2 : delete old application file')
command(ssh_config, deploy_config.delete_cmd)
print('Step 3 : upload new application file to server')
print('wait for application upload')
upload_new(ssh_config, deploy_config.local, deploy_config.server)
print('upload completed')
print('Step 4 : run application')
time.sleep(2)
command(ssh_config, deploy_config.start_cmd)
print('deploy application completed')
2、主要代碼
def upload_dirs(ssh_config, local, server):
client, transport = sftp(ssh_config)
rel_dirs, rel_filenames = dir_utils.foreach(local)
path_cmd = dir_utils.linux_path_cmd(server)
command(ssh_config, path_cmd)
for path in rel_dirs:
server_dir = server + path.replace(local, '')
path_cmd = dir_utils.linux_path_cmd(server_dir)
command(ssh_config, path_cmd)
for file in rel_filenames:
server_path = server + file.replace(local, '')
print('FILE UPLOAD ↑↑↑ LOCAL:', file, ' -> SERVER: ', server_path)
client.put(file, server_path)
transport.close()
3、運行代碼
from auto_deploy.nsbd import ssh_config
from auto_deploy import auto_template
if __name__ == '__main__':
server_path = '/tmp/lllllll/'
local_path = 'C:/Users/A-PC/Desktop/123/'
auto_template.command(ssh_config, 'rm -rf ' + server_path)
auto_template.upload_dirs(ssh_config, local_path, server_path)
4、運行結果
47.114.154.255@root : rm -rf /tmp/lllllll/
47.114.154.255@root : [ ! -d "/tmp/lllllll/" ] && mkdir /tmp/lllllll/
47.114.154.255@root : [ ! -d "/tmp/lllllll/4444777" ] && mkdir /tmp/lllllll/4444777
47.114.154.255@root : [ ! -d "/tmp/lllllll/5555" ] && mkdir /tmp/lllllll/5555
47.114.154.255@root : [ ! -d "/tmp/lllllll/66666" ] && mkdir /tmp/lllllll/66666
47.114.154.255@root : [ ! -d "/tmp/lllllll/4444777/212554" ] && mkdir /tmp/lllllll/4444777/212554
47.114.154.255@root : [ ! -d "/tmp/lllllll/4444777/2222" ] && mkdir /tmp/lllllll/4444777/2222
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/123.txt -> SERVER: /tmp/lllllll/123.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/1234567.txt -> SERVER: /tmp/lllllll/1234567.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/4444777/324324324.txt -> SERVER: /tmp/lllllll/4444777/324324324.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/4444777/gdfsgdgf.txt -> SERVER: /tmp/lllllll/4444777/gdfsgdgf.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/4444777/212554/1551.txt -> SERVER: /tmp/lllllll/4444777/212554/1551.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/4444777/2222/545.txt -> SERVER: /tmp/lllllll/4444777/2222/545.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/5555/4324.txt -> SERVER: /tmp/lllllll/5555/4324.txt
FILE UPLOAD ↑↑↑ LOCAL: C:/Users/A-PC/Desktop/123/66666/54545.txt -> SERVER: /tmp/lllllll/66666/54545.txt