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
50
|
我的博客已迁移到xdoujiang.com请去那边和我交流
一、基础环境
1、角色、ip、版本、内核
serverA 10.1.10.117 3.2.0-4-amd64 7.8 python pycrypto paramiko ecdsa
python-2.7.3
pycrypto-2.6.1
paramiko-1.15.3
pycrypto-2.6.1.
tar
.gz
ecdsa-0.13.
tar
.gz
paramiko-1.15.3.
tar
.gz
二、apt方式安装paramiko
1、安装基础包
apt-get -y
install
gcc python-dev python-setuptools
2、安装pip命令
easy_install pip
3、查询下是否有安装的包
1)pip search
"pycrypto"
pycrypto - Cryptographic modules
for
Python.
2)pip search
"paramiko"
paramiko - SSH2 protocol library
4、使用pip方式安装
1)pip
install
pycrypto
Collecting pycrypto
/usr/local/lib/python2
.7
/dist-packages/pip-7
.1.2-py2.7.egg
/pip/_vendor/requests/packages/urllib3/util/ssl_
.py:90: InsecurePlatformWarning: A
true
SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For
more
information, see https:
//urllib3
.readthedocs.org
/en/latest/security
.html
#insecureplatformwarning.
InsecurePlatformWarning
Downloading pycrypto-2.6.1.
tar
.gz (446kB)
100% |████████████████████████████████| 446kB 96kB
/s
Installing collected packages: pycrypto
Running setup.py
install
for
pycrypto
Successfully installed pycrypto-2.6.1
/usr/local/lib/python2
.7
/dist-packages/pip-7
.1.2-py2.7.egg
/pip/_vendor/requests/packages/urllib3/util/ssl_
.py:90: InsecurePlatformWarning: A
true
SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For
more
information, see https:
//urllib3
.readthedocs.org
/en/latest/security
.html
#insecureplatformwarning.
InsecurePlatformWarning
2)pip
install
paramiko
Collecting paramiko
/usr/local/lib/python2
.7
/dist-packages/pip-7
.1.2-py2.7.egg
/pip/_vendor/requests/packages/urllib3/util/ssl_
.py:90: InsecurePlatformWarning: A
true
SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For
more
information, see https:
//urllib3
.readthedocs.org
/en/latest/security
.html
#insecureplatformwarning.
InsecurePlatformWarning
Downloading paramiko-1.15.3-py2.py3-none-any.whl (166kB)
100% |████████████████████████████████| 167kB 104kB
/s
Collecting ecdsa>=0.11 (from paramiko)
Downloading ecdsa-0.13-py2.py3-none-any.whl (86kB)
100% |████████████████████████████████| 90kB 165kB
/s
Requirement already satisfied (use --upgrade to upgrade): pycrypto!=2.4,>=2.1
in
/usr/local/lib/python2
.7
/dist-packages
(from paramiko)
Installing collected packages: ecdsa, paramiko
Successfully installed ecdsa-0.13 paramiko-1.15.3
三、测试
|
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
|
四、
tar
包安装paramiko
1、安装基础包
apt-get -y
install
gcc python-dev
2、下载pycrypto
wget --no-check-certificate https:
//pypi
.python.org
/packages/source/p/pycrypto/pycrypto-2
.6.1.
tar
.gz
#md5=55a61a054aa66812daf5161a0d5d7eda
3、解压并进入相关目录
tar
zxvf pycrypto-2.6.1.
tar
.gz &&
cd
pycrypto-2.6.1
4、检查环境
.
/configure
5、编译安装
python setup.py build && python setup.py
install
6、下载ecdsa
wget --no-check-certificate https:
//pypi
.python.org
/packages/source/e/ecdsa/ecdsa-0
.13.
tar
.gz
#md5=1f60eda9cb5c46722856db41a3ae6670
7、解压并进入相关目录
tar
zxvf ecdsa-0.13.
tar
.gz &&
cd
ecdsa-0.13
8、编译安装
python setup.py build && python setup.py
install
9、下载paramiko
wget --no-check-certificate https:
//pypi
.python.org
/packages/source/p/paramiko/paramiko-1
.15.3.
tar
.gz
#md5=c959088669f8d951585aa6abc25c8ef6
10、解压并进入相关目录
tar
zxvf paramiko-1.15.3.
tar
.gz &&
cd
paramiko-1.15.3
11、
|
1
2
3
4
|
12、编译安装
python setup.py build && python setup.py
install
五、测试
|
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
六、使用paramiko模块(eg:多台服务器查询)
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#--------------------------------------------------
#Author:jimmygong
#Email:jimmygong@taomee.com
#FileName:paramiko.py
#Function:
#Version:1.0
#Created:2015-10-13
#--------------------------------------------------
import
re
import
fileinput
import
os
import
sys
import
paramiko
host=
"host.txt"
cmd=
"cmd.txt"
if
os.path.isfile(host) and os.path.isfile(cmd):
print
"%s and %s exists,you can continue"
% (host,cmd)
else
:
print
"%s or %s is lost,please check!"
% (host,cmd)
sys.
exit
()
port=22
user=
"root"
password=
"redhat"
i=0
cmd1=
"cat %s|grep '10.1'|wc -l"
% host
num1=os.popen(cmd1).
read
().strip()
s=paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for
line
in
fileinput.input(host,inplace=0):
if
re.search(
"10.1"
,line):
i+=1
host=line.strip()
t=paramiko.Transport((host,port))
t.connect(username=user,password=password)
s.connect(host,port,user,password,timeout=5)
f=
open
(cmd)
while
True:
cmd_line=f.readline()
if
len(cmd_line) == 0:
break
stdin,stdout,stderr=s.exec_command(cmd_line)
cmd_result=stdout.
read
(),stderr.
read
()
for
result
in
cmd_result:
print
"\033[32m %s \033[0m\n"
% result,
f.close()
print
"\x1B[0;33m %d/%d %s\x1B[0m"
% (int(i),int(num1),line)
m=i
else
:
pass
print
"\033[32m allnum %d \033[0m\n"
% int(m)
print
"#"
*40
s.close()
PS:相关文件
cat
cmd.txt
crontab
-l
cat
host.txt
10.1.10.185
10.1.10.24
七、效果
|
1
2
3
4
5
6
|
八、参考文章
https:
//pypi
.python.org
/pypi/pycrypto
https:
//pypi
.python.org
/pypi/paramiko
https:
//pypi
.python.org
/pypi/ecdsa/
https:
//github
.com
/paramiko/paramiko/
http:
//docs
.paramiko.org
/en/1
.15
/api/agent
.html
|
本文转自 xdoujiang 51CTO博客,原文链接:http://blog.51cto.com/7938217/1702704,如需转载请自行联系原作者