paramiko
一、安装
pip3 install paramiko
二、使用
1、SSHClient
用于连接远程服务器并执行基本命令
import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname=‘c1.salt.com‘, port=22, username=‘wupeiqi‘, password=‘123‘) # 执行命令 stdin, stdout, stderr = ssh.exec_command(‘ls‘) # 获取命令结果 result = stdout.read() # 关闭连接 ssh.close()
看代码:
1、ssh = paramiko.SSHClient()
def __init__(self): """ Create a new SSHClient. """ self._system_host_keys = HostKeys() self._host_keys = HostKeys() self._host_keys_filename = None self._log_channel = None self._policy = RejectPolicy() self._transport = None self._agent = None
2、ssh.connect(hostname=‘c1.salt.com‘, port=22, username=‘wupeiqi‘, password=‘123‘) 326 t = self._transport = Transport(sock, gss_kex=gss_kex, gss_deleg_creds=gss_deleg_creds)
#其实SSHClient内部就是定义了transport来链接的
import paramiko #创建transport指定主机地址和端口 transport = paramiko.Transport((‘hostname‘, 22)) #使用transport连接主机 transport.connect(username=‘wupeiqi‘, password=‘123‘) #创建ssh ssh = paramiko.SSHClient() #赋值transport ssh._transport = transport stdin, stdout, stderr = ssh.exec_command(‘df‘) print stdout.read() transport.close()
SSHClient 封装了 transport
使用公钥链接
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=‘c1.salt.com‘, port=22, username=‘wupeiqi‘, key=private_key) # 执行命令 stdin, stdout, stderr = ssh.exec_command(‘df‘) # 获取命令结果 result = stdout.read() # 关闭连接 ssh.close()
import paramiko #定义秘钥位置 private_key = paramiko.RSAKey.from_private_key_file(‘/home/auto/.ssh/id_rsa‘) #创建transport transport = paramiko.Transport((‘hostname‘, 22)) #使用transport 指定用户名,指定秘钥 transport.connect(username=‘wupeiqi‘, pkey=private_key) #连接 ssh = paramiko.SSHClient() #使用将transport赋值 ssh._transport = transport #执行命令 stdin, stdout, stderr = ssh.exec_command(‘df‘) transport.close()
SSHClient 封装了 transport
2、SFTPClient
用于连接远程服务器并执行上传下载
基于用户名密码上传下载:
import paramiko #使用 transport 创建链接 transport = paramiko.Transport((‘hostname‘,22)) transport.connect(username=‘wupeiqi‘,password=‘123‘) #并且将transport倒入 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() 基于公钥密钥上传下载:
3、总结通过上面的ssh 和 sftp 可以看出来paramiko的链接都是使用的transport来实现的
#!/usr/bin/env python # -*- coding:utf-8 -*- import paramiko import uuid class SSHConnection(object): def __init__(self, host=‘172.16.103.191‘, port=22, username=‘wupeiqi‘,pwd=‘123‘): self.host = host self.port = port self.username = username self.pwd = pwd self.__k = None def create_file(self): file_name = str(uuid.uuid4()) with open(file_name,‘w‘) as f: f.write(‘sb‘) return file_name def run(self): self.connect() self.upload(‘/home/wupeiqi/tttttttttttt.py‘) self.rename(‘/home/wupeiqi/tttttttttttt.py‘, ‘/home/wupeiqi/ooooooooo.py) 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 upload(self,target_path): # 连接,上传 file_name = self.create_file() sftp = paramiko.SFTPClient.from_transport(self.__transport) # 将location.py 上传至服务器 /tmp/test.py sftp.put(file_name, target_path) def rename(self, old_path, new_path): ssh = paramiko.SSHClient() ssh._transport = self.__transport # 执行命令 cmd = "mv %s %s" % (old_path, new_path,) stdin, stdout, stderr = ssh.exec_command(cmd) # 获取命令结果 result = stdout.read() def cmd(self, command): ssh = paramiko.SSHClient() ssh._transport = self.__transport # 执行命令 stdin, stdout, stderr = ssh.exec_command(command) # 获取命令结果 result = stdout.read() return result ha = SSHConnection() ha.run()
时间: 2024-10-21 10:29:37