一、paramiko简介
-
paramiko是用Python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。
-
paramiko主要是通过ssh协议对远程主机进行管理:包括执行远程主机CLI、上传和下载文件等。
二、快速安装paramiko模块
pip的详细安装请参考我的另外一篇文章:http://467754239.blog.51cto.com/4878013/1613612
1
2
|
# yum install python-devel
# pip install paramiko
|
三、paramiko命令参数详解
-
利用密码登陆方式批量执行命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
导入模块
import
paramiko
实例化一个SSHClient对象
s
=
paramiko.SSHClient()
自动添加策略
#首次登陆用交互式确定(允许连接不在know_hosts文件中的主机)
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
要连接的主机地址信息
s.connect(hostname
=
'ip地址'
, port
=
端口号, username
=
'用户名'
, password
=
'密码'
)
要执行的命令
stdin, stdout, stderr
=
s.exec_command(
'执行的命令'
)
查看命令的执行结果
print
stdout.read()
|
-
利用公钥验证方式批量执行命令
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
|
首先建立公钥和私钥文件
# ssh-keygen
上传本地的公钥到远程主机的.ssh
/
authorized_keys文件中
# ssh-copy-id -i .ssh/id_rsa.pub root@ip
导入模块
import
paramiko
实例化一个对象
s
=
paramiko.SSHClient()
自动添加策略
#首次登陆用交互式确定
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
建立私钥文件连接
key
=
paramiko.RSAKey.from_private_key_file(
'/root/.ssh/id_rsa'
)
通过私钥验证的方式登录远程主机
s.connect(hostname
=
'ip地址'
, port
=
端口号, username
=
'用户名'
, pkey
=
key)
要执行的命令
stdin, stdout, stderr
=
s.exec_command(
'执行的命令'
)
查看命令的执行结果
print
stdout.read()
|
-
从远程主机上传、下载文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
导入模块
import
paramiko
#建立一个加密的传输管道
s
=
paramiko.Transport((
'ip地址'
,端口号))
#建立连接
s.connect(username
=
'用户名'
,password
=
'密码'
)
#建立一个sftp客户端对象,通过ssh transport操作远程文件
sftp
=
paramiko.SFTPClient.from_transport(s)
#上传本地文件到远程主机
sftp.put(localFile,remoteFile)
#从远程主机下载文件到本地
sftp.get(remoteFile,localFile)
#关闭sftp连接
s.close()
|
四、脚本演示
-
局域网物理主机批量管理:执行命令
脚本还有很多瑕疵,只是作为参考练习,前提是所有主机的用户名、密码和端口号都要相同
例如:Username:root Password:redhat Port:22
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
|
[root@python script]
# cat 02_paramiko_process.py
#!/usr/bin/env python
#coding:utf8
from
multiprocessing
import
Process
import
paramiko
import
sys
Username
=
'root'
Password
=
'redhat'
Port
=
22
def
runCmd(ip,cmd):
s
=
paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try
:
s.connect(hostname
=
ip, port
=
Port, username
=
Username, password
=
Password)
stdin, stdout, stderr
=
s.exec_command(cmd)
result
=
stdout.read()
print
ip,result,
except
:
print
'%s is not exists'
%
ip
def
ipProcess():
try
:
cmd
=
sys.argv[
1
]
for
i
in
range
(
2
,
255
):
ip
=
'192.168.1.%s'
%
i
p
=
Process(target
=
runCmd,args
=
(ip,cmd))
p.start()
except
IndexError:
print
'please input a command.'
if
__name__
=
=
'__main__'
:
ipProcess()
|
执行脚本:
1
|
[root@python script]
# python 02_paramiko_process.py uptime
|
截图部分返回结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
192.168
.
1.57
10
:
48
:
49
up
2
:
29
,
0
users, load average:
0.00
,
0.00
,
0.00
192.168
.
1.56
10
:
50
:
25
up
2
:
29
,
0
users, load average:
0.00
,
0.00
,
0.00
192.168
.
1.100
10
:
48
:
50
up
2
:
29
,
0
users, load average:
0.00
,
0.00
,
0.00
192.168
.
1.127
is
not
exists
192.168
.
1.210
22
:
48
:
51
up
2
:
28
,
0
users, load average:
0.00
,
0.00
,
0.00
192.168
.
1.51
10
:
48
:
52
up
2
:
28
,
0
users, load average:
0.00
,
0.00
,
0.00
192.168
.
1.55
10
:
50
:
26
up
2
:
29
,
0
users, load average:
0.00
,
0.00
,
0.00
192.168
.
1.122
is
not
exists
192.168
.
1.58
is
not
exists
192.168
.
1.52
is
not
exists
192.168
.
1.53
is
not
exists
192.168
.
1.50
is
not
exists
192.168
.
1.215
02
:
31
:
14
up
2
:
07
,
4
users, load average:
4.75
,
1.46
,
0.82
|
-
局域网物理主机批量管理:执行上传、下载文件
这里只对批量上传做了演示,对于批量下载文件只是一行代码的更换就可以了,我也在脚本中添加了注释行。
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
|
[root@python script]
# cat 03_put_paramiko_process.py
#!/usr/bin/env python
#coding:utf8
from
multiprocessing
import
Process
import
paramiko
import
sys
Username
=
'root'
Password
=
'redhat'
Port
=
22
def
sftpPut(ip):
try
:
s
=
paramiko.Transport((ip,Port))
s.connect(username
=
Username,password
=
Password)
sftp
=
paramiko.SFTPClient.from_transport(s)
localFile
=
'/root/sync.sh'
remoteFile
=
'/opt/sync.sh'
sftp.put(localFile,remoteFile)
#sftp.get(remoteFile,localFile)
s.close()
print
'%s put successful.'
%
ip
except
:
print
'%s not exists.'
%
ip
def
ipProcess():
for
i
in
range
(
2
,
255
):
ip
=
'192.168.1.%s'
%
i
p
=
Process(target
=
sftpPut,args
=
(ip,))
p.start()
if
__name__
=
=
'__main__'
:
ipProcess()
|
执行脚本:
1
|
[root@python script]
# python 03_put_paramiko_process.py
|
截图部分返回结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
192.168
.
1.55
put successful.
192.168
.
1.181
not
exists.
192.168
.
1.198
not
exists.
192.168
.
1.200
not
exists.
192.168
.
1.207
not
exists.
192.168
.
1.209
not
exists.
192.168
.
1.57
put successful.
192.168
.
1.51
put successful.
192.168
.
1.56
put successful.
192.168
.
1.233
not
exists.
192.168
.
1.127
not
exists.
192.168
.
1.100
put successful.
192.168
.
1.210
put successful.
|
登陆远程主机查看文件是否上传完成:
1
2
3
4
5
6
7
8
|
[root@python script]
# ssh 192.168.1.51
Address
192.168
.
1.51
maps to localhost, but this does
not
map
back to the address
-
POSSIBLE BREAK
-
IN ATTEMPT!
root@
192.168
.
1.51
's password:
Last login: Wed Mar
11
11
:
04
:
30
2015
from
192.168
.
1.112
[root@naginx ~]
# ifconfig |grep inet |head -1
inet addr:
192.168
.
1.51
Bcast:
192.168
.
1.255
Mask:
255.255
.
255.0
[root@naginx ~]
# ll /opt/sync.sh -d
-
rw
-
r
-
-
r
-
-
1
root root
333
3
月
11
11
:
05
/
opt
/
sync.sh
#已经成功上传文件
|
-
注意:
-
批量上传文件脚本只能满足单个文件,如何实现多个文件上传和下载呢?可以通过os模块来实现,下一篇文章中介绍如何以更人性化的方式实现参数上传下载,那就是os模块和optparse模块
多文件上传、下载请参考此文章:http://467754239.blog.51cto.com/4878013/1619323