1.列举当前目录以及所有子目录下的文件,并打印出绝对路径#!/usr/bin/python# coding=utf8import osimport sys if len(sys.argv) < 2: filepath="."else: filepath=sys.argv[1]for root,dirs,files in os.walk(filepath): for filename in files: path=os.path.join(root,filename) print path2.写一个函数,计算字符串中所有数字的和#!/usr/bin/python# coding=utf8import sysnumber="123456"def Numsum(num): sum=0 if len(num) > 0: for i in range(len(num)): if num[i] > "0" and num[i] < "9": sum=sum+int(num[i]) print "sum=%s" % sum else: sys.exit(1)if __name__ == "__main__": Numsum(number)3.每天生成一个文件,并把磁盘的使用情况写到到这个文件中,文件名为日期格式(yyyy-mm-dd),如2018-06-10.log #!/usr/bin/python# coding=utf8import timeimport osimport getoptimport sysdisk_path="/root/disk"memory_path="/root/memory"if not os.path.exists(disk_path): os.makedirs(disk_path)if not os.path.exists(memory_path): os.makedirs(memory_path)def Disk(time): status=os.popen("df -hTP").readlines() a="".join(status) f=open("/root/disk/"+time+".log","w") f.write(a) f.close()def Memory(time): status=os.popen("free -g").readlines() a="".join(status) f=open("/root/memory/"+time+".log","w") f.write(a) f.close()time = time.strftime("%Y-%m-%d-%H:%M:%S")options,args = getopt.getopt(sys.argv[1:],"-d-m",["disk_status","memory_status"])for opt_name,opt_value in options: if opt_name in ("-d","--disk_status"): try: Disk(time) print "Disk_status Complete" exit() except: print "Fail" if opt_name in ("-m","--memory_status"): try: Memory(time) print "Memory_status Complete" exit() except: print "Fail"4.从nginx日志中统计出每个IP的访问量有多少,访问量超过10次的ip,用防火墙禁止使用,并发送邮件,三天后再打开限制,#!/usr/bin/python#coding=utf8import osimport subprocessfrom email.mime.text import MIMETextimport smtplibrefuse_ip = "/etc/nginx/refuse_nginx"sender = "wjq@123"receiver = "wujqc@yonyou.com"password = "123456"subject = "nginx ip refuse"def SendMail(ip): try: print "11111" content = ip+" is refuse" msg = MIMEText(content,"plain","utf-8") msg["From"] = sender msg["To"] = receiver msg["Subject"] = subject server=smtplib.SMTP("localhost") server.sendmail(sender,receiver,msg.as_string()) print "发送成功" except smtplib.SMTPException: print "发送失败" if not os.path.exists(refuse_ip): os.mknod(refuse_ip)def nginx_protect(ip): f=open(refuse_ip,"r+") cmd="iptables -A INPUT -p tcp --dport 80 -s "+ip+" -j DROP" cmd2="at now + 1 minutes << EOF iptables -D INPUT -p tcp --dport 80 -s "+ip+" -j DROP sed -i "s/"+ip+"//" /etc/nginx/refuse_nginx EOF" for i in f: if ip not in i: f.write(ip) os.popen(cmd) os.popen(cmd2) SendMail(ip) print("ip 限制成功") list=[]nginx_file="/var/log/nginx/access.log"f=file(nginx_file).readlines()for i in f: nginx_ip=i.split()[0] list.append(nginx_ip)nginxip=set(list)for j in nginxip: num=list.count(j) if num > 10: nginx_protect(j) print "IP:%s NUM:%s" % (j,num)5.写一个脚本计算出所有进程所占用内存大小#!/usr/bin/python# coding=utf8import oslist=[]cmd="ps aux"sum=0status=os.popen(cmd).readlines()for i in status: a=i.split()[5] list.append(a)for i in list[1:-1]: sum=sum+int(i)print "%s:%sB" % (list[0],sum) 6.MySQL状态监控#!/usr/bin/python#coding=utf8import MySQLdbhost="localhost"user="wjq"passwd="123456"db="test"Com_insert="show global status like "Com_insert";"Com_update="show global status like "Com_update";"Com_select="show global status like "Com_select";"Com_delete="show global status like "Com_delete";"Open_tables="show global status like "Open_tables";"Qcache_hits="show global status like "Qcache_hits";"def getConn(host,user,passwd,db): try: conn = MySQLdb.connect(host,user,passwd,db) return conn except: print("数据库连接失败")def getValue(conn,query): cursor = conn.cursor() getNum = cursor.execute(query) if getNum > 0: data = cursor.fetchone() return int(data[1]) if __name__ == "__main__": conn=getConn(host,user,passwd,db) Com_insert=getValue(conn,Com_insert) Com_update=getValue(conn,Com_update) Com_select=getValue(conn,Com_select) Com_delete=getValue(conn,Com_delete) Open_tables=getValue(conn,Open_tables) Qcache_hits=getValue(conn,Qcache_hits) print " *****MySQL Status*****" print " Com_insert:%s " % Com_insert print " Com_update:%s " % Com_update print " Com_select:%s " % Com_select print " Com_delete:%s " % Com_delete print " Open_tables:%s " % Open_tables print " Qcache_hits:%s " % Qcache_hits7.自定义密码长度,生成随机密码#!/usr/bin/python# coding=utf8import randomlist=[]def Passwd(num): for i in range(int(num)): a=random.randrange(0,int(num)) if i == a: b1=random.randint(0,9) list.append(str(b1)) else: b2=chr(random.randint(65,90)) list.append(b2) b3="".join(list) return b3 if __name__ == "__main__": num=raw_input("生成密码的长度为:") password=Passwd(num) print password8.python实现mysql的zabbix监控脚本#!/usr/bin/python#coding=utf8import MySQLdbimport syshost="localhost"user="wjq"passwd="123456"db="test"Com_insert="show global status like "Com_insert";"Com_update="show global status like "Com_update";"Com_select="show global status like "Com_select";"Com_delete="show global status like "Com_delete";"Open_tables="show global status like "Open_tables";"Qcache_hits="show global status like "Qcache_hits";"def getConn(host,user,passwd,db): try: a = MySQLdb.connect(host,user,passwd,db) return a except: print "数据库连接失败"def getValue(conn,query): try: cursor = conn.cursor() getNum = cursor.execute(query) if getNum > 0: data = cursor.fetchone() return int(data[1]) except: print "查询失败"conn = getConn(host,user,passwd,db)if sys.argv[1] == "insert": Com_insert = getValue(conn,Com_insert) print Com_insert elif sys.argv[1] == "delete": Com_delete = getValue(conn,Com_delete) print Com_deleteelif sys.argv[1] == "select": Com_select = getValue(conn,Com_select) print Com_selectelif sys.argv[1] == "update": Com_update = getValue(conn,Com_update) print Com_updateelif sys.argv[1] == "table_num": Open_tables = getValue(conn,Open_tables) print Open_tableselif sys.argv[1] == "cache_hit": Qcache_hits = getValue(conn,Qcache_hits) print Qcache_hits

python运维自动化脚本案例-python自动化运维脚本范例相关推荐

  1. python脚本案例-python+adb命令实现自动刷视频脚本案例

    python小白第一次发博客,自己自学了一下写了一个demo,可能语法啥的不够标准,毕竟没有真正学过python 主要用到的是 import os #os包用于运行cmd命令 adb shell #这 ...

  2. python脚本案例-python脚本范例

    广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. [伯乐在线导读]:有网友在 quora 上提问,「你用 python 写 ...

  3. python递归函数例题_递归案例python

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 而对应的中文翻译 "递归" 却表达了两个意思:"递 ...

  4. python自动化脚本编写教程_开发工具pycharm写第一个Python自动化程序案例|python基础教程|python入门|python教程...

    https://www.xin3721.com/eschool/pythonxin3721/ 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. ...

  5. python 自动化办公 案例_python自动化工具之pywinauto实例详解

    python自动化工具之pywinauto实例详解 来源:中文源码网 浏览: 次 日期:2019年11月5日 [下载文档: python自动化工具之pywinauto实例详解.txt ] (友情提示: ...

  6. python自动化办公案例-python自动化办公:文件篇(自动整理文件,一键完成)

    import os list_all=[]#初始化一个空列表 for root ,dirs,files in os.walk(r'C:UsersShineionDesktop新建文件夹'): for ...

  7. linux 脚本案例,30个关于Shell脚本的经典案例(上)

    对于初学者而言,因为没有实战经验,写不出来 很正常,如果工作了几年的运维老年还是写不出来,那就是没主动找需求,缺乏练习,缺乏经验.针对以上问题,总结了30个生产环境中经典的 ,通过这些需求案例,希望能 ...

  8. python自动化办公 51cto_利用python实现批量自动化运维脚本案例

    本文为通过密码或密钥实现python批量自动化运维脚本案例分享,是老男孩linux培训 python课程教学案例内容,后续会分享多线程并发执行这个脚本的更高级的功能(http://oldboy.blo ...

  9. python paramiko模块下载_Python自动化运维实战:使用Python管理网络设备

    现在,我们已经知道如何在不同的操作系统中使用和安装Python以及如何使用EVE-NG搭建网络拓扑.在本章中,我们将学习如何使用目前常用的网络自动化库自动完成各种网络任务.Python可以在不同的网络 ...

最新文章

  1. BeginInvoke与EndInvoke方法解决多线程接收委托返回值问题
  2. div固定在浏览器顶部_手写几种常见的css布局,1垂直两栏左边固定右边自适应,垂直3栏左右固定中间自适应...
  3. mysql会话超时机制_mysql超时机制
  4. Angular Material 攻略 04 Icon
  5. 音标,口型,舌位对照表:适合已有一定英语基础的人重新回顾音标发音,在不断的练习中让自己的发音更准确,口型更标准。...
  6. 使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中lt;meta-datagt;变量的值...
  7. C++解析char *p与char p[]
  8. 计算机毕业设计-ssm超市进销存管理系统(项目+类似文档)超市仓库管理系统javaweb-超市库存预警管理系统源码
  9. [转][学习]软件绿色联盟应用体验标准5.0_功耗标准-公示版
  10. 字符串与vector和list
  11. matplotlib命令与格式:系统字体与显示中文
  12. 2022-2027年中国安全仪表系统(SIS)行业发展监测及投资战略研究报告
  13. php 499,服务频繁499(Nginx+PHP-fpm),应该从哪些角度入手追查这问题?
  14. 实力认证!Coremail连续9次入围安全牛《中国网络安全行业全景图》
  15. OpenCV C++案例实战五《答题卡识别》
  16. python制作炫酷吊炸天的二维码
  17. 计算机二级系统班C语言讲义,计算机二级C语言2017辅导讲义:C语言基本知识
  18. 20230208 对偶四元数的乘法
  19. 一些程序设计常识(bushi)
  20. 产品部和业务部门的利益之争

热门文章

  1. 2022年计算机软件水平考试程序员系统集成项目管理工程师(中级)练习题及答案
  2. php的redis函数
  3. 中小学计算机教学大纲,中小学信息技术教材教法教学大纲.doc
  4. S4 BOM批量修改
  5. JS实现Excel表格数据的导出
  6. 【git新建分支拉取分支】git submodule update --init --recursive
  7. JAVA day06:代码作业(封装)
  8. python 进行深入学习
  9. 高温熔融玻璃液位检测用激光玻璃液位计
  10. 服务器挂起文件怎么删除,挂起的更改