[工具] Python 批量多线程检测服务器的联通状态

介绍

使用方法

1. 将此脚本和 ips.txt 文件放在同一目录下
2. ips.txt 里每 1 个 IP 地址占用 1 行
3. 给此脚本添加执行权限
4. 执行此脚本

脚本

#!/usr/bin/python3

import subprocess
import threading

def newping(ip):
    m=subprocess.call('ping -c3 -i0.4 -w0.8 %s &> /dev/null' %ip , shell=True)
    if m==0:
        print("%s is up" %ip)
    else:
        print("%s is down" %ip)

ips=open('ips.txt','r')

for lines in ips:
    lines=lines.strip('\n')
    newslines = threading.Thread(target=newping,args=[lines])
    newslines.start()

ips.close

[工具] Python 统计文件中 IP 地址出现的次数

介绍

使用方法

1. 将此脚本和 access.log 文件放在同 1 目录下
2. access.log 里每 1 个 IP 地址占用 1 行
3. 给此脚本添加执行权限
4. 执行此脚本

脚本

#!/usr/bin/python3

import re
ips={}

log=open('access.log')

for i in log:
    ip=re.search('(\d+.){3}\d+',i)
    if ip:
        ip=ip.group()
        ips[ip]=ips.get(ip,0)+1

print(ips)

[工具] Python 计算斐波那契数列

介绍

使用方法

1. 给此脚本添加执行权限
2. 执行此脚本

脚本

#!/usr/bin/python3

print('Fibonacci sequence will be calculated')
num=input('Please make sure to calculate the number of months? ')

fb=[0,1]
month=int(num)

for i in range(1,month-2):
    new=fb[-1]+fb[-2]
    fb.append(new)
print(fb)