python常用的运维脚本有哪些
发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章给大家分享的是有关python常用的运维脚本的介绍,小编觉得挺实用的,因此分享给大家学习,话不多说,一起往下看吧。1.获取外网ip#!/usr/bin/env python-*- coding
千家信息网最后更新 2025年02月03日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安全错误
数据库的锁怎样保障安全
怎么等ftp服务器
玩偶龙珠服务器
常州做软件开发什么价位
电脑网络技术入股
当前网络技术应用趋势
服务器安全狗+linux
上海运营网络技术包括什么
网络安全治理基石图片
企业怎么做好网络安全
服务器机柜安装教程图
福州青少年网络安全素养
郑州宾仕网络技术有限公司
数据库日志错误18456
ios清空流量数据库
考勤签到数据库设计
网络安全知名上市公司
优质软件开发的优服务
实有人口数据库发挥效能
云数据库4核8g够了吗
绥化华信网络技术支持
军营网络安全是什么
计算机网络技术的安全常识
网络安全倡议书顺口溜
辽宁省网络安全大讲堂朝阳站
戴尔服务器u盘启动按f几
ios清空流量数据库
外卖小程序软件开发搭建
粤苗app显示服务器异常
网络安全最低标准
云数据库4核8g够了吗