【python】之SCPClient库,远程文件copy!_python scp_==PP的博客-CSDN博客
-
- import paramiko # 用于调用scp命令
- from scp import SCPClient
-
- ###实例化SSH
- ssh=paramiko.SSHClient()
- ssh.load_system_host_keys()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
-
- ###hostname为主机IP,port为端口号,password为密码
- ssh.connect(hostname='192.1.1.1', port=22, username='root', password='root', compress=True)###建链
-
- ###scp建立连接
- scpclient = SCPClient(ssh.get_transport(),socket_timeout=15.0)
-
- local_path = 'boot.bin' ###local_path为本地文件路径
-
- remote_path = '/home/' ###remote_path为远程文件路径
-
- ###文件传输做一个判断
- try:
- scpclient.put(local_path, remote_path) ###
- scpclient.get(local_path, remote_path) ###
- except FileNotFoundError as e:
- print(e)
- print("系统找不到指定文件" + local_path)
- else:
- print("文件上传成功")