1 上傳檔案
# 從本地上傳到伺服器檔案
import paramiko
'''
remote_dir 遠端目标路徑
local_dir 本地路徑
hostname 遠端ip
username 使用者名
password
port 端口
'''
def remote_file(local_dir,remote_dir,hostname='hostname',username='root',password='passwd',port='22'):
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh_client.connect(hostname=hostname, port=port, username=username, password=password)
# 擷取傳輸執行個體
tran=ssh_client.get_transport()
sftp=paramiko.SFTPClient.from_transport(tran)
sftp.put(local_dir,remote_dir)
print("##########################上傳完成###################")
ssh_client.close()
remote_file('D:\python21\python\練習\hnf\dddd.txt','/root/aa','192.168.204.128','root','redhat','22',)
2 下載下傳檔案
# 從遠端伺服器下載下傳檔案
import paramiko
'''
remote_dir 遠端目标路徑
local_dir 本地路徑
hostname 遠端ip
username 使用者名
password
port 端口
'''
def remote_file(remote_dir,local_dir,hostname='hostname',username='root',password='passwd',port='22'):
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh_client.connect(hostname=hostname, port=port, username=username, password=password)
# 擷取傳輸執行個體
tran=ssh_client.get_transport()
sftp=paramiko.SFTPClient.from_transport(tran)
sftp.get(remote_dir,local_dir)
print("##########################下載下傳完成###################")
ssh_client.close()
remote_file('/root/hnf.txt','D:\python21\python\練習\hnf\dddd.txt','192.168.204.128','root','redhat','22',)