[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 codegay 于 2018-4-27 05:14 编辑

python3代码。思路是把ip地址转成一个10进制的自然数,然后range范围。再把数字转成IP地址。
IP地址转换成数字的部分参考了 https://blog.csdn.net/zhihaoma/article/details/51841169 等相关文章。
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Apr 27 01:45:55 2018
  4. @author: codegay
  5. python3 & 编辑器 == spyder
  6. """
  7. # 把IP地址转换为一个自然数
  8. def IPToN(ipaddr, sep='.'):
  9.     ip = [int(i) for i in ipaddr.split(sep)]
  10.     result = ip[0]*256**3 + ip[1]*256**2 + ip[2]*256 + ip[3]
  11.     return result
  12. # 把一个自然数转换成IP
  13. def NToIP(natural, sep='.'):
  14.     ip = int(natural)
  15.     result = [ip >> 24, (ip & 0x00FF0000) >> 16, (ip & 0x0000FF00) >> 8, ip & 0x000000FF]
  16.     result = sep.join(map(str, result))
  17.     return result
  18. def iprange(start, end, newline=''):
  19.     result = [NToIP(n)+newline for n in range(IPToN(start), IPToN(end)+1)]
  20.     return result
  21. print(IPToN("58.217.200.112"))
  22. print(IPToN("10.0.3.193"))
  23. print(NToIP(987351152))
  24. print(iprange("192.168.1.1", "192.168.1.255"))
  25. with open("ip.txt") as f:
  26.     lines = [r.rstrip().split(" ") for r in f.readlines()]
  27.     for line in lines:
  28.         with open("result.txt", "w+") as rf:
  29.             rf.writelines(iprange(line[0], line[1], newline='\r\n'))
复制代码
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

返回列表