paramiko 模块
paramiko 模块
paramiko是一个基于SSH用于连接远程服务器并执行相关操作(SSHClient和SFTPClinet,即一
个是远程连接,一个是上传下载服务),使用该模块可以对远程服务器进行命令或文件操作,
值得一说的是,fabric和ansible内部的远程管理就是使用的paramiko来现实
下载安装
pip install paramiko
远程链接
远程链接分为两种:一种是基于用户名密码
一种是基于公钥和私钥
基于用户名密码
import paramiko
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='ip地址', port=22, username='root', password='@999')
# 执行命令
stdin, stdout, stderr = ssh.exec_command('ls')
# 获取命令结果
result = stdout.read()
print(result)
# 关闭连接
ssh.close()
# SSHClient()封装transport
import paramiko
transport = paramiko.Transport(('ip地址', 22))
transport.connect(username='root', password='密码')
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('ssh命令')
print(stdout.read())
transport.close()
基于公钥和私钥
import paramiko
private_key = paramiko.RSAKey.from_private_key_file('id_rsa路径')
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='hostname', port=22, username='username', key=private_key)
# 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
result = stdout.read()
# 关闭连接
ssh.close()
# SSHClient()封装transport
import paramiko
private_key = paramiko.RSAKey.from_private_key_file('id_rsa路劲')
transport = paramiko.Transport(('hostname', 22))
transport.connect(username='root', pkey=private_key)
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('ssh命令')
transport.close()
远程操作服务器
远程操作分为两种:一种是SSHClient(),对服务器进行命令操作
一种是SFTPClient.from_transport(), 上传下载服务
SFTPClient.from_transport()
# 基于公钥私钥
import paramiko
private_key = paramiko.RSAKey.from_private_key_file('id_rsa路劲')
transport = paramiko.Transport(('hostname', 22))
transport.connect(username='用户名', 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()
# 基于用户名密码
import paramiko
transport = paramiko.Transport(('ip地址', 22))
transport.connect(username='root', password='密码')
sftp = paramiko.SFTPClient.from_transport(transport)
# 将up.py 上传至服务器
# sftp.put('up.py', 'up.py')
#将服务器的文件 下载到本地
sftp.get('up9.py', 'up_9.py')
transport.close()
paramiko之SSH进阶版
'''
实现一次链接服务器,操作多次命令,手动关闭transport
'''
import paramiko
class SSHProxy(object):
def __init__(self, hostname, port, username, private_key_path):
self.hostname = hostname
self.port = port
self.username = username
self.private_key_path = private_key_path
self.transport = None
def open(self):
private_key = paramiko.RSAKey.from_private_key_file(self.private_key_path)
self.transport = paramiko.Transport(self.hostname, self.port)
self.transport.connect(username=self.username, pkey=private_key)
def close(self):
self.transport.close()
def command(self, cmd):
ssh = paramiko.SSHClient()
ssh._transport = self.transport
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read()
ssh.close()
return result
def upload(self, local_path, remote_path):
sftp = paramiko.SFTPClient.from_transport(self.transport)
sftp.put(local_path, remote_path)
sftp.close()
def __enter__(self):
self.open()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
if __name__ == '__main__':
with SSHProxy('192.168.16.129', 22, 'root', 'id_rsa') as proxy:
# proxy.upload('your.tar', '/data/your.tar')
v = proxy.command('ifconfig')
print(v)
SSHProxy组件,(StringIO)私钥读写在内存
import paramiko
from io import StringIO
class SSHProxy(object):
def __init__(self, hostname, port, username, private_key_string):
self.hostname = hostname
self.port = port
self.username = username
self.private_key = paramiko.RSAKey(file_obj=StringIO(private_key_string))
self.transport = None
def open(self):
self.transport = paramiko.Transport((self.hostname, self.port))
self.transport.connect(username=self.username, pkey=self.private_key)
def close(self):
self.transport.close()
def command(self, cmd):
ssh = paramiko.SSHClient()
ssh._transport = self.transport
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read()
# ssh.close()
return result
def upload(self, local_path, remote_path):
sftp = paramiko.SFTPClient.from_transport(self.transport)
sftp.put(local_path, remote_path)
sftp.close()
def __enter__(self):
self.open()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
if __name__ == '__main__':
with SSHProxy('1.2.3.4', 22, 'root', 'hello') as proxy:
proxy.upload('xx','xx')
proxy.command('ifconfig')
proxy.command('ifconfig')
proxy.upload('xx', 'xx')
with SSHProxy('5.6.7.8', 22, 'root', 'world') as proxy:
proxy.upload('xx','xx')
proxy.command('ifconfig')
proxy.command('ifconfig')
proxy.upload('xx', 'xx')
希望你眼眸有星辰,心中有山海,从此以梦为马,不负韶华