python运维开发笔记4

发布时间:2019-06-23 08:44:19编辑:auto阅读(1387)

    1.函数如何被调用,通过return返回值来调用

    2.生成器和return区别

    yield 生成器返回对象,可以迭代

    可以执行


    glob模块 类似shell中的正则匹配

    shlex模块  Popen 将命令参数直接分词

    cmd = "ps ax -o pid,ppid,cmd"

    shlex.split(cmd)

    ['ps','ax','-o','pid,ppid,cmd']

    ['mysql','-u','root','-p123','-e','show processlist']

    p = Popen(shlex.split(cmd),stdout=PIPE)

    p.communicate()

    ('',None)

    代替

    p.stdin.write()

    p.stdin.close()

    p.stdout.read()


    p.terminate()#终止执行

    p = Popen(['sleep','10'],stdout=PIPE)

    p.pid

    kill -HUP

    HUP(1) 是让进程挂起,睡眠;

    kill(9) 六亲不认的杀掉

    term(15) 正常的退出进程

    因为进程可能屏蔽某些信号,所以它们的用处也就不一样i


    内核产生的2,是用户产生的1

    列表变成字符串  ''.join(s[2:])

    字符串变成列表  s.split()


    搜集主机信息

    devname

    macaddr

    ipaddr


    字符串换行符  split,列表最后一个元素为空

    [i.strip() for i in data.split('\n') if i]


    定义函数的参数是需要传值的


    正则表达式

    re模块

    分析日志,提取信息

    普通字符

    元字符:

    . 除了换行符\n

    ^  

    * 前面的字母出现0次或者n次

    {} 数字

    [] 字符串

    () 分组

    \  转义符号

    |  或

    import re

    p = re.compile('ab*')

    re.match

    re.match 尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词。 


    import re

    text = "JGood is a handsome boy, he is cool, clever, and so on..."

    m = re.match(r"(\w+)\s", text)

    if m:

    print m.group(0), '\n', m.group(1)

    else:

    print 'not match'  


    re.match的函数原型为:re.match(pattern, string, flags)


    第一个参数是正则表达式,这里为"(\w+)\s",如果匹配成功,则返回一个Match,否则返回一个None;


    第二个参数表示要匹配的字符串;


    第三个参数是标致位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。


    re.search

    re.search函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回,如果字符串没有匹配,则返回None。


    import re

    text = "JGood is a handsome boy, he is cool, clever, and so on..."

    m = re.search(r'\shan(ds)ome\s', text)

    if m:

    print m.group(0), m.group(1)

    else:

    print 'not search'  


    re.search的函数原型为: re.search(pattern, string, flags)

    每个参数的含意与re.match一样。 


    re.match与re.search的区别:re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。

     

    re.sub

    re.sub用于替换字符串中的匹配项。下面一个例子将字符串中的空格 ' ' 替换成 '-' :  


    import re

    text = "JGood is a handsome boy, he is cool, clever, and so on..."

    print re.sub(r'\s+', '-', text) 

    re.sub的函数原型为:re.sub(pattern, repl, string, count)


    其中第二个函数是替换后的字符串;本例中为'-'


    第四个参数指替换个数。默认为0,表示每个匹配项都替换。


    re.sub还允许使用函数对匹配项的替换进行复杂的处理。如:re.sub(r'\s', lambda m: '[' + m.group(0) + ']', text, 0);将字符串中的空格' '替换为'[ ]'。


    re.split

    可以使用re.split来分割字符串,如:re.split(r'\s+', text);将字符串按空格分割成一个单词列表。


    re.findall

    re.findall可以获取字符串中所有匹配的字符串。如:re.findall(r'\w*oo\w*', text);获取字符串中,包含'oo'的所有单词。


    re.compile

    可以把正则表达式编译成一个正则表达式对象。可以把那些经常使用的正则表达式编译成正则表达式对象,这样可以提高一定的效率


    re.S

    re.I

    re.M

    re.I|re.M|re.S

    re.I:忽略大小写

    re.L:表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境

    re.M:多行模式

    re.S:’ . ’并且包括换行符在内的任意字符(注意:’ . ’不包括换行符)

    re.U: 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库

     

    re.findall(r'[gG].d',s1,re.s)

    reg = re.compile(r'[gG].d',re.s)


    p = r'[0-9a-zA-Z_]{1,16}@[0-9a-zA-Z_-]{1,}\.[a-zA-Z]{2,3}'#匹配邮箱地址


    python正则匹配HTML标签

    设计3个需求:

    1. 取<script> </script>标签之间的内容。

    >>> re.findall('<script>([\s\S]*?)</script>', s)

    [" alert('test') "]


    2. 取包含<script ...></script>标签以及之间的内容。

    >>> re.findall('<script[\s\S]*?</script>', s)

    ['<script type="text/javascript" src="/scripts/jquery.js"></script>', "<script> alert('test') </script>"]


    3. 取<script> </script>标签之外所有的内容。

    >>> re.sub('<script[\s\S]*?</script>', '', s)

    '\n<html>\n<head>\n<title>This is title</title>\n\n</head>\n<body>\n<h1>hello world!</h1>\n\n</body>\n</html>\n'

    >>> re.subn('<script[\s\S]*?</script>', '', s)

    ('\n<html>\n<head>\n<title>This is title</title>\n\n</head>\n<body>\n<h1>hello world!</h1>\n\n</body>\n</html>\n', 2)


    匹配IP,MAC,IFNAME


    作业 

    1./proc  status  cmdline  实现'pid'  'ppid'  'cmd'

    2.统计所有httpd进程占用物理内存多少,物理内存比例是多少   status中VmRSS  apache的内存值

    /proc/meminfo 总的内存


    取得http所有的pid号,依次取得进程号下所有的VmRSS的值

    #ps -e 

    #正则过滤 httpd pid号

    #用pid号找内存

    #正则过滤出apache内存的值 

    for循环得出总的apache的内存值

    计算公式除meminfo的内存 Memtotal的值


    TypeError: expected string or buffer

    原因:re_h=re.compile('</?\w+[^>]*>')

             s=re_h.sub('',str)


             传入的str是list变量导致出错


    解决办法:传入str类型变量

关键字