Ansible scp Python脚本
import os
import paramiko
def RemoteScp(host_ip, host_port, host_username, host_password, remote_path, local_path):
scp = paramiko.Transport((host_ip, host_port))
scp.connect(username=host_username, password=host_password)
sftp = paramiko.SFTPClient.from_transport(scp)
try:
remote_files = sftp.listdir(remote_path)
for file in remote_files:
local_file = local_path + file
remote_file = remote_path + file
sftp.get(remote_file, local_file)
except IOError:
return ("remote_path or local_path is not exist")
scp.close()
if __name__ == '__main__':
host_ip = '192.168.1.100' --源端IP
host_port = 22 --源端SSH端口
host_username = 'root' --源端用户名
host_password = '12345678' --源端密码
remote_path = '/orabak/baktmp/' --源端存放备份集路径
local_path = '/bak/scorabak/' --目的端路径
RemoteScp(host_ip, host_port, host_username, host_password, remote_path, local_path)