python paramiko模块简介
一:简介
paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。
由于使用的是python这样的能够跨平台运行的语言,所以所有python支持的平台,如Linux, Solaris, BSD, MacOS X, Windows等,paramiko都可以支持,因此,如果需要使用SSH从一个平台连接到另外一个平台,进行一系列的操作时,paramiko是最佳工具之一。
举个常见的例子,现有这样的需求:需要使用windows客户端,远程连接到Linux服务器,查看上面的日志状态,大家通常使用的方法会是:
1:用telnet
2:用PUTTY
3:用WinSCP
4:用XManager等…
那现在如果需求又增加一条,要从服务器上下载文件,该怎么办?那常用的办法可能会是:
1:Linux上安装FTP并配置
2:Linux上安装Sambe并配置…
大家会发现,常见的解决方法都会需要对远程服务器必要的配置,如果远程服务器只有一两台还好说,如果有N台,还需要逐台进行配置,或者需要使用代码进行以上操作时,上面的办法就不太方便了。
使用paramiko可以很好的解决以上问题,比起前面的方法,它仅需要在本地上安装相应的软件(python以及PyCrypto),对远程服务器没有配置要求,对于连接多台服务器,进行复杂的连接操作特别有帮助。
二:安装
安装paramiko有两个先决条件,python和另外一个名为PyCrypto的模块。
通常安装标准的python模块,只需要在模块的根目录下运行:
1
2
|
python setup.py build
python setup.py install
|
备注:安装前先检查是否安装gcc(yum -y install gcc)
2.1 PyCrypto安装
1
2
3
4
|
wget http:
/
/
ftp.dlitz.net
/
pub
/
dlitz
/
crypto
/
pycrypto
/
pycrypto
-
2.6
.tar.gz
tar
-
zxvf pycrypto
-
2.6
.tar.gz
cd pycrypto
-
2.6
/
python setup.py build && python setup.py install
|
测试:
1
|
python>>
import
Crypto
|
(编译时报错:error: command 'gcc' failed with exit status 1;这是因为缺少python-dev的软件包,所yum -y install python-devel)
2.2 paramiko安装
1
2
3
4
5
|
wget http:
/
/
www.lag.net
/
paramiko
/
download
/
paramiko
-
1.7
.
7.1
.tar.gz
tar xvzf paramiko
-
1.7
.
7.1
.tar.gz
cd paramiko
-
1.7
.
7.1
/
python setup.py build && python setup.py install
Crypto error:
'module'
object
has no attribute
'HAVE_DECL_MPZ_POWM_SEC'
|
测试:
1
2
3
4
5
6
7
|
python>>
import
paramiko
(Crypto error:
'module'
object
has no attribute
'HAVE_DECL_MPZ_POWM_SEC'
找到
/
usr
/
lib
/
python2.
7
/
site
-
packages
/
Crypto
/
Util
/
number.py
把
if
_fastmath
is
not
None
and
not
_fastmath.HAVE_DECL_MPZ_POWM_SEC:
注释了
#if _fastmath is not None and not _fastmath.HAVE_DECL_MPZ_POWM_SEC:
)
|
三: 使用paramiko
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#设置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有两个模块SSHClient()和SFTPClient()
3.1、利用SSHClient()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#coding:utf-8
import
paramiko
#创建SSH对象
ssh
=
paramiko.SSHClient()
# 允许连接不在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变量,然后执行连接操作
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#coding:utf-8
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()
|
3.2、用transport实现上传下载以及命令的执行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#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()
|
四,与linux连接
下面是两种使用paramiko连接到linux服务器的代码
方式一:
1
2
3
|
ssh
=
paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
"某IP地址"
,
22
,
"用户名"
,
"口令"
)
|
方式二:
1
2
|
t
=
paramiko.Transport((“主机”,”端口”))
t.connect(username
=
“用户名”, password
=
“口令”)
|
SFTPClient()也是使用transport来实现的,因此如果有需求需要执行命令和上传文件糅合在一起的话,那么就需要使用transport的方式来实现。
如果连接远程主机需要提供密钥,上面第二行代码可改成:
1
|
t.connect(username
=
“用户名”, password
=
“口令”, hostkey
=
”密钥”)
|
4.1 windows对linux运行任意命令,并将结果输出
如果linux服务器开放了22端口,在windows端,我们可以使用paramiko远程连接到该服务器,并执行任意命令,然后通过 print或其它方式得到该结果,
代码如下
1
2
3
4
5
6
7
8
9
10
|
#coding:Utf8
import
paramiko
ssh
=
paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
"某IP地址"
,
22
,
"用户名"
,
"口令"
)
stdin, stdout, stderr
=
ssh.exec_command(
"你的命令"
)
print
stdout.readlines()
ssh.close()
|
其中的”你的命令”可以任意linux支持的命令
4.2 从widnows端下载linux服务器上的文件
1
2
3
4
5
6
7
8
9
10
11
|
coding:utf8
import
paramiko
t
=
paramiko.Transport((“主机”,”端口”))
t.connect(username
=
“用户名”, password
=
“口令”)
sftp
=
paramiko.SFTPClient.from_transport(t)
remotepath
=
’
/
var
/
log
/
system.log’
localpath
=
’
/
tmp
/
system.log’
sftp.get(remotepath, localpath)
t.close()
|
4.3 从widnows端上传文件到linux服务器
1
2
3
4
5
6
7
8
9
|
import
paramiko
t
=
paramiko.Transport((“主机”,”端口”))
t.connect(username
=
“用户名”, password
=
“口令”)
sftp
=
paramiko.SFTPClient.from_transport(t)
remotepath
=
’
/
var
/
log
/
system.log’
localpath
=
’
/
tmp
/
system.log’
sftp.put(localpath,remotepath)
t.close()
|
4.4 在Linux上安装paramiko模块
安装scrapy这个应用中遇到的问题
1
2
3
4
5
6
7
8
9
10
|
c
/
_cffi_backend.c:
2
:
20
: fatal error: Python.h: No such
file
or
directory
sudo apt
-
get install python
-
dev
c
/
_cffi_backend.c:
13
:
17
: fatal error: ffi.h: No such
file
or
directory
1
sudo apt
-
get install libffi
-
dev
*
make sure the development packages of libxml2
and
libxslt are installed
*
1
sudo apt
-
get install libxslt1
-
dev
1.
下载安装wget http:
/
/
peak.telecommunity.com
/
dist
/
ez_setup.py
2.python
ez_setup.py
3.easy_install
paramiko
|