The application scenario is to transfer files on one server to another server through a program.
Manually type the linux command
scp: remote copy (local --> remote server)
scp file name username@IP address: absolute path
# -- coding: utf-8 --
import paramiko
def remote_scp(host_ip,remote_path,local_path,username,password):
t = paramiko.Transport((host_ip,22))
t.connect(username=username, password=password) # 登录远程服务器
sftp = paramiko.SFTPClient.from_transport(t)# sftp传输协议
src = remote_path
des = local_path
sftp.get(src,des)
t.close()
host_ip='xxx.xx.xx.xx' #不需要带端口
username='xxx'
password='xxx'
remote_path=r'/root/xxx/xx.html'#远程文件名的路径
local_path=r'./xx.html'#必须要带文件名,不带会出错。
remote_scp(host_ip,remote_path,local_path,username,password)