Skip to content

Commit

Permalink
《Python自动化运维:技术与最佳实践》V1
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkoliu committed Nov 23, 2014
1 parent 7944f68 commit aa1fdee
Show file tree
Hide file tree
Showing 1,615 changed files with 117,177 additions and 54 deletions.
54 changes: 0 additions & 54 deletions .gitignore

This file was deleted.

30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#《python自动化运维:技术与最佳实践》附带示例及案例源码

##章节列表
* 第一章
* 第二章
* 第三章
* 第四章
* 第五章
* 第六章
* 第七章
* 第八章
* 第九章
* 第十章
* 第十一章
* 第十二章
* 第十三章
* 第十四章
* 第十五章
* 第十六章

##问题反馈

在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流,谢谢。

* 问答平台 [qa.liuts.com](http://qa.liuts.com)
* 邮件([email protected])
* weibo: [@yorkoliu](http://weibo.com/u/1775431677)
* blog: [blog.liuts.com](http://blog.liuts.com)
* 微信号:yorkoliu
20 changes: 20 additions & 0 deletions 第一章/IPy/simple1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python

from IPy import IP

ip_s = raw_input('Please input an IP or net-range: ')
ips = IP(ip_s)


if len(ips) > 1:
print('net: %s' % ips.net())
print('netmask: %s' % ips.netmask())
print('broadcast: %s' % ips.broadcast())
print('reverse address: %s' % ips.reverseNames()[0])
print('subnet: %s' % len(ips))
else:
print('reverse address: %s' % ips.reverseNames()[0])

print('hexadecimal: %s' % ips.strHex())
print('binary ip: %s' % ips.strBin())
print('iptype: %s' % ips.iptype())
15 changes: 15 additions & 0 deletions 第一章/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#《Python自动化运维:技术与最佳实践》附带示例及案例源码

##第一章 目录说明
+ dnspython - dnspython模块示例
+ IPy - IPy模块示例

##问题反馈

在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流

* 问答平台 [qa.liuts.com](http://qa.liuts.com)
* 邮件([email protected])
* weibo: [@yorkoliu](http://weibo.com/u/1775431677)
* blog: [blog.liuts.com](http://blog.liuts.com)
* 微信号:yorkoliu
10 changes: 10 additions & 0 deletions 第一章/dnspython/simple1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import dns.resolver

domain = raw_input('Please input an domain: ')

A = dns.resolver.query(domain, 'A')
for i in A.response.answer:
for j in i.items:
print j.address

8 changes: 8 additions & 0 deletions 第一章/dnspython/simple2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python
import dns.resolver

domain = raw_input('Please input an domain: ')

MX = dns.resolver.query(domain, 'MX')
for i in MX:
print 'MX preference =', i.preference, 'mail exchanger =', i.exchange
9 changes: 9 additions & 0 deletions 第一章/dnspython/simple3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python
import dns.resolver

domain = raw_input('Please input an domain: ')
ns = dns.resolver.query(domain, 'NS')
for i in ns.response.answer:
for j in i.items:
print j.to_text()

10 changes: 10 additions & 0 deletions 第一章/dnspython/simple4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import dns.resolver

domain = raw_input('Please input an domain: ')

cname = dns.resolver.query(domain, 'CNAME')
for i in cname.response.answer:
for j in i.items:
print j.to_text()

42 changes: 42 additions & 0 deletions 第一章/dnspython/simple5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/python

import dns.resolver
import os
import httplib

iplist=[] #定义域名IP列表变量
appdomain="www.google.com.hk" #定义业务域名

def get_iplist(domain=""): #域名解析函数,解析成功IP将追加到iplist
try:
A = dns.resolver.query(domain, 'A') #解析A记录类型
except Exception,e:
print "dns resolver error:"+str(e)
return
for i in A.response.answer:
for j in i.items:
iplist.append(j.address) #追加到iplist
return True

def checkip(ip):
checkurl=ip+":80"
getcontent=""
httplib.socket.setdefaulttimeout(5) #定义http连接超时时间(5秒)
conn=httplib.HTTPConnection(checkurl) #创建http连接对象

try:
conn.request("GET", "/",headers = {"Host": appdomain}) #发起URL请求,添加host主机头
r=conn.getresponse()
getcontent =r.read(15) #获取URL页面前15个字符,以便做可用性校验
finally:
if getcontent=="<!doctype html>": #监控URL页的内容一般是事先定义好,比如“HTTP200”等
print ip+" [OK]"
else:
print ip+" [Error]" #此处可放告警程序,可以是邮件、短信通知

if __name__=="__main__":
if get_iplist(appdomain) and len(iplist)>0: #条件:域名解析正确且至少要返回一个IP
for ip in iplist:
checkip(ip)
else:
print "dns resolver error."
14 changes: 14 additions & 0 deletions 第七章/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#《Python自动化运维:技术与最佳实践》附带示例及案例源码

##第七章 目录说明
+ fabric - fabric模块示例

##问题反馈

在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流

* 问答平台 [qa.liuts.com](http://qa.liuts.com)
* 邮件([email protected])
* weibo: [@yorkoliu](http://weibo.com/u/1775431677)
* blog: [blog.liuts.com](http://blog.liuts.com)
* 微信号:yorkoliu
5 changes: 5 additions & 0 deletions 第七章/fabric/fabfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python
from fabric.api import run

def host_type():
run('uname -s')
14 changes: 14 additions & 0 deletions 第七章/fabric/simple1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python
from fabric.api import *

env.user='root'
env.hosts=['192.168.1.21','192.168.1.22']
env.password='SKJh935yft#'

@runs_once
def local_task():
local("uname -a")

def remote_task():
with cd("/data/logs"):
run("ls -l")
18 changes: 18 additions & 0 deletions 第七章/fabric/simple2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
from fabric.api import *

env.user='root'
env.hosts=['192.168.1.21','192.168.1.22']
env.password="SKJh935yft#"

@runs_once
def input_raw():
return prompt("please input directory name:",default="/home")

def worktask(dirname):
run("ls -l "+dirname)

@task
def go():
getdirname = input_raw()
worktask(getdirname)
36 changes: 36 additions & 0 deletions 第七章/fabric/simple3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import confirm

env.user='root'
env.gateway='192.168.1.23'
env.hosts=['192.168.1.21','192.168.1.22']
env.passwords = {
'[email protected]:22': 'SKJh935yft#',
'[email protected]:22': 'SKJh935yft#',
'[email protected]:22': 'KJSD9325hgs'
}

lpackpath="/home/install/lnmp0.9.tar.gz"
rpackpath="/tmp/install"

@task
def put_task():
run("mkdir -p /tmp/install")
with settings(warn_only=True):
result = put(lpackpath, rpackpath)
if result.failed and not confirm("put file failed, Continue[Y/N]?"):
abort("Aborting file put task!")

@task
def run_task():
with cd("/tmp/install"):
run("tar -zxvf lnmp0.9.tar.gz")
with cd("lnmp0.9/"):
run("./centos.sh")

@task
def go():
put_task()
run_task()
39 changes: 39 additions & 0 deletions 第七章/fabric/simple4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import confirm

env.user='root'
env.hosts=['192.168.1.21','192.168.1.22','192.168.1.23']
env.password='SKJh935yft#'


@task
@runs_once
def tar_task():
with lcd("/data/logs"):
local("tar -czf access.tar.gz access.log")
@task
def put_task():
run("mkdir -p /data/logs")
with cd("/data/logs"):
with settings(warn_only=True):
result = put("/data/logs/access.tar.gz", "/data/logs/access.tar.gz")
if result.failed and not confirm("put file failed, Continue[Y/N]?"):
abort("Aborting file put task!")

@task
def check_task():
with settings(warn_only=True):
lmd5=local("md5sum /data/logs/access.tar.gz",capture=True).split(' ')[0]
rmd5=run("md5sum /data/logs/access.tar.gz").split(' ')[0]
if lmd5==rmd5:
print "OK"
else:
print "ERROR"

@task
def go():
tar_task()
put_task()
check_task()
43 changes: 43 additions & 0 deletions 第七章/fabric/simple5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python
from fabric.colors import *
from fabric.api import *

env.user='root'
env.roledefs = {
'webservers': ['192.168.1.21', '192.168.1.22'],
'dbservers': ['192.168.1.23']
}

env.passwords = {
'[email protected]:22': 'SKJh935yft#',
'[email protected]:22': 'SKJh935yft#',
'[email protected]:22': 'KJSD9325hgs'
}

@roles('webservers')
def webtask():
print yellow("Install nginx php php-fpm...")
with settings(warn_only=True):
run("yum -y install nginx")
run("yum -y install php-fpm php-mysql php-mbstring php-xml php-mcrypt php-gd")
run("chkconfig --levels 235 php-fpm on")
run("chkconfig --levels 235 nginx on")

@roles('dbservers')
def dbtask():
print yellow("Install Mysql...")
with settings(warn_only=True):
run("yum -y install mysql mysql-server")
run("chkconfig --levels 235 mysqld on")

@roles ('webservers', 'dbservers')
def publictask():
print yellow("Install epel ntp...")
with settings(warn_only=True):
run("rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm")
run("yum -y install ntp")

def deploy():
execute(publictask)
execute(webtask)
execute(dbtask)
Loading

0 comments on commit aa1fdee

Please sign in to comment.