python常用的运维脚本有哪些
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章给大家分享的是有关python常用的运维脚本的介绍,小编觉得挺实用的,因此分享给大家学习,话不多说,一起往下看吧。1.获取外网ip#!/usr/bin/env python-*- coding
千家信息网最后更新 2025年12月02日python常用的运维脚本有哪些
这篇文章给大家分享的是有关python常用的运维脚本的介绍,小编觉得挺实用的,因此分享给大家学习,话不多说,一起往下看吧。
1.获取外网ip
#!/usr/bin/env python-*- coding:utf-8 -*-Time: 2019/12/20 10:05import socketimport requests,re#方法一text=requests.get("http://txt.go.sohu.com/ip/soip").textip=re.findall(r'\d+.\d+.\d+.\d+',text)#方法二ipqwb = socket.getaddrinfo('www.baidu.com', 'http') #获取指定域名的A记录nowIp = (ipqwb[0][4][0]) # 赋值print("本机外网IP: " + ip[0])print("qwb IP: " + nowIp)2.生成随机密码:
#!/usr/bin/env python# -*- coding:utf-8 -*-# Time: 2019/11/21 11:43import random,stringdef passwd(): src = string.ascii_letters + string.digits count = input('请确认要生成几条密码: ') list_passwds = [] for i in range(int(count)): #密码位数为N+3,例如下面就是5+3=8位密码 list_passwd_all = random.sample(src, 5) #从字母和数字中随机取5位 list_passwd_all.extend(random.sample(string.digits, 1)) #让密码中一定包含数字 list_passwd_all.extend(random.sample(string.ascii_lowercase, 1)) #让密码中一定包含小写字母 list_passwd_all.extend(random.sample(string.ascii_uppercase, 1)) #让密码中一定包含大写字母 random.shuffle(list_passwd_all) #打乱列表顺序 str_passwd = ''.join(list_passwd_all) #将列表转化为字符串 if str_passwd not in list_passwds: #判断是否生成重复密码 list_passwds.append(str_passwd) print(list_passwds[i]) #print(list_passwds)passwd()3.发送邮件:
#!/usr/bin/env python# -*- coding:utf-8 -*-# Time: 2019/11/15 17:18import smtplibfrom email.mime.text import MIMETextfrom time import sleepfrom email.header import Headerhost = 'smtp.163.com'port = 25sender = 'xxxx@163.com'pwd = 'xxxxx'receiver = ['22222222@qq.com', 'xxxxxxxx@163.com'] # 可以不用添加自己的邮箱,添加为了防止系统认为是垃圾邮箱发送失败会报错body = '邮件内容'title = '邮件标题'def sentemail(): msg = MIMEText(body, 'plain', 'utf-8') msg['subject'] = Header(title, 'utf-8').encode() msg['from'] = sender msg['to'] = ','.join(receiver) try: s = smtplib.SMTP(host, port) s.login(sender, pwd) s.sendmail(sender, receiver, msg.as_string()) print ('Done.sent email success') except smtplib.SMTPException as e: print ('Error.sent email fail') print (e)if __name__ == '__main__': sentemail()4.基础log日志配置:
#!/usr/bin/env python# -*- coding:utf-8 -*-# Time: 2019/11/27 13:04import loggingdef logger(): logger=logging.getLogger() fh=logging.FileHandler("test.log") #向文件中发送内容,有自己默认的日志格式 ch=logging.StreamHandler() #向屏幕发送文件,有自己默认的日志格式 fm=logging.Formatter("%(asctime)s %(message)s") #定义自己的日志格式 fh.setFormatter(fm) #添加自定义的日志格式,如果不添加会用自己默认的日志格式 ch.setFormatter(fm) logger.addHandler(fh) #显示出fh,ch的日志 logger.addHandler(ch) logger.setLevel("DEBUG") #定义日志级别 return logger # 返回函数对象logger=logger() #调用函数logger.debug("hello 1") #打印日志logger.info("hello 2")logger.warning("hello 3")logger.error("hello 4")logger.critical("hello 5")5.查看本地端口是否开放:
#!/usr/bin/env python# -*- coding:utf-8 -*-# Time: 2019/11/21 11:05import socketport_number = [135,443,80,3306,22]for index in port_number: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = sock.connect_ex(('127.0.0.1', index)) if result == 0: print("Port %d is open" % index) else: print("Port %d is not open" % index) sock.close()6.装饰器调用一次使函数执行5次
#!/usr/bin/env python-*- coding:utf-8 -*-def again_func(func): def inner(*args, **kwargs): for line in range(5): func(*args, **kwargs) return inner@again_funcdef func1(): print("from func1...")func1()7.可变参数定义*args, **kwargs的区别
#!/usr/bin/env python-*- coding:utf-8 -*-def foo(*args, **kwargs): print("args=:", args) print("kwargs=:", kwargs) print("-------------")if __name__ == '__main__': foo(1,2,3) foo(a=1, b=2, c=3) foo(1,2,3,a=1,b=2,c=3)以上便是python常用的运维脚本介绍,虽然从篇幅上看很复杂,但是示例代码非常详细且容易理解,如果想了解更多相关内容,请关注行业资讯。
日志
密码
utf-8
格式
内容
函数
字母
邮件
生成
常用
脚本
数字
文件
方法
邮箱
复杂
实用
不用
代码
位数
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
负责人翻译软件开发
数据库表格的时间类型
自己搭建一个服务器
mysql数据库难装
销售管理数据库设计结论
游戏机网络技术
淮北精益管理软件开发
公安网络安全大检查方案
连接云服务器超时是怎么回事
浙江浪潮服务器虚拟化系统物理机
杭州五际网络技术有限公司
招软件开发人才
云南企业交流外贸软件开发
福尔摩斯下载软件开发
apex选完服务器一直黑屏
软件开发的八个过程
新出的网络安全法律
宝山区智能软件开发售后服务
2018国家网络安全
广西网络技术专业情况
计箕机网络技术
广州太平洋网络技术公司地址
网络安全法处罚 医院
h3c服务器管理口装系统
清镇网络安全系统报价
sci数据库检索目录
软件开发需求分析师
服务器外访
从数据库导出 图表
探探协议软件开发者后台