python模块:
paramiko
fabric
1. paramiko
installtion:
yum -y install python-devel
pip install paramiko
problem:
1.1 error: command ‘gcc’ failed with exit status 1
这是缺少python-devel软件包,安装即可
1.2 导入paramiko时报错: 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:
use:
1.3 执行命令并将结果输出
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()
1.4 下载远程文件
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()
1.5 上传文件
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()
2. fabric
web:http://docs.fabfile.org/en/1.8/
installation:
yum install python-devel
pip install fabric
code: named fabfile.py
from fabric.api import *
env.username = ‘root‘
env.password = ‘123‘
env.hosts = [‘localhost‘,‘127.0.0.1‘]
def host_type():
run(‘uname -s‘)
def check_ver():
run("""LAST_VER=`curl -s http://deploymaster/deploy/lastver`;
LAST_WP=/var/www/releases/wordpress-${LAST_VER};
test -d ${LAST_WP};
""")
run:
fab host_type
result:
[root@localhost ~]# fab host_type
[localhost] Executing task ‘host_type‘
[localhost] run: uname -s
[localhost] out: Linux
[localhost] out:
[127.0.0.1] Executing task ‘host_type‘
[127.0.0.1] run: uname -s
[127.0.0.1] out: Linux
[127.0.0.1] out:
Done.
Disconnecting from 127.0.0.1... done.
Disconnecting from localhost... done.
fabric应用:
1、fab -H X.X.X.X host_type
2、fab host_type check_ver
3、role: 当client较多时可以定义角色,然后按角色进行操作。
#!/usr/bin/pyhton
from fabric.api import *
env.user = ‘root‘
env.password = ‘vagrant‘
env.roledefs = {
‘web‘: [‘X.X.X.X‘],
‘dns‘: [‘Y.Y.Y.Y‘]
}
def test():
run(‘uname -a‘)
run:
fab -R web test
result:
[X.X.X.X] Executing task ‘test‘
[X.X.X.X] run: uname -a
[X.X.X.X] out: Linux salt-master 2.6.32-358.23.2.el6.x86_64 #1 SMP Wed Oct 16 18:37:12 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[X.X.X.X] out:
[Y.Y.Y.Y] Executing task ‘test‘
[Y.Y.Y.Y] run: uname -a
[Y.Y.Y.Y] out: Linux salt-minion-3 2.6.32-358.23.2.el6.x86_64 #1 SMP Wed Oct 16 18:37:12 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[Y.Y.Y.Y] out:
Done.
Disconnecting from Y.Y.Y.Y... done.
Disconnecting from X.X.X.X... done.
#从结果可以看到,fab只执行了属于web组的client。
4、extend属性
from fabric.api import env, run
env.hosts.extend([‘host3‘, ‘host4‘])
def test():
run(‘uname -r‘)
When this fabfile is run as fab -H host1,host2 mytask, env.hosts will then contain [‘host1‘, ‘host2‘, ‘host3‘, ‘host4‘] at the time that mytask is executed.
5、fabfile中没有定义client,可以在命令行中指定
fab mytask:hosts=‘X.X.X.X‘
6、使用task方式:
from fabric.api import hosts, run
my_hosts = (‘X.X.X.X‘, ‘Y.Y.Y.Y‘)
@hosts(my_hosts)
def test():
run(‘uname -r‘)
run:
fab test
resule: 从结果中可以看到,fab只执行了task定义的test部分
[X.X.X.X] Executing task ‘test‘
[X.X.X.X] run: uname -a
[X.X.X.X] out: Linux salt-master 2.6.32-358.23.2.el6.x86_64 #1 SMP Wed Oct 16 18:37:12 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[X.X.X.X] out:
[Y.Y.Y.Y] Executing task ‘test‘
[Y.Y.Y.Y] run: uname -a
[Y.Y.Y.Y] out: Linux salt-minion-1 2.6.32-358.23.2.el6.x86_64 #1 SMP Wed Oct 16 18:37:12 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[172.23.177.46] out:
Done.
Disconnecting from X.X.X.X... done.
Disconnecting from Y.Y.Y.Y... done.
7、综合应用:
from fabric.api import env, hosts, roles, run
env.roledefs = {‘role1‘: [‘b‘, ‘c‘]}
@hosts(‘a‘, ‘b‘)
@roles(‘role1‘)
def test():
run(‘uname -r‘)
8、遇到主机不可达的情况,可以使用--skip-bad-hosts参数,这样就会跳过不存在或有问题的client,而执行其他的client,从返回结果中可以排除那些是有问题的client,进而在进行处理
fab --skip-bad-hosts test
node:
my_hosts = (‘X.X.X.X‘)
@hosts(my_hosts)
def test():
run(‘uname -a‘)
本文出自 “muzinan的技术博客” 博客,请务必保留此出处http://muzinan110.blog.51cto.com/684213/1439966