paramiko模块介绍
ssh的远程连接
基于用户名密码的连接
import paramiko # 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='x.x.x.x', port=, username='xiaoli, password='') # 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
result = stdout.read() # 关闭连接
ssh.close()
基于封装的transport的连接
import paramiko # 创建transport对象
transport = paramiko.Transport(('x.x.x.x', ))
transport.connect(username='xiaoli', password='') ssh = paramiko.SSHClient()
ssh._transport = transport stdin, stdout, stderr = ssh.exec_command('df')
print stdout.read() transport.close()
基于公钥密钥连接
跟密码连接差不多,只是密码的地方变成了私钥 import paramiko #私钥路径
private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa') # 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='x.x.x.x, port=22, username='xiaoli, key=private_key) # 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
result = stdout.read() # 关闭连接
ssh.close()
基于封装的transport的连接
同理,跟上边的差不多 import paramiko private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa') transport = paramiko.Transport(('x.x.x.x', ))
transport.connect(username='xiaoli', pkey=private_key) ssh = paramiko.SSHClient()
ssh._transport = transport stdin, stdout, stderr = ssh.exec_command('df') transport.close()
SFTP的上传下载
基于用户名密码的上传下载,只支持transport形式的连接然后实现上传下载
简单小例 import paramiko transport = paramiko.Transport(('x.x.x.x',))
transport.connect(username='xiaoli',password='') sftp = paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put('/tmp/location.py', '/tmp/test.py')
# 将remove_path 下载到本地 local_path
sftp.get('remove_path', 'local_path') transport.close()
基于密钥的上传下载
import paramiko private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa') transport = paramiko.Transport(('x.x.x.x.', ))
transport.connect(username='xiaoli', pkey=private_key ) sftp = paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put('/tmp/location.py', '/tmp/test.py')
# 将remove_path 下载到本地 local_path
sftp.get('remove_path', 'local_path') transport.close()
一个简单的上传下载,文件
#!/usr/bin/env python
#_*_coding:utf-8_*_ #上传以及重命名
import paramiko
import uuid
class Haproxy(object):
def __init__(self):
self.host = 'x.x.x.x.'
self.port =
self.username = 'root'
self.pwd = ''
def create_file(self):
filename = str(uuid.uuid4())
with open(filename,'w') as f:
f.write('sb')
return filename
def connect(self):
transport = paramiko.Transport((self.host,self.port))
transport.connect(username=self.username,password=self.pwd)
self.__transport = transport
def close(self):
self.__transport.close()
def run(self):
self.connect()
self.upload()
self.rename()
self.close() def upload(self):
file_name = self.create_file()
sftp = paramiko.SFTPClient.from_transport(self.__transport)
sftp.put(file_name,'/home/zdk/tttttt.py') def rename(self):
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
stdin, stdout ,stderr = ssh.exec_command('mv /home/zdk/tttttt.py /home/zdk/aaaaaaa.py')
result = stdout.read()
ssh.close()
a = Haproxy()
a.run()