python paramiko的使用介绍

Posted 叶落kiss

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python paramiko的使用介绍相关的知识,希望对你有一定的参考价值。

一: 使用paramiko

#设置ssh连接的远程主机地址和端口
t=paramiko.Transport((ip,port))
#设置登录名和密码
t.connect(username=username,password=password)
#连接成功后打开一个channel
chan=t.open_session()
#设置会话超时时间
chan.settimeout(session_timeout)
#打开远程的terminal
chan.get_pty()
#激活terminal
chan.invoke_shell()
然后就可以通过chan.send(‘command‘)和chan.recv(recv_buffer)来远程执行命令以及本地获取反馈。

 二: paramiko的两个模块介绍

paramiko有两个模块SSHClient()和SFTPClient()

SSHClient()的使用代码:

import paramiko

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

stdin, stdout, stderr = ssh.exec_command(‘ls‘)  # 执行命令
result = stdout.read()  # 获取命令结果
print (str(result,encoding=‘utf-8‘))
ssh.close()  # 关闭连接

SSHClient()里有个transport变量,是用于获取连接,我们也可单独的获取到transport变量,然后执行连接操作

import paramiko

transport = paramiko.Transport((‘192.168.2.103‘, 22))
transport.connect(username=‘root‘, password=‘123456‘)

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

stdin, stdout, stderr = ssh.exec_command(‘df‘)
print (str(stdout.read(),encoding=‘utf-8‘))

transport.close()

  

用transport实现上传下载以及命令的执行:

#coding:utf-8
import paramiko
import uuid

class SSHConnection(object):

def __init__(self, host=‘192.168.2.103‘, port=22, username=‘root‘,pwd=‘123456‘):
self.host = host
self.port = port
self.username = username
self.pwd = pwd
self.__k = None

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,local_path,target_path):
# 连接,上传
# file_name = self.create_file()
sftp = paramiko.SFTPClient.from_transport(self.__transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(local_path, target_path)

def download(self,remote_path,local_path):
sftp = paramiko.SFTPClient.from_transport(self.__transport)
sftp.get(remote_path,local_path)

def cmd(self, command):
ssh = paramiko.SSHClient()
ssh._transport = self.__transport
# 执行命令
stdin, stdout, stderr = ssh.exec_command(command)
# 获取命令结果
result = stdout.read()
print (str(result,encoding=‘utf-8‘))
return result

ssh = SSHConnection()
ssh.connect()
ssh.cmd("ls")
ssh.upload(‘s1.py‘,‘/tmp/ks77.py‘)
ssh.download(‘/tmp/test.py‘,‘kkkk‘,)
ssh.cmd("df")
ssh.close()


技术分享图片
 1 #coding:utf-8
 2 import paramiko
 3 import uuid
 4 
 5 class SSHConnection(object):
 6 
 7     def __init__(self, host=‘192.168.2.103‘, port=22, username=‘root‘,pwd=‘123456‘):
 8         self.host = host
 9         self.port = port
10         self.username = username
11         self.pwd = pwd
12         self.__k = None
13 
14     def connect(self):
15         transport = paramiko.Transport((self.host,self.port))
16         transport.connect(username=self.username,password=self.pwd)
17         self.__transport = transport
18 
19     def close(self):
20         self.__transport.close()
21 
22     def upload(self,local_path,target_path):
23         # 连接,上传
24         # file_name = self.create_file()
25         sftp = paramiko.SFTPClient.from_transport(self.__transport)
26         # 将location.py 上传至服务器 /tmp/test.py
27         sftp.put(local_path, target_path)
28 
29     def download(self,remote_path,local_path):
30         sftp = paramiko.SFTPClient.from_transport(self.__transport)
31         sftp.get(remote_path,local_path)
32 
33     def cmd(self, command):
34         ssh = paramiko.SSHClient()
35         ssh._transport = self.__transport
36         # 执行命令
37         stdin, stdout, stderr = ssh.exec_command(command)
38         # 获取命令结果
39         result = stdout.read()
40         print (str(result,encoding=‘utf-8‘))
41         return result
42 
43 ssh = SSHConnection()
44 ssh.connect()
45 ssh.cmd("ls")
46 ssh.upload(‘s1.py‘,‘/tmp/ks77.py‘)
47 ssh.download(‘/tmp/test.py‘,‘kkkk‘,)
48 ssh.cmd("df")
49 ssh.close()
技术分享图片























































以上是关于python paramiko的使用介绍的主要内容,如果未能解决你的问题,请参考以下文章

python第三方模块之paramiko模块

python paramiko模块

Python 之Paramiko模块

python3之paramiko模块

paramiko安装和使用

Paramiko模块,用python代码连接服务器执行命令