Linux Python 编程篇

lance 2020-03-31 PM 93℃ 0条

1: 写脚本生成随机的 20 个 ID

ID 格式要求:时间戳三位随机数字号码 8 位随机小写字母 1506571959089xxkeabef

#!/usr/bin/python
import datetime
idlist = []
for _ in range(20):
    s1 = datetime.datetime.now().timestamp()
    # 返回的是时间戳,但是带微秒
    s2 = ".join([str(random.randint(0,9)) for _ in range(3)])"
    s3 = ".join([chr(random.randint(97,122)) for _ in range(8)])"
    idlist.append(str(int(s1)) + '_' + s2 + '_' + s3)
print(idlist)

2: 写脚本判断密码强弱

要求密码必须由 10‐15 位指定字符组成:
十进制数字,大写字母,小写字母,下划线,要求四种类型的字符都要出现才算合法的强密码
例如:Aatb32_67mnq,其中包含大写字母、小写字母、数字和下划线,是合格的强密码

#!/usr/bin/python
s = input("请输入密码: ")
count = 0
flag1, flag2, flag3, flag4 = True, True, True, True
len = len(s)
if len >= 10 and len <= 15:
    for i in s:
        if i in "0123456789":
            if flag1:
                count += 1
            flag1 = False
        if i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
            if flag2:
                count += 1
            flag2 = False
        if i in "abcdefghijklmnopqrstuvwxyz":
            if flag3:
                count += 1
            flag3 = False
        if i in "_":
            if flag4:
                count += 1
            flag4 = False
    if count == 4:
        print("it's a right passwd")
    else:
        print("passwd is wrong")
else:
    print("the length is wrong")

3: 写脚本列举当前目录以及所有子目录下的文件,并打印出绝对路径

#!/usr/bin/env python
import os
for root, dirs, files in os.walk('/tmp'):
    for name in files:
        print(os.path.join(root, name))
os.walk()

4: 写脚本生成磁盘使用情况的日志文件

import time
import os
new_time = time.strftime('%Y‐%m‐%d')
disk_status = os.popen('df ‐h').readlines()
str1 = ''.join(disk_status)
f = file(new_time + '.log', 'w')
f.write('%s' % str1)
f.flush()
f.close()

5: 写脚本统计出每个 IP 的访问量有多少?(从日志文件中查找)

list = []
f = file('/usr/local/nginx/logs/access.log')
str1 = f.readlines()
f.close()
for i in str1:
    ip = i.split()[0]
    list.append(ip)
    list_num = set(list)
for j in list_num:
    num = list.count(j)
    print '%s : %s' % (j, num)

标签: linux, python, 运维宝典

非特殊说明,本博所有文章均为博主原创。

觉得文章不错,打赏一点吧,1分也是爱😀

WeChat Pay

微信打赏

Alipay

支付宝打赏

评论啦~