一、paramiko上传
代码块:
from paramiko.ssh_exception import NoValidConnectionsError,AuthenticationException
import paramiko
def put(hostname,password,source_name,target_name):
try:
transport = paramiko.Transport((hostname,22))
transport.connect(username='root',password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
except AuthenticationException as e:
return '主机%s密码错误' %(hostname)
except Exception as e:
return '未知错误: ',e
else:
sftp.put(source_name,target_name)
finally:
transport.close()
put('172.25.254.110','redhat','/tmp/hosts','/mnt/westos')
程序及运行结果:
注意:/tmp/hosts文件是自己建立的
二、paramiko下载
代码块:
from paramiko.ssh_exception import NoValidConnectionsError,AuthenticationException
import paramiko
def put(hostname,password,source_name,target_name):
try:
transport = paramiko.Transport((hostname,22))
transport.connect(username='root',password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
except AuthenticationException as e:
return '主机%s密码错误' %(hostname)
except Exception as e:
return '未知错误: ',e
else:
sftp.get(source_name,target_name)
finally:
transport.close()
put('172.25.254.110','redhat','/mnt/westos','/tmp/1111')
程序及运行结果: