[工具] Python 批量检测某一个软件包的安装情况 (通过读取和生成 CSV 文件实现)

实现脚本一:

介绍:

使用方法:
1. 将此脚本和 hostsrpms.csv 文件放在同一目录下
2. hostsrpms.csv 里每一个 IP 地址和每一个 RPM 包名称占用一行,并用逗号 “,” 隔开
3. 给此脚本添加执行权限
4. 执行此脚本
5. 此脚本执行完成后,会将运行结果写入当前目录下的 checkout.csv

注意:此脚本执行前必须要先保证执行本脚本的用户能无密码 ssh 远程这些远程服务器

补充:hostsrpms.csv 示例如下:

192.168.100.101,kernel-4.18.0-80.el8.x86_64,other0
192.168.100.102,kernel-4.18.0-80.el8.x86_64,other1
192.168.100.103,kernel-4.11.0-80.el8.x86_64,other2
192.168.100.104,kernel-4.18.0-80.el8.x86_64,other3
192.168.100.105,kernel-4.18.0-80.el8.x86_64,other4

脚本:

#!/usr/bin/python3

import csv
import subprocess

checkout=open('checkout.csv','w',encoding='utf-8')
csvfile=open('hostsrpms.csv','r',encoding='utf-8')
writer=csv.writer(checkout)

for lines in csvfile:
    lines=lines.strip('\n')
    hosts=lines.split(',')[0]
    rpms=lines.split(',')[1]
    result=subprocess.call('ssh -t %s "rpm -qa | grep %s" &> /dev/null' %(hosts,rpms) , shell=True)
    
    if result==0:
        writer.writerow((hosts,rpms,'yes'))
    else:
        writer.writerow((hosts,rpms,'no'))

csvfile.close()
checkout.close()

实现脚本二:

介绍:

使用方法:
1.将此脚本和 hostsrpms.csv 文件放在同一目录下
2.hostsrpms.csv 里每一个 IP 地址和每一个 RPM 包名称占用一行
3.给此脚本增加执行权限
4.执行此脚本
5.此脚本执行完成后,会将运行结果写入当前目录下的 checkout.csv

脚本:

#!/usr/bin/python3

import os
import paramiko
import re
import csv

port = 22
user = 'root'
password = ""
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

checkout = open('checkout.csv','w',encoding='utf-8')

with open('hostsrpms.csv','r',encoding='utf-8') as csvfile:
    for lines in csvfile:

        lines=lines.strip('\n')
        hosts=lines.split(',')[0]
        rpms=lines.split(',')[1]

        ssh.connect(hosts,port,user,password,timeout = 30)
        cmd="rpm -qa | grep "
        excute = cmd+rpms
        stdin,stdout,stderr = ssh.exec_command(excute)
        result = stdout.read()
        
        match=re.findall(rpms,result.decode('utf-8'))
        if match !=[]:
           checkout.write(hosts+','+rpms+',yes'+'\n')
        else:
           checkout.write(hosts+','+rpms+',no'+'\n')
        
        ssh.close()

checkout.close()

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

介绍:

使用方法:
1. 将此脚本和 ips.txt 文件放在同一目录下
2. ips.txt 里每一个 IP 地址占用一行
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 文件放在同一目录下
2. access.log 里每一个 IP 地址占用一行
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)