侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

20170220--python--log+ssh

2023-11-25 星期六 / 0 评论 / 0 点赞 / 47 阅读 / 8730 字

syslog --只能用在linux系统下 自定义logging md5值的生成 mylog.py --用logging写一个日志备份脚本 1 #!/usr/bin/env python

syslog --只能用在linux系统下

自定义logging

md5值的生成

mylog.py --用logging写一个日志备份脚本

  1 #!/usr/bin/env python  2 # coding: utf8  3 #  4 # /etc/hosts  /tmp/hosts-20170220  5 #  6   7 import shutil  8 import time  9 import logging 10 import hashlib 11  12 logging.basicConfig(filename="/tmp/myapp.log", format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO)  //定义日志输出到屏幕的级别为INFO 13  14 nowdate = time.strftime("%Y%m%d") 15  16 logging.info("backup begin...")   //默认情况下,logging将日志打印到屏幕,日志级别为WARNING;日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。 17 shutil.copy2("/etc/hosts", "/tmp/hosts-%s" % nowdate) 18 logging.info("backup end!") 19  20 mymd5 = hashlib.md5() 21 with open("/etc/hosts") as f: 22     mymd5.update(f.read()) 23     oldmd5 = mymd5.hexdigest() 24  25 mymd5 = hashlib.md5() 26 with open("/tmp/hosts-%s" % nowdate) as f: 27     mymd5.update(f.read()) 28     newmd5 = mymd5.hexdigest() 29  30 if oldmd5 == newmd5: 31     logging.info("backup OK") 32 else: 33     logging.error("backup KO")######################################################logging.basicConfig函数各参数:filename: 指定日志文件名filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示: %(levelno)s: 打印日志级别的数值 %(levelname)s: 打印日志级别名称 %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0] %(filename)s: 打印当前执行程序名 %(funcName)s: 打印日志的当前函数 %(lineno)d: 打印日志的当前行号 %(asctime)s: 打印日志的时间 %(thread)d: 打印线程ID %(threadName)s: 打印线程名称 %(process)d: 打印进程ID %(message)s: 打印日志信息datefmt: 指定时间格式,同time.strftime()level: 设置日志级别,默认为logging.WARNINGstream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

二、捕获异常(ctrl+c)

sigal 模块 用signal.SIGINT捕获信号,用signal.SIG_IGN忽略捕获到的信号

三、paramiko--python 中最常用的连接ssh的模块

第8行的作用是相当于我们初次连接用户的时候需要回答yes/no 第8行的意思就是帮我们默认回答为yes

 

用我们的私钥进行连接

 

#!/usr/bin/env python  2 # coding: utf8  3 #  4   5 import os  6 import yaml  7 import paramiko  8   9 from multiprocessing.dummy import Pool 10 #from multiprocessing import Pool 11  12 with open("hosts.yaml") as f: 13     config = yaml.safe_load(f) 14  15 hosts = config.items() 16  17  18 def runcmd(info): 19     #print host[0], ": ", host[1].get("host") 20     hostname = info[0] 21     hostinfo = info[1] 22  23     if hostinfo.get("pkey"): 24         keyfile = os.path.expanduser(hostinfo.get("pkey")) 25     else: 26         keyfile = None 27  28     client = paramiko.SSHClient() 29     client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 30  31     try: 32         client.connect(hostinfo.get("host"), hostinfo.get("port", 2200), hostinfo.get("user", "root"), str(hostinfo.get("    pass", "")), key_filename=keyfile, timeout=5) 33     except: 34         print hostname + ": Connect Fail!" 35     else: 36         _, stdout, stderr = client.exec_command("free -m") 37         print hostname + ":" 38         print stdout.read() 39     finally: 40         client.close() 41  42 #map(runcmd, hosts) 43 pool = Pool() 44 pool.map(runcmd, hosts) 45 pool.close() 46 pool.join()

 

hosts.yaml

 up17:  2     host: 192.168.30.17  3     user: root  4     pass: xy9860  5     port: 22  6     pkey: ~/.ssh/id_rsa.bak2  7   8 localhost:  9     host: localhost 10     user: tom 11     pass: uplooking 12     port: 2200 13  14 up153: 15     host: 192.168.30.153 16     user: root 17     pass: 123456 18     port: 22 19  20 up20: 21   host: 192.168.30.17 22   port: 22 23   user: root                                                              1,1           Top

 

 

四、配置文件,可以使用python的模块机制,直接将配置文件import使用

如果我们用yaml格式保存配置文件。

 

将修改的内容写入到配置文件中

 

yaml的一般格式为

五、

threading 多线程

multiprocessing 多进程

multiprocessing.dummy 多线程

IO 密集型--- 多线程

CPU 密集型---多进程 core

产生100个线程提供服务

 

加入正则表达式的多线程

1 #!/usr/bin/env python  2 # coding: utf8  3 #  4   5 import re  6 import os  7 import sys  8 import yaml  9 import paramiko 10  11 from multiprocessing.dummy import Pool,Lock 12 #from multiprocessing import Pool 13  14 from optparse import OptionParser 15  16 op = OptionParser() 17 op.add_option("-r", dest="regex", help="regex pattern", metavar="REGEX", def    ault=r'.*') 18 op.add_option("-e", dest="cmd", help="run cmd", metavar="CMD", default="date    ") 19  20 options, _ = op.parse_args() 21   22 myre = re.compile(options.regex) 23  24 with open("hosts.yaml") as f: 25     config = yaml.safe_load(f) 26  27 hosts = config.items() 28  29 lock = Lock() 30  31 def runcmd(info): 32     global lock 33     #print host[0], ": ", host[1].get("host") 34     hostname = info[0] 35     hostinfo = info[1] 36  37     if hostinfo.get("pkey"): 38         keyfile = os.path.expanduser(hostinfo.get("pkey")) 39     else: 40         keyfile = None 41  42     client = paramiko.SSHClient() 43     client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 44  45     try: 46         client.connect(hostinfo.get("host"), hostinfo.get("port", 2200), hos    tinfo.get("user", "root"), str(hostinfo.get("pass", "")), key_filename=keyfi    le, timeout=5) 47     except: 48         lock.acquire() 49         print hostname + ": Connect Fail!" 50         lock.release() 51     else: 52         _, stdout, stderr = client.exec_command(options.cmd) 53         lock.acquire() 54         print hostname + ":/n", stdout.read() 55         lock.release() 56     finally: 57         client.close() 58  59 #[("up10", {host: port ..),(),()] 60 #match_hosts = [] 61 #for h in hosts: 62 #    if myre.search(h[0]): 63 #        match_hosts.append(h) 64  65 match_hosts = [h for h in hosts if myre.search(h[0])] 66 #match_hosts = filter(lambda h: myre.search(h[0]), hosts) 67  68 #map(runcmd, hosts) 69 pool = Pool() 70 pool.map(runcmd, match_hosts) 71 pool.close() 72 pool.join()

 

Python 与数据库的连接

python与数据库的连接,必须定义相应的方法

 

广告 广告

评论区