paramiko模块,基于SSH用于连接远程服务器并执行相关操作。
 

使用

(1)SSHClient

用于连接远程服务器并执行基本命令

基于用户名密码连接:

 

#!/usr/bin/env python
# Version = 3.5.2
# __auth__ = 'xxx'
import paramiko

# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='192.168.168.231', port=22, username='oracle', password='oracle')

# 执行命令
stdin, stdout, stderr = ssh.exec_command('ls')
# 获取命令结果
result = stdout.read()
print(result.decode())
# 关闭连接
ssh.close()
-----------------
使用transport :
#!/usr/bin/env python
# Version = 3.5.2
# __auth__ = 'xxx'
import paramiko

transport = paramiko.Transport(('192.168.168.231', 22))
transport.connect(username='oracle', password='oracle')

ssh = paramiko.SSHClient()
ssh._transport = transport

stdin, stdout, stderr = ssh.exec_command('df')
print(stdout.read().decode())

transport.close()
--------------------------

(2)SFTPClient

用于连接远程服务器并执行上传下载

基于用户名密码上传下载:

import paramiko
  
transport = paramiko.Transport(('192.168.168.231',22))
transport.connect(username='root',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/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 = paramiko.Transport(('hostname', 22))
transport.connect(username='wupeiqi', 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()
 
-------------------------------------------------------------------------------------
实用小例子:(使用transport既执行命令又上传下载文件)
#!/usr/bin/env python
# Version = 3.5.2
# __auth__ = 'xxx'
import paramiko


class SSHConnection(object):

def __init__(self, host='192.168.168.231', port=22, username='oracle', pwd='oracle'):
self.host = host
self.port = port
self.username = username
self.pwd = pwd
self.__k = None

def run(self):
self.connect()
pass
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, command):
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
# 执行命令
stdin, stdout, stderr = ssh.exec_command(command)
# 获取命令结果
result = stdout.read()
return result

def upload(self,local_path, target_path):
# 连接,上传
sftp = paramiko.SFTPClient.from_transport(self.__transport)
# location.py 上传至服务器 /tmp/test.py
sftp.put(local_path, target_path)

ssh = SSHConnection()
ssh.connect()
r1 = ssh.cmd('df')
print(r1.decode())
ssh.upload('class12.py', "test.py")
ssh.close()
-------------------------------------------------------------------

实现远程登录并进行操作

#!/usr/bin/env python # Version = 3.5.2 # __auth__ = 'xxx' import paramiko import sys import os import socket import select import getpass from paramiko.py3compat import u tran = paramiko.Transport(('192.168.191.3', 22,)) tran.start_client() tran.auth_password('root', '7ujm8ik,') # 打开一个通道 chan = tran.open_session() # 获取一个终端 chan.get_pty() # 激活器 chan.invoke_shell() while True: # 监视用户输入和服务器返回数据 # sys.stdin 处理用户输入 # chan 是之前创建的通道,用于接收服务器返回信息 readable, writeable, error = select.select([chan, sys.stdin, ], [], [], 1) if chan in readable: try: x = u(chan.recv(1024)) if len(x) == 0: print('\r\n*** EOF\r\n') break sys.stdout.write(x) sys.stdout.flush() except socket.timeout: pass if sys.stdin in readable: inp = sys.stdin.readline() chan.sendall(inp) chan.close() tran.close()


参考文档 :http://www.cnblogs.com/wupeiqi/articles/5699254.html