从Excel获取整列内容进行批量扫描

实习工作原因,需要测试excel表里面ip地址是否存在漏洞,扫了一眼,呕,四五百个IP,光是挨个进行访问,都是一个浩大的工程,所以准备开始摸鱼认真工作

思路是:excel按列提取->将IP按行存储在txt文件中->对TXT中的IP进行批量扫描操作

excel按列提取

参考自:https://www.cnblogs.com/xiao987334176/p/11240086.html

excel大概长这样:

1.png

使用python xlrd库,打开excel文件,创建一个workbook对象,book对象也就是我本地的wordexcel.xlsx

import xlrd
filename='workexcel.xlsx'
rbook=xlrd.open_workbook(filename)

选择第一个工作簿

rsheet=rbook.sheet_by_index(0)

我们遍历每一行,IP地址所在列下标为15,取其值

    for row in rsheet.get_rows():
        ips=row[15].value.split()

使用split()是为了防止有一个单元格中存在多行IP的情况

同时使用python集合来存储,防止重复:

    ipset=set()
    for row in rsheet.get_rows():
        ips=row[15].value.split()
        for ip in ips:
            if ip!="IP地址":
            # print("="*30)
                ipset.add(ip)

接着是保存ip的集合,这个比较简单,就直接给代码了:

def SaveSet(ipset):
    filename='test.txt'
    try:
        with open(filename, 'w') as f:
            while len(ipset) != 0:
                f.write(ipset.pop() + '
')
    except Exception as e:
        print(e)
        pass
    return

所有代码为:

import xlrd

def SaveSet(ipset):
    filename='test.txt'
    try:
        with open(filename, 'w') as f:
            while len(ipset) != 0:
                f.write(ipset.pop() + '
')
    except Exception as e:
        print(e)
        pass
    return

def GetData():
    filename='workexcel.xlsx'
    rbook=xlrd.open_workbook(filename)
    rsheet=rbook.sheet_by_index(0)
    ipset=set()
    for row in rsheet.get_rows():
        ips=row[15].value.split()
        for ip in ips:
            if ip!="IP地址":
            # print("="*30)
                ipset.add(ip)
    SaveSet(ipset)

def main():
    GetData()

if __name__=='__main__':
    main()

运行之后成功获取excel中的所有IP,接下来就能进行快乐批量啦~~

 

原文地址:https://www.cnblogs.com/Cl0ud/p/13402031.html