Python之路第十二天,高级(6)-paramiko
paramiko
一、安装
pip3 install paramiko
二、使用
SSHClient
用于连接远程服务器并执行基本命令
1. 基于用户和密码链接
import paramiko
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='192.168.224.136', port=22, username='zhangxunan', password='123456')
# 执行命令
stdin, stdout, stderr = ssh.exec_command('ls')
# 获取命令结果
result = stdout.read().decode()
print(result)
# 关闭连接
ssh.close()
SSHClient封装transport
import paramiko
transport = paramiko.Transport(('192.168.224.136', 22))
transport.connect(username='zhangxunan', password='123456')
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('df')
print(stdout.read().decode())
transport.close()
基于公钥密钥连接:
import paramiko
private_key = paramiko.RSAKey.from_private_key_file('/home/zhangxunan/.ssh/id_rsa')
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='192.168.224.137', port=22, username='zhangxunan', key=private_key)
# 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
result = stdout.read().decode()
# 关闭连接
ssh.close()
SSHClient封装transport
import paramiko
private_key = paramiko.RSAKey.from_private_key_file('/home/zhangxunan/.ssh/id_rsa')
transport = paramiko.Transport(('hostname', 22))
transport.connect(username='zhangxunan', pkey=private_key)
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('df')
transport.close()
SFTPClient
用于连接远程服务器并执行上传下载:
基于用户名密码上传下载:
import paramiko
transport = paramiko.Transport(('192.168.224.136',22))
transport.connect(username='zhangxunan',password='123456')
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/zhangxunan/.ssh/id_rsa')
transport = paramiko.Transport(('192.168.224.137', 22))
transport.connect(username='zhangxunan', 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 threading
import paramiko
class Anstack(object):
def __init__(self,host,cmd):
#host为变量
self.host = host
self.cmd = cmd
self.port = 22
self.username = 'root'
self.pwd = '123456'
self.run()
def run(self):
#执行方法
self.connect()
self._cmd()
self.close()
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 _cmd(self):
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
# 执行命令
stdin, stdout, stderr = ssh.exec_command(self.cmd)
# 获取命令结果
result = stdout.read()
print(result)
if __name__ == '__main__':
args = ['192.168.1.1','192.168.1.2','192.168.1.3','192.168.1.4']
cmd = input('您要执行的命令')
for i in args:
Execcmd = threading.Thread(target=Anstack, args=(i,cmd,))
Execcmd.start()